Static keyword can be used with
- Variables
- Methods
- Blocks
- Static inner class
lets see them one by one
Static Variables in Java
Static variable can be create by adding static keyword in front of variable.
below count is static variable.
A static variable is shared among all objects of class.
Each class have their own instance variable but they all share static variables.
Static variable is also know as class variable because it belongs to class not specific to any object.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | class Test { int a, b; //instance variable static int count; //class variable Test(int a, int b) { this.a = a; //a & b are local variables this.b = b; count++; } public void show() { System.out.println("a=" + a + " b=" + b + " count=" + count); } } public class StaticExample1 { public static void main(String[] s) { Test t = new Test(4, 5); t.show(); Test t1 = new Test(6, 4); t1.show(); } } |
In diagram memory allocation for different variables are shown.
It also represents static variables are stored in MetaSpace( Java8 Onward ) from where all objects are sharing it.
Why Static variable required?
A static variable is shared amount all objects of class. So to share information amount objects static variable can used.
2. Static Methods in Java
To create a method static add static keyword before to method.
<access-specifier> static <return-type> <method-name>{
statements;
}
A static method can access only static fields and member functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class Test1 { int a, b; static int count; Test1(int a, int b) { this.a = a; this.b = b; count++; } public void show() { System.out.println("a=" + a + " b=" + b); } public static void test() { System.out.println("count is " + count); } } public class StaticExample2 { public static void main(String[] s) { Test1 t = new Test1(4, 5); t.show(); Test1.test(); Test1 t1 = new Test1(6, 4); t1.show(); Test1.test(); } } |
It can not access not static fields and methods.
Why Static method can not access not static variables?
Static variable initialize when a class in loaded in JVM
Instance variable is initialized when object is created.
At the time when static method called Instance variables they are not created. Thats why static method can not access instance variable.
3. Static Block in Java
A block is set of statements inside opening closing brackets {….}.
A static block is a block which contains static in front.
1 2 3 4 5 6 7 8 | public class StaticExample3{ static{ System.out.println("Inside static"); } public static void main(String[] s){ System.out.println("Inside main"); } } |
Why Static block is needed?
A static block can be used to initialize some values that a program required during load time or in throughout the program.
4. Static Inner classes in Java
A class inside a class in called nested class java supports both static and non static nested classes.
A static nested class is nested class with preceded static keyword.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | class Outer{ void show(){ System.out.println("Inside show() in Outer class"); } public static class StaticInner{ void display(){ System.out.println("Inside display() in Inner class"); } } } public class TestMain{ public static void main(String[] s){ Outer outer=new Outer(); outer.show(); Outer.StaticInner staticInner=new Outer.StaticInner(); staticInner.display(); } } |
Output
1 2 | Inside show() in Outer class Inside display() in Inner class |