What is the use of a private static variable in Java?
Question
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 are shared by all instances of a class and are not reallocated in each instance.
Is declaring a variable as
private static varName; any different from declaring a variable
private varName;?
In both cases it cannot be accessed as
ClassName.varName or as
ClassInstance.varName from any other class.
Does declaring the variable as static give it other special properties?
Accepted Answer
Of course it can be accessed as
ClassName.var_name, but only from inside the class in which it is defined - that's because it is defined.
Popular Answer
Static variables have a single value for all instances of a class.
If you were to make something like:
public class Person { private static int numberOfEyes; private String name; }
and then you wanted to change your name, that is fine, my name stays the same. If, however you wanted to change it so that you had 17 eyes then everyone in the world would also have 17 eyes.
Read more... Read less...
Private static variables are useful in the same way that private instance variables are useful: they store state which is accessed only by code within the same class. The accessibility (private/public/etc) and the instance/static nature of the variable are entirely orthogonal concepts.
I would avoid thinking of static variables as being shared between "all instances" of the class - that suggests there has to be at least one instance for the state to be present. No - a static variable is associated with the type itself instead of any instances of the type.
So any time you want some state which is associated with the type rather than any particular instance, and you want to keep that state private (perhaps allowing controlled access via properties, for example) it makes sense to have a private static variable.
As an aside, I would strongly recommend that the only type of variables which you make public (or even non-private) are constants - static final variables of immutable types. Everything else should be private for the sake of separating API and implementation (amongst other things).
Well you are right public static variables are used without making an instance of the class but private static variables are not. The main difference between them and where I use the private static variables is when you need to use a variable in a static function. For the static functions you can only use static variables, so you make them private to not access them from other classes. That is the only case I use private static for.
Here is an example:
Class test { public static String name = "AA"; private static String age; public static void setAge(String yourAge) { //here if the age variable is not static you will get an error that you cannot access non static variables from static procedures so you have to make it static and private to not be accessed from other classes age = yourAge; } }
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.
Is declaring a variable as
private static varName;any different from declaring a variable
private 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
staticgive 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 variablein 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; } }
What is the use of a private static class variable?
Let's say you have a library book Class. Each time you create a new Book, you want to assign it a unique id. One way is to simply start at 0 and increment the id number. But, how do all the other books know the last created id number? Simple, save it as a static variable. Do patrons need to know that the actual internal id number is for each book? No. That information is private.
public class Book { private static int numBooks = 0; private int id; public String name; Book(String name) { id = numBooks++; this.name = name; } }
This is a contrived example, but I'm sure you can easily think of cases where you'd want all class instances to have access to common information that should be kept private from everyone else. Or even if you can't, it is good programming practice to make things as private as possible. What if you accidentally made that numBooks field public, even though Book users were not supposed to do anything with it. Then someone could change the number of Books without creating a new Book.
Very sneaky!