wood burning stoves 2.0*
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static variable in static function" Watch "static variable in static function" New topic
Author

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
Sheriff

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


JavaBeginnersFaq
"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".
HTH
Pete
------------------
PAS
peter@panvox.net
2b | | !2b


PAS<br />peter@panvox.net<br />2b || !2b
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static variable in static function
 
Similar Threads
Static variable
about protected n static
Fix an infity loop
Best Practice for getters and setters
Dont declare global variables in Servlet