What Is the Static Keyword in Java?

Static is a Non Access Modifier. It means that something (a field, method, block or nested class) is related to the type rather than any particular instance of the type. The Static keyword can be applied to:
  1. Static Method
  2. Static Variable
  3. Initialization Block
  4. Nested class

Java Static Method

In Java, static methods can be called without creating an object of the class. In normal case we call a method from the instance of that class. Example
public class TestClass { public static void main(String[] args) { TestClass tc = new TestClass();//create a class instance tc.ShowMessage(); //call method using instance } void ShowMessage() { System.out.println("Here is the message"); } }
When we create a static method inside a class, methods can be called without creating an object of class. Example
public class TestClass { public static void main(String[] args) { ShowMessage(); //calling without creting instance } static void ShowMessage() { System.out.println("Here is the message using static"); } }
In Java, a static method can access only static variables of class and invoke only static methods of the class. Usually static methods are utility methods that we want to expose to be used by other classes without the need of creating an instance . Class methods are often used to provide global functions for Java programs. For example, Methods in the java.lang.Math package are Class methods. Since static methods also do not belong to a specific instance, they can't refer to instance members .The static members can only refer to static members.

Features of static method

  1. A static method is a method which belongs to the class and not to the instance(object)
  2. A static method can be invoked without the need for creating an instance of a class
  3. A static method can call only other static methods and cannot call a non-static method from it
  4. A static method can access static data member and can change the value of it
  5. A static method cannot refer to this or super keywords in anyway

Java Static variable

A static variable is one that's associated with a class, not instance (object) of that class. They are initialized only once , at the start of the execution . A single copy to be shared by all instances of the class and it can be accessed directly by the class name and doesn't need any object. One common use of static is to create a constant value that's attached to a class.

For example, if you have a variable: private static int stc = 0; and you increment it (stc++) in one instance, the change will be reflected in all instances. stc will now be 1 in all instances.

Initialization Block

The static initializer is a static {} block of code inside java class, and run only one time before the constructor or main method is called. The code block with the static modifier signifies a class initializer; without the static modifier the code block is an instance initializer.
public class TestClass { static { System.out.println("Hello before main"); } }
It's automatically invoked when the class is loaded. A static block executes once in the life cycle of any program, and there's no other way to invoke it From Java 7 , however, this does not work anymore, even though it compiles, the following error will appear when you try to execute it:
Main class should contain method: public static void main (String[] args).

Nested class

Nested classes in Java are divided into two categories: static and non-static . Nested classes that are declared static are simply called static nested classes. Example
public class OuterClass { public static class StaticNestedClass { } public class NonStaticNestedClass { } }

In order to create an object for the static nested class, use the following syntax:

OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();
The key difference between them, you can't create an NonStaticNestedClass instance without an OuterClass whereas you can create an StaticNestedClass object independently.

Difference between Static Nested Class and Nonstatic Nested class

A Static Nested Class can't access enclosing class instance and invoke methods on it, so should be used when the nested class doesn't require access to an instance of the enclosing class . A common use of static nested class is to implement a components of the outer object.

A Nonstatic Nested class is implicitly associated with the enclosing instance of the containing class, this means that it is possible to invoke methods and access variables of the enclosing instance. One common use of a nonstatic nested class is to define an Adapter class.