Static Keyword In Java With Examples

Static keyword in java is mainly used to save memory as with the help of static we can declare data one time and access it in a whole program where we need the same data more than one time in a program. After creating static keyword there is no need to declare that data again and again. Static keyword can be used with:


Static Nested Class

A static nested class is a class that is declared inside another class with a static modifier. A static nested class can refer directly to static members of the enclosing classes, even if those members are private.

Basic Structure of Static Nested Class:

The basic structure of creating a static nested class is as follows:

class OuterClass
{

  static class InnerClass
  {

    //body of static nested class

  }

  //more members of outside class

}

Nested classes are generally made static when they have a strong relationship with the enclosing class, but their existence is independent of an instance of the enclosing class. Unlike inner classes, you can instantiate the static nested class even if you have not instantiated the outer class. For example:

OuterClassName.InnerClassName=new OuterClassName.InnerClassName();

Program Example of Static Nested Class:

The following program demonstrates the use of static nested class.

Step 1: First we create a class OuterClass in which we create static class InnerClass:

class OuterClass
{

  double d=16.5;

  static String s="Static";

  static class InnerClass
  {

    int x;

    public void show()
    {

       x=6;

       System.out.println("value of x is:"+x);

       System.out.println("value of s is:"+s);

     }

  }

}

Step 2: Second we create a class StaticNested in which we create an object of static nested class InnerClass:

class StaticNested
{

  public static void main(String[] args)
  {

    OuterClass.InnerClass obj1=new OuterClass.InnerClass();

    Obj1.show();

  }

}

Output:

value of x is: 6
value of s is: Static

Explanation of Example:

In the above example, as static keyword is used inside the innerclass so it will be called as static class. The s variable is also declared as static so it can be called in the static class.


Static Variable

Any variable when declared with the keyword “static”, it is known as static variable or class variable. Static variable is used to fulfill the common properties or behavior of all objects. For example: institute name for students is common for all students. The static variable gets memory at class loading time only once in class area.

Program Example of Static Variable:

Let us take an example to show the use of static variable.

class Employee
{

  int id;

  String name;

  static String company ="Ducat";

  Employee(int i,String n)
  {

    id = i;

    name = n;

  }

  void print ()
  {

    System.out.println(id+" "+name+" "+company);

  }

  public static void main(String args[])

  {

    Employee e1 = new Employee(111,"Gourav");

    Employee e2 = new Employee(222,"Nishant");

    e1.print();

    e2.print();

  }

}

Output:

111 Gourav Ducat
222 Nishant Ducat

Explanation of Program:

The above program has a class Employee in which we use static variable company. This variable is common to all employees so we take it static and used to display employee information with different id and name but same company. The advantage to make company variable static is that it save memory as it loads once in a class area at class loading time.

Important Points of Static Variable:

  • There is only one copy of the static variable in a class which is shared among all the objects of the class.
  • A static variable can be accessed before any object or instance of a class is created, without making reference to any object.
  • We can make a variable as both static and final by making a static variable constant in java.

For more details read static variable in JAVA


Static Method

The static method is similar to instance or class method of a class but with the difference that the static method can be called through the name of class without creating any instance of that class. A static method is also called class method as it is related with a class and not with individual instance of the class.

It is important to note that static method can only access static block or fields and other static methods of a class. This is because static methods can be used even if no object of that class has been created. We will show this in the example below. In case from the static method you try to access a non static field then compilation error will be generated as the static method does not know which non static field value to use.

Program Example of Static Method:

Let us take an example to show the use of static method.

Step 1: First we create a class Methods in which we declare two static methods:

class Methods
{

  static int add(int a,int b)
  {

    return (a+b);

  }

  static void show()
  {

    System.out.println("show method");

  }

}

Step 2: Second we create a class StaticMethod in which we call the above class methods:

class StaticMethod extends Methods
{

  public static void main(String[] args)
  {

    int result =add(6, 7);

    System.out.println("Result is:" + result);

    show();

   }

}

Output:

Result is: 13
show method

Important Points of Static Method:

  • A static method should be called using the class name rather than an object reference variable.
  • A static method can neither access a non static variable nor a non static method.
  • If you have a class with only static methods and you do not want to create an object of class, you can mark the constructor private.