Let’s learn static keyword in java.
Static keyword in java
A member declared with keyword static belongs to class rather than instance of a class. static keyword can be used with class level variables, code block, methods and inner class. static keyword is a non-access modifier.

Static class in java
To declare a class as static, that class should be an inner class. static inner class is a nested class which is static member of outer class.
Static inner class can be accessed without using object of outer class. Now let’s see static class in java with example.
public class OuterClass { // static class static class NestedClass { public void show() { System.out.println("in static class"); } } public static void main(String args[]) { OuterClass.NestedClass obj = new OuterClass.NestedClass(); obj.show(); } }
Output:
in static class
Static variable
To declare static variable in java, use keyword “static”.
Syntax:
static datatype variableName;
Static variables belongs to the class, not an object. Each instance of a class shares same static variable. If we make changes to a static variable all other instances will see effect of that change.
Let’s see static variable in java with example.
class Demo { static int b = 6; } class Main { public static void main(String[] args) { System.out.println(Demo.b); } }
Output:
6
NOTE:
- Static variables is used for memory management.
- Only class level variables can be “static” variables.
- Static variable belongs to class not object. It can be accessed using class name.
- Static variable is a class level variable and it is initialized when class is loaded.
- Default values for non-static varaible and static variable are same. (int, short long: 0, float and double: 0.0, boolean: false, object reference: null).
Static method
We can declare a method as static by adding keyword “static” modifier before method name. Static method belongs to class, not an object.
Syntax:
ClassName.methodName();
Static methods can be called directly by class name (as shown above). Inside static methods we cannot use this keyword. For example “main” is a static method, it is called by the JVM when it starts an application.
Let’s see example of static method in java.
class Demo { static void show() { System.out.println("Hello World"); } public static void main(String[] args) { Demo.show(); } }
Output:
Hello World
NOTE:
- Super and this keyword cannot be used in static.
- “Static” method can access only static data.
- “Static” methods can call only static methods.
- Like static variables, static methods are used for memory management.
Static keyword in java: static block in java
Static block is used to initialize “static” data member of a class. Static block is executed before “main()” method.
Static block executes automatically when the class is loaded in memory. Here’s an example.
class StaticBlockExample { static { System.out.println("first static block"); } public static void main(String[] args) { System.out.println("In main method"); } static { System.out.println("second static block"); } }
Output:
first static block
second static block
In main method
Use of static block in java
- Static block is executed at the time of class loading. So at the time of class loading if we want to perform activity, define in static block. For example, native methods.
- Static block is used to initialize static members.
Static keyword in java: Why is main method static in java?
“main” method is static because when we want to run a java program there has to be an entry point, in other words a method that is executed when the program runs.
Until the program runs there’s no classed instances to call methods on, so java has to use this static method that can be called from the class name rather than from a class instance.
So that’s a reason why the main methods is static.