Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Private Static

 
Raveendra Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
This is my first post on these forums.
I have a basic doubt and hope somebody here might clear it.
What is the use of a private static class level variable?


regards
Ravi
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Do you know what "private" and "static" mean?

"private" is an access specifier. It tells you that the member is only visible inside the class - other classes can't access the private members of a class.

"static" means that the variable is a class-level variable; there's only one variable, which is shared by all instances of the class. See Understanding Instance and Class Members for more info.

So, if you combine these for a member variable, you get a member variable that's only visible inside the class, and of which there's only 1 copy, shared by all instances of the class.

Private static member variables are useful for a number of constructions, for example to implement the Singleton design pattern in Java. Here's an example:
 
Raveendra Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your reply.
Say if i have a setter method like as follows:



Is the above assign correct way?
If so, i am getting a warning saying that,

Field instance should be accessed in a static way


Its correct waring because, as its static variable is should be accessed using Classname. So if i change it with class name like
, am i referring to current class instance or new one? I am confused here.
Please suggest me.
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15203
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all, if you are trying to implement the singleton design pattern, you would not want a public static setInstance method in the class. The idea of the MySingletonClass I gave you is that the class itself controls the creation of instances.

About the warning message the compiler gives you ("Field instance should be accessed in a static way"): Yes, you get the warning because you are accessing a static variable via an instance (the "this" reference), instead of the class name. It's allowed to access static members via an instance in Java, but it's bad style and confusing - because you're changing the value of the static variable for all instances, not just for the one you're using to access the variable.

If you change the line of code to "MySingletonClass.instance = instance", you will not get the warning anymore.

MySingletonClass.instance refers to exactly the same variable as this.instance - there is only one member variable called "instance" for the class.

You see now why it's confusing to refer to a static variable via an object reference (the "this" reference in your case) - you easily get confused.
 
Raveendra Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
Your explanation is really very good.
Now i have understood about private static and when & how it has to be used.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Jesper de Jong:
It's allowed to access static members via an instance in Java, but it's bad style and confusing - because you're changing the value of the static variable for all instances, not just for the one you're using to access the variable.


Not only that - for an instance variable that is hidden by an instance variable of the same name in a subclass, which of them is used in the code is actually not determined based on the runtime type of the object, but on the compile time type of the reference.
 
Raveendra Kumar
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry i didnt understand your post.
Could you please be more clear if possible with a small code snippet.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic