Site icon C1CTech

Java static Keyword(With Examples)

This post is about Java static keyword along with static methods, static variables, static class, and static blocks with the help of simple examples.

Java static Keyword

In Java, If we want to access class members without creating an instance of the class, we need to declare the class member with static keyword.

In Java, static keyword can be used for the following:

The keyword static indicates that the particular member belongs to a class itself, rather than to an instance of that class.

Example
class Test {
    // static method
    static void m1() {
        System.out.println("from m1");
    }

    // non-static method
    void m2() {
        System.out.println("from m2");
    }
}

class Main {
    public static void main(String[] args) {
        Test test = new Test();
        //to call non-static method we need object
        test.m2();

        //calling static method using object
        test.m1();

        // calling static method using class name Test
        Test.m1();
    }
}
Output
from m2
from m1
from m1

Static Variables

Example
class Employee {
    //non-static variables
    int eid;
    int salary;

    //static variable
    static String companyCeo;

    public void show() {
        System.out.println(eid + " : " + salary + " : " + companyCeo);
    }
}

public class Main {

    public static void main(String[] args) {

        Employee e1 = new Employee();

        //for non-static variable, object is compulsory
        e1.eid = 100;
        e1.salary = 5000;

        //for static variable, we do not need object
        Employee.companyCeo = "Arun";

        Employee e2 = new Employee();
        e2.eid = 101;
        e2.salary = 8000;

        e1.show();
        e2.show();
    }
}
Output
100 : 5000 : Arun
101 : 8000 : Arun
Example explained

Static Blocks

Example
class Employee {
int eid;
int salary;

//static variable
static String companyCeo;

//execute when class loads
//first static block
static {
companyCeo = "Arun";
System.out.println("Inside static block1");
}

//executes when you create an object
public Employee(int eid, int salary) {
this.eid = eid;
this.salary = salary;
System.out.println("Inside constructor");
}

//second static block execute after the first static block
static {
companyCeo = "Vrun";
System.out.println("Inside static block2");
}

public void show() {
System.out.println(eid + " : " + salary + " : " + companyCeo);
}
}

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

Employee e1 = new Employee(100, 5000);

Employee e2 = new Employee(101, 6000);

e1.show();
e2.show();
}
}
Output
Inside static block1
Inside static block2
Inside constructor
Inside constructor
100 : 5000 : Vrun
101 : 6000 : Vrun
Example explained

As soon as the Employee class is loaded,

Static Methods

Example
class Employee {
    //non-static variables
    int eid;
    int salary;

    //static variables
    static String companyCeo;
    static String companyName;

    //Employee class constructor
    public Employee(int eid, int salary) {
        this.eid = eid;
        this.salary = salary;
    }

    //non-static method
    void show() {
        System.out.print(eid + " : " + salary + " : ");
        //access static members inside non-static method
        display();
    }

    //static method
    static void display() {
        //error: cannot access non-static members inside static method
        //System.out.print(eid + " : " + salary + " : ");
        //show();
        System.out.print(companyName + " : " + companyCeo + "\n");
    }
}

class Main {

    public static void main(String[] args) {

        Employee e1 = new Employee(100, 5000);

        Employee e2 = new Employee(101, 6000);

        //to access static variables and methods, we do not need object.
        //access static variables using class 
        Employee.companyCeo = "Arun";
        Employee.companyName = "HCL";

        //access static method using class 
        Employee.display();

        e1.show();
        e2.show();
    }
}
Output
HCL : Arun
100 : 5000 : HCL : Arun
101 : 6000 : HCL : Arun
Example explained

Static Nested classes

In Java, we can declare a class inside another class (Outer class). Such classes are known as nested classes. Nested classes are of two types:

Differences between static and non-static nested classes:
Example
//Outer class
class OuterClass {
    private static String msg = "Hello World";

    // Static nested class
    public static class NestedStaticClass {

        // Only static members of Outer class are accessible
        
        public void show() {  // non-static method           
            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 
        public void display() {  // non-static method
            System.out.println("Message from non-static nested class: " + msg);
        }
    }
}

class Main {

    public static void main(String args[]) {

        // Creating instance of nested Static class
        // using Outer class name
        OuterClass.NestedStaticClass obj = new OuterClass.NestedStaticClass();

        // Calling non-static method of nested static class
        obj.show();

        // To create instance of non-static nested class,
        // outerClass instance is necessary.
        OuterClass outer = new OuterClass();
        OuterClass.InnerClass inner = outer.new InnerClass();

        // Calling non-static method of Inner class
        inner.display();

        // We can also combine above steps in one
        // step to create instance of Inner class
        //OuterClass.InnerClass innerObject = new OuterClass().new InnerClass();
    }
}
Output
Message from nested static class: Hello World
Message from non-static nested class: Hello World
Exit mobile version