aspose file tools*
The moose likes Beginning Java and the fly likes Private Static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Private Static" Watch "Private Static" New topic
Author

Private Static

Raveendra Kumar
Greenhorn

Joined: Dec 20, 2005
Posts: 12
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

Joined: Aug 16, 2005
Posts: 14270
    
  21

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raveendra Kumar
Greenhorn

Joined: Dec 20, 2005
Posts: 12
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

Joined: Aug 16, 2005
Posts: 14270
    
  21

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

Joined: Dec 20, 2005
Posts: 12
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

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Raveendra Kumar
Greenhorn

Joined: Dec 20, 2005
Posts: 12
Sorry i didnt understand your post.
Could you please be more clear if possible with a small code snippet.
 
 
subject: Private Static