Many of us still get confused with the keyword static. Now, we will discuss how static keyword is used? and how it helps to save lots of code from rewriting?
Look at the following code
public class staticDemo{
public static void main(String args[]){
System.out.println("Hello world!"); //prints "Hello world"
}
}
"Hello world" is the basic programme right ?.It is how someone starts learning java. Do you ever noticed that the main method has a keyword static
?. What this static does?.
By using static
keyword in main()
method , we can call the main()
method without any instance of object i.e., We can call the static
methods using class name.
Let's have another example -
class A{
static void show(){
System.out.println("static method in class A");
}
}
public class staticDemo{
public static void main(String args[]){
A.show() // prints "static method in class A"
}
}
static variable in Java
static
variables are the variables which are applicable to the all instances of the class declared it. When a variable is static
its value is same for the all instances. If any one of the instance changes the static variable value it reflects to all other instances too.
consider the following code -
class A{
static int i;
}
public class staticDemo{
public static void main(String args[]){
A obj1= new A();
A obj2 =new A();
A obj3 =new A();
obj3.i=10;
System.out.println(obj1.i); // 10
}
}
NOTE - static
variables can be accessed in non-static methods. But non-static variables can't be accessed in static
methods. Why that is so?, Since non-static variables need for an instance to call the variables and we can assign different values for non-static variables by creating multiple objects. So there will be an ambiguity for the compiler to understand for which value instance value I should access.
static block in java -
static
block is used for initializing the static variables. static
block is executed only when the class is loaded into the memory i.e., only once.
class A{
static int i;
static{
i=9;
System.out.println("static block in A");
}
}
public class staticDemo{
static{
System.out.println("static block in main");
}
public static void main(String args[]){
System.out.println(A.i);
System.out.println("static in main");
}
}
Always static blocks are executed first. static block gets executed and then main()
method gets executed. In main()
method we have A.i
here A class gets loaded into the memory and when memory is loaded static block gets executed and then the value of i
gets printed, at last "static in main" gets printed.
// output of above code
static block in main
static block in A
9
static in main
Summary
- variables and methods with the
static
keyword do not need an instance to access it. They can be accessed using class name. static
blocks are executed before constructors and methods.- If there are multiple
static
blocks, they are executed as per the sequence in the code. - non-static variables can't be accessed in
static
methods.
Thanks for the read!. I hope this has taught you something about static
. If you feel something wrong please let me know :)
Let's get connect on Twitter.
Happy coding :)