By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,468 Members | 1,859 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,468 IT Pros & Developers. It's quick & easy.

Question about static methods

P: n/a
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error message
telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can
still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error message telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can
still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


You need to instantiate Blah and use your instance of blah to call the
instance method.

Which means...

Blah jedi = new Blah();
jedi.doStuff();

.... goes in your main method

and doStuff() becomes an instance method (get rid of 'static').

John
Jul 17 '05 #2

P: n/a

"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:
<SNIP>
I'd like to be able to not have to make the method
doStuff() static and would like to simply call it "pubic
void doStuff" but I get an error message telling me I can
not call a non-static class from a static method (since
main is static). Is there a way to get around this so that
doStuff() can still be non-static?


The following will do the trick:

public class Blah
{
public static void main (String [] args)
{
Blah blah1 = new Blah(2), blah2 = new Blah(6);
blah1.doStuff();
blah2.doStuff();
}

public Blah(int number) { this.number = number; }

public void doStuff()
{
// ...
System.out.println(number);
// ...
}

private int number;
}

I strongly urge you to complete the tutorials at the Sun Java site:

http://java.sun.com/docs/books/tutorial/index.html

I hope this helps.

Anthony Borla
Jul 17 '05 #3

P: n/a
AckbarJedi wrote:
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff"


I'm sure you've been given good advice but it might also help if you
called the method "public void doStuff()"!

Jul 17 '05 #4

P: n/a
"John" <?> wrote in message
news:40*********************@lovejoy.zen.co.uk...
"AckbarJedi" <Ac***************@hotSPAMmail.com> wrote in message
news:40********@corp.newsgroups.com...
Given this very basic program:

public class Blah {

public static void main (String [] args) {

doStuff(); //Calls the method doStuff

}//Closes main

public static void doStuff(); {
severalCommands;
} //Closes doStuff

} //Closes public class blah

I'd like to be able to not have to make the method doStuff() static and
would like to simply call it "pubic void doStuff" but I get an error

message
telling me I can not call a non-static class from a static method (since
main is static). Is there a way to get around this so that doStuff() can still be non-static?

Thanks.

-AckbarJedi


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----


You need to instantiate Blah and use your instance of blah to call the
instance method.

Which means...

Blah jedi = new Blah();
jedi.doStuff();

... goes in your main method

and doStuff() becomes an instance method (get rid of 'static').

John

Thanks. That's extremely helpful and solves the problem elegantly!

--
AckbarJedi,
---
"That theory is worthless. It isn't even wrong!" ~Wolfgang Pauli
---
To e-mail remove DeathtoSpam from my e-mail address.


-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 100,000 Newsgroups - 19 Different Servers! =-----
Jul 17 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.