jQuery in Action, 2nd edition*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static variables in Static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static variables in Static method" Watch "Static variables in Static method" New topic
Author

Static variables in Static method

J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
I just tried the exumalator's question of the day, and I have a question:




1. char h = 1; //on the line before the System.out.println statement.
2. static char h = 1; //On the line before the System.out.println statement
3. char h = 1;//On the line before the main method
4. static char h = 1;//On the line before the main method


The answer is listed as 1 and 4, which is fine, but I also think 2 is also an option. Can't you put static variables inside a static method?
Peter MacMillan
Ranch Hand

Joined: Jun 23, 2006
Posts: 34
Originally posted by J Brewer:
Can't you put static variables inside a static method?


No. Only the final modifier is allowed.
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
No, static is a modifier you use on class variables and methods.
J Brewer
Ranch Hand

Joined: Jul 10, 2006
Posts: 46
Well, that's what I thought at first. But then I saw one source that said 'you can have static variables inside a static method', so I wasn't sure.

Thanks!
Sujittt Tripathyrr
Ranch Hand

Joined: Jun 21, 2006
Posts: 96
Hi

Here I have one doubt that in general method we can put static variable but in static method why not.
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
There is only one modifier can be used by local variables that is final modifier.
Suhas Wadadekar
Ranch Hand

Joined: May 16, 2006
Posts: 95
i agree with Owen.....we cannot have any other variable modifier inside a method. As listed in the rules governing staic declarations in K & B book, I think that anything that is method-local cannot be static.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Static variables in Static method
 
Similar Threads
Doubt in Marcus mock exam 3
Basic Java - Please help a stressed noob with a method
java
isnt it ridiculous?