If a variable is declared as public static varName;
, then I can access it from anywhere as ClassName.varName
. I am also aware that static members a
private static variable will be shared in subclass as well. If you changed in one subclass and the other subclass will get the changed value, in which case, it may not what you expect.
public class PrivateStatic {
private static int var = 10;
public void setVar(int newVal) {
var = newVal;
}
public int getVar() {
return var;
}
public static void main(String... args) {
PrivateStatic p1 = new Sub1();
System.out.println(PrivateStatic.var);
p1.setVar(200);
PrivateStatic p2 = new Sub2();
System.out.println(p2.getVar());
}
}
class Sub1 extends PrivateStatic {
}
class Sub2 extends PrivateStatic {
}
Another perspective :
If a variable is defined as public static it can be accessed via its class name from any class.
Usually functions are defined as public static which can be accessed just by calling the implementing class name.
A very good example of it is the sleep()
method in Thread class
Thread.sleep(2500);
If a variable is defined as private static it can be accessed only within that class so no class name is needed or you can still use the class name (upto you). The difference between private var_name and private static var_name is that private static variables can be accessed only by static methods of the class while private variables can be accessed by any method of that class(except static methods)
A very good example of it is while defining database connections or constants which require declaring variable as private static .
Another common example is
private static int numberOfCars=10;
public static int returnNumber(){
return numberOfCars;
}
Is declaring a variable as
private static varName;
any different from declaring a variableprivate varName;
?
Yes, both are different. And the first one is called class variable
because it holds single value for that class
whereas the other one is called instance variable
because it can hold different value for different instances(Objects)
. The first one is created only once in jvm and other one is created once per instance i.e if you have 10 instances then you will have 10 different private varName;
in jvm.
Does declaring the variable as
static
give it other special properties?
Yes, static variables gets some different properties than normal instance variables. I've mentioned few already and let's see some here: class variables
(instance variables which are declared as static) can be accessed directly by using class name like ClassName.varName
. And any object of that class can access and modify its value unlike instance variables are accessed by only its respective objects. Class variables can be used in static methods.
What is the use of a
private static variable
in Java?
Logically, private static variable
is no different from public static variable
rather the first one gives you more control. IMO, you can literally replace public static variable
by private static variable
with help of public static
getter and setter methods.
One widely used area of private static variable
is in implementation of simple Singleton
pattern where you will have only single instance of that class in whole world. Here static
identifier plays crucial role to make that single instance is accessible by outside world(Of course public static getter method also plays main role).
public class Singleton {
private static Singleton singletonInstance = new Singleton();
private Singleton(){}
public static Singleton getInstance(){
return Singleton.singletonInstance;
}
}
Well, private static
variables can be used to share data across instances of that class. While you are correct that we cannot access the private static
variables using constructs like ClassName.member
or ClassInstance.member
but the member will always be visible from methods of that class or instances of that class. So in effect instances of that class will always be able to refer to member.
For some people this makes more sense if they see it in a couple different languages so I wrote an example in Java, and PHP on my page where I explain some of these modifiers. You might be thinking about this incorrectly.
You should look at my examples if it doesn't make sense below. Go here http://www.siteconsortium.com/h/D0000D.php
The bottom line though is that it is pretty much exactly what it says it is. It's a static member variable that is private. For example if you wanted to create a Singleton object why would you want to make the SingletonExample.instance variable public. If you did a person who was using the class could easily overwrite the value.
That's all it is.
public class SingletonExample { private static SingletonExample instance = null; private static int value = 0; private SingletonExample() { ++this.value; } public static SingletonExample getInstance() { if(instance!=null) return instance; synchronized(SingletonExample.class) { instance = new SingletonExample(); return instance; } } public void printValue() { System.out.print( this.value ); } public static void main(String [] args) { SingletonExample instance = getInstance(); instance.printValue(); instance = getInstance(); instance.printValue(); } }