Static variables, methods, classes, and blocks in Java
Static is a keyword in Java that can be applied to variables, methods, classes, and blocks. This post provides an overview of the behavior of variables, methods, classes, and blocks of code in Java when the static keyword is applied to them.
1. Static variables
The static variables are also known as class variables. When static keyword is applied to a variable, it means that the variable belongs to the class and not an instance of the class. The static variables has the following characteristics:
- There is only one copy of the static variable in memory, which is shared by all instances of the class.
- Static variables are initialized before any of the instance variables.
- Static variables can be used when some value needs to be shared across all class objects. To ensure the value never gets changed after its initialization, declare the static variable as final.
- A static variable can be accessed directly using the class name without instantiating the class in which they are defined. Use the notation
ClassName.staticVariableNameto access the static variable. Within the same class, they can be accessed directly. - The standard output stream
System.outis an excellent example of static variable in Java. Hereoutis declared static in theSystemclass.
Consider the following code which has a static variable i defined in class A. It can be directly accessed by the name i inside the class A and with A.i outside the class A.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class A { public static int i = 100; @Override public String toString() { return Integer.toString(i); } } class Main { public static void main(String[] args) { System.out.println(A.i); // 100 } } |
2. Static methods
When a static keyword is applied to a method, the method can be accessed without creating an instance of the class. In other words, only a single copy of the static method is shared between all instances of a class. The static methods has the following characteristics:
- Static methods cannot access non-static variables or methods without having an instance of its class.
- A static method cannot be overridden in Java since overriding require late binding and static methods are bonded by compiler (early binding).
- A static method cannot refer to
thisandsuperkeywords since they are associated with a particular instance. - A static method can be called directly inside the class they are declared in and outside the class using the notation
ClassName.staticMethodName(). - The main method is an excellent example of static method in Java. The
main()method is declared static as the JVM (Java Virtual Machine) calls it before any objects are created. Since it is static, it can be directly invoked by the application using the class name. - A static method should be used when the method doesn’t depend on the individual characteristics of the class. Most of the utility and factory methods are usually static in Java.
Consider the following code having a static method f() defined in class A. It can be directly accessed by name f() inside the class A and with A.f() outside the class A without need for any instance.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
class A { public static void f() { System.out.println("Inside static method"); } public void fun() { f(); } } class Main { public static void main(String[] args) { A.f(); // Inside static method new A().fun(); // Inside static method } } |
3. Static class
Java provides support for the static class. But the outermost class cannot be static. A class can be static only if it is a nested class. The static class has the following characteristics:
- A static nested class cannot access the non-static variables and methods of the outer class. It can only access the static members of the outer class.
- A static nested class can have methods and variables of its own. They can even share the same name as members of the outer class.
- A static nested class doesn’t need reference of the outer class.
Consider the following code, which demonstrates the working of nested static class in Java:
|
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 28 29 |
class Main { public static String str = "A static field of the outer class"; public static void f() { System.out.println("Inside static method of the outer class"); } static class InnerClass { public void fun1() { System.out.println("Inside non-static method of the inner class"); } public static void fun2() { f(); System.out.println(str); } } public static void main(String[] args) { InnerClass obj = new InnerClass(); obj.fun1(); InnerClass.fun2(); } } |
Output:
Inside non-static method of the inner class
Inside static method of the outer class
A static field of the outer class
4. Static blocks
A static block is a block of code enclosed in curly braces, preceded by the static keyword, and can appear anywhere within the class body. They are also known as Static initialization blocks. The static blocks has the following characteristics:
- Static blocks are used to initialize static variables.
- Static blocks are executed when the class is loaded in the memory by the JVM.
- The code inside a static block is executed only once before execution of the
main()method. - A class can have multiple static blocks, and they will execute in the same sequence as they appear in the class source code.
- JVM usually combines all static blocks into single static block and then executes them.
Consider the following Java program, which has multiple static blocks. They will be executed in the same order from top to bottom in which they are defined before the main() method.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
class Main { public static final String CONST_STR; static { CONST_STR = "This value never changes"; System.out.println("Inside first static block"); } public static void fun() { System.out.println("Inside static method"); System.out.println(CONST_STR); } static { fun(); System.out.println("Inside second static block"); } public static void main(String[] args) { System.out.println("main() method is executed last"); } } |
Output:
Inside first static block
Inside static method
This value never changes
Inside second static block
main() method is executed last
That’s all about static variables, static methods, static classes, and static blocks in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)