aspose file tools
The moose likes Java in General and the fly likes Declaring static variable. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Declaring static variable." Watch "Declaring static variable." New topic
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
    
  13

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Declaring static variable.
 
Similar Threads
LIS Question
incompatible types...
weird
compilation fails why?
when does a variable is initialized ?