Categories
Uncategorized

Static Keyword in Java

The static Keyword in Java is used for memory management. The static keyword belongs to class rather than instance of the class. All instances share the same copy of the variable. A class variable can be accessed directly with the class, without the need to create a instance. we can apply static keyword with:

  1. Variables
  2. Methods
  3. Block

Java static Variable : A variable which is declared with the static keyword is a static variable.

  • The static variable can be used to refer the common property of all objects.
  • The static variable gets memory only once in class area at the time of class loading.
  • Static variables are initialized only once , at the start of the execution . These variables will be initialized first, before the initialization of any instance variables.
  • A static variable can be accessed directly by the name and doesn’t need any object.

For example with non-static variable                                                                                                    

class Counter

int count=0;//will get memory when instance is created.

Counter(){ 

        count++; 

        System.out.println(count); 

         } 

     public static void main(String args[]){ 

        Counter c1=new Counter(); 

        Counter c2=new Counter(); 

        Counter c3=new Counter(); 

        } 

}

Output: 1

               1

               1

Using static Variable:

class Counter2{ 

       static int count=0;  //will get memory only once and retain its value.

       Counter2(){

  count++; 

       System.out.println(count); 

       }

public static void main(String args[]){ 

      Counter2 c1=new Counter2();

Counter2 c2=new Counter2(); 

Counter2 c3=new Counter2(); 

        } 

}

Output: 1

             2

             3 

Static Method : If we apply static keyword with any method then it is a static method.Static Methods can access class variables without using object of the class. It can access non-static methods and non-static variables by using objects. Static methods can be accessed directly in static and non-static methods.

  • A static method belongs to the class rather than object of a class.
  • A static method can be invoked without the need for creating an instance of a class.
  • static method can access static data member and can change the value of it.

Example:

class Calculate{

static int cube(int x){ 

        return x*x*x; 

}

public static void main(String args[]){ 

int result=Calculate.cube(2); 

System.out.println(result); 

}

Output: 8

But there is some restriction in static method i.e. The static method can not use non static data member or call non-static method directly.

example program:

class A{ 

int a=40;//non static

public static void main(String args[]){

System.out.println(a); 

}

}       

Output: Compile time error.

Static Block : Static block is mostly used for changing the default values of static variables.This block gets executed before main method at the time when class is loaded in the memory. A class can have multiple Static blocks, which will execute in the same sequence in which they have been written into the program.

example:

class A2{ 

static{

          System.out.println(“static block is invoked”);

          }

public static void main(String args[]){

          System.out.println(“Hello main”); 

          } 

}

Output: Static block is invoked

          Hello main.

 

References: http://www.javatpoint.com

                      http://beginnersbook.com 

Note: Anyone who want to update anything in this please comment. Your knowledge can help others. Thank you.