The static keyword is used in java mainly for memory management. It is used with variables, methods, blocks and nested class. It is a keyword that are used for share the same variable or method of a given class.
No object needs to be created to use static variable or call static methods, just put the class name before the static variable or method to use them. Static method can not call non-static method.
Static variable
If any variable we declared as static is known as static variable. Static variable is used for fulfill the common requirement. The static variable allocate memory only once in class area at the time of class loading. Using static variable we make our program memory efficient (i.e it saves memory).
In the below example count variable is commonly sharable by both S1 and S2 objects. Static data variable are store in method area and non static variable is store in java heap.
We can create static variables at class-level only. Static block and static variables are executed in order they are present in a program.
Static blocks
If you need to do computation in order to initialize your static variables, you can declare a static block that gets executed exactly once, when the class is first loaded. It is executed before the main method at the time of classloading. Also, static blocks are executed before constructors.
Consider the following java program demonstrating use of static blocks.
// Java program to demonstrate use of static blocks class Test { // static variable static int a = 10; static int b; // static block static { System.out.println("Static block initialized."); b = a * 4; } public static void main(String[] args) { System.out.println("from main"); } }
Static methods
When a method is declared with static keyword, it is known as static method. The most common example of a static method is main( ) method. As discussed above, Any static method can be accessed before any objects of its class are created, and without reference to any object. Methods declared as static have several restrictions:
- They can only directly call other static methods.
- They can only directly access static data.
- They cannot refer to this or super in any way.
- A static method belongs to the class rather than the object of a class.
// Java program to demonstrate restriction on static methods class Test { // static variable static int a = 10; // instance variable int b = 20; // static method static void m1() { a = 20; // Cannot make a static reference to the non-static field b b = 10; // compilation error // Cannot make a static reference to the non-static method m2() from the type Test m2(); // compilation error // Cannot use super in a static context System.out.println(super.a); // compiler error } // instance method void m2() { } }
Static Class
Java allows us to define a class within another class. Such a class is called a nested class. The class which enclosed nested class is known as Outer class. In java, we can’t make Top level class static. Only nested classes can be static. Differences between static and non-static (Inner Class) nested classes
- Nested static class doesn’t need reference of Outer class, but Non-static nested class or Inner class requires Outer class reference.
- Inner class(or non-static nested class) can access both static and non-static members of Outer class. A static class cannot access non-static members of the Outer class. It can access only static members of Outer class.
- An instance of Inner class cannot be created without an instance of outer class and an Inner class can reference data and methods defined in Outer class in which it nests, so we don’t need to pass reference of an object to the constructor of the Inner class. For this reason Inner classes can make program simple and concise.
/* Java program to demonstrate how to implement static and non-static classes in a java program. */ class OuterClass{ private static String msg = "OuterClass"; // Static nested class public static class NestedStaticClass{ // Only static members of Outer class is directly accessible in nested static class public void printMessage() { System.out.println("Message from nested static class: " + msg); } } // Non-static nested class public class InnerClass{ // Both static and non-static members of Outer class are accessible in this Inner class public void display(){ System.out.println("Message from non-static nested class: "+ msg); } } } class Main { public static void main(String args[]){ // Create instance of nested Static class OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass(); // Call non static method of nested static class printer.printMessage(); // For creating instance of Inner class we need an Outer class instance. OuterClass outer = new OuterClass(); OuterClass.InnerClass inner = outer.new InnerClass(); // Calling non-static method of Inner class inner.display(); } }
What good are static classes? A good use of a static class is in defining one-off, utility and/or library classes where instantiation would not make sense. A great example is the Math class that contains some mathematical constants such as PI and E and simply provides mathematical calculations. Requiring instantiation in such a case would be unnecessary and confusing. Notice that it is final and all of its members are static.