Static keyword in java

Let’s learn what is 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 applied to variables, code block, methods and nested class. static keyword is a non-access modifier.

static keyword in java

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 are also known as static member variables. 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 Sparrow
{
   private static String name;
   public Sparrow(String name)
   {
      Sparrow.name = name;
   }
   public void displayName()
   {
      System.out.println("Sparrow name : " + name);
   }
}
public class Main
{
   public static void main(String[] args)
   {
      Sparrow obj1 = new Sparrow("Song Sparrow");
      Sparrow obj2 = new Sparrow("Field Sparrow");
      obj1.displayName();
      obj2.displayName();
   }
}

Output:

Sparrow name : Field Sparrow
Sparrow name : Field Sparrow

As you can see the output both method calls (displayName() method) will print “Field Sparrow”. Why?

Because static variables are shared between instances. That is, once we change static variable all instances will see that change we made.

So when constructor is called with parameter “Field Sparrow”, it modified the static variable name and because both instances are sharing that variable and that’s why it prints “Field Sparrow” twice.

NOTE: 

  1. Static variables is used for memory management.
  2. Only class level variables can be “static” variables.
  3. Static variable belongs to class not object. It can be accessed using class name.
  4. Static variable is a class level variable and it is initialized when class is loaded.
  5. 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.

Syntax:

ClassName.methodName();

Static methods cannot access instance methods and instance variables directly. 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 Addition
{
   public static void displaySum(int a, int b)
   {
      System.out.println("Addition = " + (a + b));
   }
}
public class Main
{
   public static void main(String[] args)
   {
      Addition.displaySum(10, 10);
      displayHelloWorld(); // short form for Main.displayHelloWorld()
   }
   public static void displayHelloWorld()
   {
      System.out.println("HelloWorld");
   }
}

Output:

Addition = 20
HelloWorld

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.

public class StaticBlockExample
{
   // static variable
   static int a = 10;
   static int b;
   // static block
   static
   {
      System.out.println("Static block called.");
      b = a * 5;
   }
   public static void main(String[] args)
   {
      System.out.println("In main method");
      System.out.println("Value of a : " + a);
      System.out.println("Value of b : " + b);
   }
}

Output:

Static block called.
In main method
Value of a : 10
Value of b : 50

As you can see the output “Static block called” prints first. So static block always get executed first and then “main()” method.

NOTE:

  • We cannot execute java program without main() method. Because it was possible till JDK 1.6. Since JDK 1.7, it is not possible to execute class without main method.

Advantage 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.