Introduction To Static Keyword In Java

Introduction

In this article we discuss static keywords in Java.

Static Keyword

The main use of static is for memory management. It can be be used in 3 ways:

  1. Static method 
  2. Static variable
  3. Static blocks

1. Static method

When we use the static keyword with a method in the class it is known as a static method.

  • It can be invoked without the need for creating an instance of a class.
  • It belongs to the class rather than object of a class.
  • It can call only other static methods and cannot call a non-static method from it.
  • It can access a static data member and it cannot access non-static data.
  • It can't refer to the "super" and "this" keywords anyway.

Example

In this example, we create a static method to print the value of the static variable y.

class StaticMethodEx
 
{
   
int x; //set to zero
   
static int y; //when class is loaded y is set to zero but not other object created.
   
StaticMethodEx()
      {
        //
Constructor incrementing static variable y
       
y++;
      }
   
public void showvalues()
     
{
       
System.out.println("x = "+x);
       
System.out.println("y = "+y);
      }
    //
public static void incrementvalue()
     
{
        //
x++;
        //
      }
  }
class OutputClass
 
{
   
public static void main(String args[])
     
{
       
StaticMethodEx s1 = new StaticMethodEx();
       
s1.showvalues();
       
StaticMethodEx s2 = new StaticMethodEx();
       
s2.showvalues();
        //
StaticMethodEx.y++;
        //
s1.showvalues();
      }
  }

Output

pic-1.jpg

2. Static variable

When we declare any variable in our program as static, it is known as a static variable. Some properties of static variables are:

  • It can get memory only once in the class area at the time of class loading.
  • It can be used to refer to the same property of all objects, in other words a single copy is shared by all instances of the class (that is not unique for each obj).
  • It can be accessed directly by the class name and doesn't need an object

Example

In this example, sv1 is the instance of the object, in this (initial) the value of a is 1 and the value of b is 8, according to the arithmetic operations in the constructor. Now, in the next instance of the object StaticVariablesEx is sv2. Here, the value of a is 2 because of the static variable while the value of b is again 8, because of non-static. In the final instance of the object StaticVariablesEx, the a is 3 while the b is 8.

public class StaticVariablesEx
 
{
    private static int a = 0;
   
private int b = 3;
    public StaticVariablesEx
()
     
{
       
a++;
       
b += 3;
       
System.out.println(a + " and " + b);
     
}
    public static void main(String args[]
)
     
{
        StaticVariablesEx sv1 = new StaticVariablesEx
();
        StaticVariablesEx sv2 = new StaticVariablesEx
();
        StaticVariablesEx sv3 = new StaticVariablesEx
();
     
}
 
}

Output 

pic-2.jpg

3. Static block

A Static block is a block of statements inside a Java class and it will be executed when a class is first loaded into the JVM. A Static block:

  • has the main purpose of creating the static data member.
  • is executed before the main method at the time of classloading.

Example

In this example, we create three static blocks and two static methods but we are not providing any functionality for them because we create them just for clarification about "how to create static method and static block" .

public class StaticBlockEx
 
{
   
static
      
{
        System.out.println("1st static block"
);
     
}
    public static String stString = "Static Variable"
;
   
static
     
{
        System.out.println("2nd static block and "  + stString
);
      
}
    public static void main(String[] args
)
     
{
        StaticBlockEx statEx = new StaticBlockEx
();
        StaticBlockEx.stMethod2
();
     
}
   
static
     
{
        stMethod1
();
        System.out.println("3rd static block"
);
     
}
    public static void stMethod1
()
     
{
        System.out.println("1st static method"
);
     
}
    public static void stMethod2
()
     
{
        System.out.println("2nd static method"
);
     
}

  }

Output

pic-3.jpg

 

Up Next
    Ebook Download
    View all
    Learn
    View all