• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Declaring static variable only in static method

 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

In one of the site i read this statement :

you can declare static variable only in static method .

Except a class and static methods , no other can contain static data .


But when i tried this , its not compiling (Trying to declare a static variable in a static method)


Not Compiles

Please tell me is the statement wrong or i am i missing something?
 
Paul Clapham
Sheriff
Pie
Posts: 20739
30
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's wrong. You can only declare static variables at the top level of a class. Variables you declare inside methods are called local variables and can't be static.

Either you misinterpreted what you read on that site or it's a very bad source of Java information.
 
PavanPL KalyanK
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much paul .
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic