• Post Reply Bookmark Topic Watch Topic
  • New Topic

non-static access of static variables and methods  RSS feed

 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was surprised that this compiles without errors:



Why am I allowed to access static methods and variables in a non-static way (via an object), as I thought the purpose of 'static' keyword is to serve as class variables / methods (object independant) ?
Is there a practical application for this ?

Thanks.
 
Knute Snortum
Sheriff
Posts: 4274
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An object can access a a static variable. If you create three objects of that class, you can access a static variable from all of them, but it will be the same variable.
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Syntactically, it's allowed but not recommended. There's really no difference except for the sake of clarity you should access static members of a class via a reference to the class itself, not via an instance.

As for the practical application of accessing a static member from a non-static context, there are valid reasons to do so, the most common of which is to access information that is common to all instances of that class. Constants are also usually declared as static.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Talking about static variable,
They can be accessed directly within any method of that class

By using classname.(dot) variable, they can be accessed within any method of that class or outside of it.

You can even access it by reference variable of the instance of that class (but not recommended)

It is associated with class not with any particular object,as every instance of that class share them
so it should be accessed by class name


 
Dennis Schroeder
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the replies.

edit:
I knew that the common way of access is without an object, so I was just a little surprised initially that access via object also works.

Understood and marked as solved.






 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:. . .
By using classname.(dot) variable, they can be accessed within any method of that class or outside of it.
. . .
Since variables should be private that may not work.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yup,you got me again,
I should have mentioned, my statement is perfectly valid if variable has public access modifier,rest for others it is subjected to change.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe the language should have forbidden access to static members via an object reference, but it doesn't. If you use an IDE like Eclipse, however, it will give you a warning about static access.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!