Java Static Keyword

Java Static Keyword

In this article, you’ll learn about static keyword, static variables, methods and block in Java with the help of examples.

What is a static keyword in Java?

Normally, if you want to access a class member in Java, you must first create an instance of the class. But, there will be cases when you have to define a class member that will be used independently without creating an instance of that class.

However, in Java, it is possible to create a class members (variables and methods) that can be accessed and invoked without creating an instance of the class. In order to to create such a member, you have to use static keyword while declaring a class member.

The best use case of static class members is in Math class of package java.lang. Almost all the class members (variables and methods) of the Math class are static. Hence, these class members can be accessed without creating an instance of Math class. Let’s look at the use of Math class and its class members:

Example 1: Using the static variables and methods from builtin Math class.

public class StaticMembers {

	public static void main( String[] args ) {

        System.out.println("Absolute value of -12 =  " + Math.abs(-12));
        System.out.println("Value of PI = " + Math.PI);
        System.out.println("Value of E = " + Math.E);
        System.out.println("2^2 = " + Math.pow(2,2));

When you run this program, the output will be:

Absolute value of -12 = 12
Value of PI = 3.141592653589793
Value of E = 2.718281828459045
2^2 = 4.0

Did you notice in above program, a new instance of Math class is not created every time its member function is called. Rather, method is invoked directly from the class itself. This is all possible because almost every class members are static in the Math class.

Static Methods

Static methods are also called class methods because it belongs to the class rather than object of a class. Which means that static methods can be invoked directly from the class name itself rather than creating an instance and invoking from the instance.

The syntax for invoking static method is:


As you might have noticed in every Java program, the main() method is always declared as static. Hence, in order for the JVM to allow invocation of the main() method or to run the program in the initial phase where no object exists in the memory, it must be declared as static.

Example 2: Using the static method and non static method.

public class StaticTest {

    int multiply(int a, int b){
        return a * b;

    static int add(int a, int b){
        return a + b;


public class StaticRunner{

   public static void main( String[] args ) {

        StaticTest st = new StaticTest();

        // calling a nonstatic member function
        System.out.println(" 2 * 2 = " + st.multiply(2,2));

        // calling a static member function
        System.out.println(" 2 + 3 = " + StaticTest.add(2,3));

When you run this program, the output will be:

2 * 2 = 4
2 + 3 = 5

In above program, multiply method is not a static method whereas add method is a static method. So, in the main method of the new public class StaticRunner we can directly invoke static method like this: StaticTest.add(). But, in order to invoke the non-static method you have to explicitly create the instance of StaticTest class.

Static Variables

Like static methods, static instance variables are actually, global variables unless they aren’t explicitly declared private. Also, when the instance are created for a class, seperate copy of static variables aren’t made each time but, all instances of the class share the same static variable. For example, the instance variable PI in the Math class of java.lang package is static. Hence, it can be referenced directly from class name itself.

The syntax to declare a static variable is by using the static keyword like this:

static int VAR1 = 123; // with initialization

static int VAR2;// without initialization

Example 3: Compute the area of a circle using Math class

We are going to compute area of the circle using the static variable PI and static method pow() from builtin Math class.

public class StaticCircleArea {

    public static void main( String[] args ) {

        double radius = 5.45;
        double area = Math.PI * Math.pow(radius,2);
        System.out.println("area of circle with r=5.45 is " + area);


When you run this program, the output will be:

area of circle with r=5.45 is 93.31315579325083

Here, PI is a static instance variable in Math class and it can be referenced directly via the class name.

Example 4: Use of the static keyword in a variable and not in a variable.

public class CustomStaticVariable {

    static int MAX = 10; // static variable
    int min = 5; // non static variable

    public static void main(String[] args) {

        CustomStaticVariable csv = new CustomStaticVariable();

        System.out.println("min+1 = " + (csv.min + 1));
        System.out.println("MAX+1 = " + (MAX + 1));

When you run the program, the output will be:

min+1 = 6
MAX+1 = 11

Here in above program, variable named MAX is declared as static whereas min is a non static variable. Now, when you look at the main method, you need to create the instance of the class to access the instance variable min, however, MAX variable can be accessed directly.

If static variables are not explicitly declared as private, it can be accessed by direct reference from other class as well.

Example 5: Access class variable directly via class name in java

public class UseStatic {
	public static void main( String[] args ) {

        System.out.println("CustomStaticVariable.MAX = " + CustomStaticVariable.MAX);


When you run the above program, the output will be:

CustomStaticVariable.MAX = 10

Note: It is a convention in Java that if you want to define a instance variable as static, the variable name should be in all capital letters. If there are multiple words, it needs to be separated by underscores.

Inner Class vs Static Nested Class

In Java you can define a class within another class. Such a class is called a nested class.

Nested class are of two types: static and non-static. Nested classes that are declared static are called static nested classes. Non-static nested classes are called inner classes. The nested classes can be declared like this:

class OuterClass {
    static class StaticNestedClass {
    class InnerClass {

Static Class

Non-Static Class

Static nested classes do not have access to other members of the enclosing class but it can access only the static members of the enclosing class.

Non-Static classes has access to other members of the outer or enclosing class, even if they are declared private.

Static nested class does not need an instance of the enclosing class in order to be instantiated itself.

Non-Static inner class needs the help from enclosing class to instantiate itself.

Example 6: Use nested class in java

public class NestedTest {

    private int x = 10;
    private static int y = 20;

    private class NonStatic {
        void display(){
            System.out.println("private int x = " + x);
    static class StaticNested {
        void display(){
            // You can't access non-static members here
            // System.out.println("private int x = " + x);

            // You can only access the static members
            System.out.println("private static int = " + y);
    public static void main(String[] args) {
        NestedTest nc = new NestedTest();
        // instantiation of inner non-static class
        NestedTest.NonStatic nonStatic = NonStatic();
        // You can directly instantiate nested static class
        StaticNested staticNested = new StaticNested();

When you run the program, the output will be:

private int x = 10

private static int = 20

You can read more about nested class from here.

Static Block

If your class has different static variables and they either needed to be initialized individually or by through some computation, then in this case static block is used. Static block gets executed exactly once, when the class is first loaded in the memory.

A class can have multiple Static blocks and each block will execute in the same sequence in which they have been written into the program.

The syntax for static block is:

static {
	// variable initialization and compuation

Example 7: Use of static block in java

class StaticExample {

    static int A = 3;
    static int B;
    static int MAX;
    static {

        System.out.println("First Static block.");
        B = A * 4;
    static {

        System.out.println("Second Static block.");
        MAX = 10;

    static void meth(int x) {

        System.out.println("x = " + x);
        System.out.println("A = " + A);
        System.out.println("B = " + B);
        System.out.println("MAX = " + MAX);

    public static void main(String args[]) {


When you run this program, the output will be:

First Static block.
Second Static block.
x = 42
A = 3
B = 12
MAX = 10

This is how the above program works:

As soon as the StaticExample class is loaded, all of the static statements run.

First, A is set to 3, then the static block executes one by one in sequence. First block is executed at first which prints First Static block. and initializes B to A*4 which is 12 because A is initialized to 3 in the beginning.

Then second block is executed which prints Second Static block. and MAX is initialized to 10. Then main() method is called, which calls meth(), passing 42 to x.

The four println() statements refer to the three static variables A, B and MAX, as well as to the local variable x.