The static in java is not associated with any instance of class. It indicates that anything which is declared as static will be initialized before any instances of class and such things will have only one copy containing values and those values will be used by all the instances of class whenever these classes (specifically instances of class) need them. In other words we can say that static variables belongs to class as whole and not to particular instance of class.
Here in this section we will see how to declare static variables and how to use them in different scenarios.
Static variables can declared inside the class. And if you want to use that static variable in same class then you can directly use it, as shown in following example.
public class StaticVariable{
static String name="I am String variable and I am declared as Static";
public static void main(String[] args){
System.out.println(name);
//Static variable declared and used in same class.
Ping p=new Ping();
Pong po=new Pong();
}
}
But if you want to use the static variable declared in another class then you have to use the dot operator along with name of class and name of static variable. In following example I am accessing the static variable declared in StaticVariable class in Ping class. For this I have to use the syntax(StaticVarible.name). You can consider following as general syntax while accessing static variables declared in another class:
{nameofClassWhereSatticVariable_is_declared.nameof_StaticVariables}
class Ping{
System.out.println(name);
Trying to access the static variables declared in another class without using the name of class in which it is declared.
Here we are accessing the static variables in ping class which is declared in StaticVariable class.We are using the constructor of Ping class just to show that we have accessed the static variable from another class by printing it on console.Remember in main() method we just created the instance of ping class and by using simply constructor we can print value variable without using methods and calling them. Constructor for ping class here is used only for simplicity. You can access the static variables of the one class in another class using the same syntax for static variable unless it is declared as 'final'.
public Ping(){
System.out.println(StaticVariable.name);
}
}
If you have static variable declared in parent class and if you want to use that static variable in child class then you can directly use that static variable. Because when a class extends another class (i.e. when a class inherits another class) all the members of parent class can be used by child class as if they are declared by child class only;except the variables in parent class which are declared as 'private'.
class Pong extends StaticVariable{
Again we will use the constructor of Pong class to show that static variable name is accessed directly. We can access the static variable in methods or blocks of code just we are accessing it in constructors;as follows:
public Pong(){ System.out.println(name);
}
}
The static variables are also called as class variables as they are attached to whole class rather than any instance of class.
0 comments:
Post a Comment