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
*)If a variable is declared as private then it is not visible outside of the class.this is called as datahiding.
*)If a variable is declared as static then the value of the variable is same for all the instances and we no need to create an object to call that variable.we can call that variable by simply
classname.variablename;
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 as private
.
public static
or private static
variables are often used for constants. For example, many people don't like to "hard-code" constants in their code; they like to make a public static
or private static
variable with a meaningful name and use that in their code, which should make the code more readable. (You should also make such constants final
).
For example:
public class Example {
private final static String JDBC_URL = "jdbc:mysql://localhost/shopdb";
private final static String JDBC_USERNAME = "username";
private final static String JDBC_PASSWORD = "password";
public static void main(String[] args) {
Connection conn = DriverManager.getConnection(JDBC_URL,
JDBC_USERNAME, JDBC_PASSWORD);
// ...
}
}
Whether you make it public
or private
depends on whether you want the variables to be visible outside the class or not.
If you use private static variables in your class, Static Inner classes in your class can reach your variables. This is perfectly good for context security.
In the following example, eye
is changed by PersonB, while leg
stays the same. This is because a private variable makes a copy of itself to the method, so that its original value stays the same; while a private static value only has one copy for all the methods to share, so editing its value will change its original value.
public class test {
private static int eye=2;
private int leg=3;
public test (int eyes, int legs){
eye = eyes;
leg=leg;
}
public test (){
}
public void print(){
System.out.println(eye);
System.out.println(leg);
}
public static void main(String[] args){
test PersonA = new test();
test PersonB = new test(14,8);
PersonA.print();
}
}
> 14 3
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!
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;
}
}