This week's giveaway is in the Spring forum.
We're giving away four copies of REST with Spring (video course) and have Eugen Paraschiv on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static variable in static function Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variable in static function" Watch "static variable in static function" New topic

static variable in static function

Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
why can't i put a static variable inside the main method?

if you don't know, then ask. if you do know, then share. love is knowledge.
Marilyn de Queiroz

Joined: Jul 22, 2000
Posts: 9058
You cannot use the static modifier inside a method.
Not static variables, not static (local) inner classes, no static anything inside a method.

"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Thomas Whalen
Ranch Hand

Joined: Aug 26, 2001
Posts: 123
why is this? is it because of the method's life span? what i mean is if the method is destroyed after use, but a variable local to it is declared as static then the compiler throws an error? i guess i am not getting why this matters with methods, even though i understand it from a scope point of view.
thank you marilyn.
Peter Simard
Ranch Hand

Joined: Oct 31, 2001
Posts: 54
Anything "static" is initialized first by the compiler. Static members are available a) class wide and B) before instantiation of an object derived from the class
Methods are inherently temporary and local in scope. Temporary because variables used in methods are destroyed as soon as the method has done it's work. Anything "static" can also be called "un-changing and available classwide".
2b | | !2b

PAS<br /><br />2b || !2b
I agree. Here's the link:
subject: static variable in static function
It's not a secret anymore!