| Author |
Declaring static variable.
|
jose chiramal
Ranch Hand
Joined: Feb 12, 2010
Posts: 266
|
|
Iam able to declare & initialise local variable j however when i try to mention a static variable it gives me error. It doesnt give me any error outside the main. Why so ? Since main is mentioned inside a class shouldnt the static variable get associated with the class having the main method ??
Please assist in understanding this.
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24057
|
|
You can't declare static variables inside a method, in main or in any other method. "static" makes sense only for member variables, and variables declared inside a method are local variables, not members.
Java's "static" is unlike the same keyword in "C" in this way -- in C, you can use "static" inside a function -- and so perhaps that's why you're expecting it to work in Java. But it doesn't.
|
[Jess in Action][AskingGoodQuestions]
|
 |
jose chiramal
Ranch Hand
Joined: Feb 12, 2010
Posts: 266
|
|
|
Thanks a lot Ernest Friedman-Hill in helping me understand is.
|
 |
 |
|
|
subject: Declaring static variable.
|
|
|