The static keyword in java is used to make methods and variables associate with the class so that object is not required to call these methods and variables.

The static keyword can be used for following purpose :

1 ) it is used to make the variable static.
2 ) it is used to make method static (also known as class method)
3 ) it is used to create static block
4 ) it is used to create nested class .

  • static : creating static variable/data member

    • A single copy of the static data member is created by the JRE in the memory at the time of class loading , reference of the static data member is commonly shared to all the objects of the same class by the JRE.

    • If we are in same class we can directly access static member from static context without creating the object of the class , otherwise we have to use class name to access static member.
      forexample : ClassName.memberName

    • It makes your program memory efficient (i.e it saves memory).


    Syntax to declare static data member

    
    static [data_type]  [variable_name] ;
    


    Example of static data member.

    
    class StaticMember {
    	int x, y;// Instance data members
    	static int z; // static/class member
    
    	public StaticMember(int a, int b, int c) {
    		x = a;
    		y = b;
    		z = c;
    	}
    
    	public void show() {
    		System.out.println("x =" + x);
    		System.out.println("y =" + y);
    		System.out.println("static z =" + z);
    
    	}
    
    	public static void main(String[] args) {
    		// create object
    		StaticMember s1 = new StaticMember(12, 11, 9);
    		s1.show();
    		System.out.println("===============");
    		StaticMember s2 = new StaticMember(10, 5, 7);
    		s1.show();
    		System.out.println("===============");
    		s2.show();
    		// Note:static member z is shared by both the objects
    	}
    }
    

    Output:
    x =12
    y =11
    static z =9
    ===============
    x =12
    y =11
    static z =7
    ===============
    x =10
    y =5
    static z =7
    

    If one object changes the value of static variable. it is also reflected for other objects.

  • Memory representation of above exmaple

    Memory representation of above exmaple
  • static: for creating static method

    Method which is defined by using static keyword is known as static Methods.

    • For static methods object is not required to call these methods. we can call these methods with class name, for example.
      ClassName.methodName();

    • static methods can access static data member directly and can change the value of it.

    • static methods are associated with the class . They belongs to class rather than object of the class.


    Syntax to create static method

    
    static [other modifiers if any]  [return type]  [method name](argument if any)
    {
    //body of methods
    }


    Example of static method.

    class Student {
    	int rollno;
    	String name;
    	static String college;
    
    	Student(int rollno, String name) {
    		this.rollno = rollno;
    		this.name = name;
    		
    	}
    
    	public static void setCollege(String col) {
    		college = col;
    	}
    
    	public void show() {
    		System.out.println("Name :" + this.name);
    		System.out.println("RollNo. :" + this.rollno);
    		System.out.println("College :" + Student.college);
    	}
    
    	public static void main(String[] args) {
    		college="IIT Kanpur";
    		Student s1 = new Student(145, "John");
    		s1.show();
    		System.out.println("================");
    		Student s2=new Student(1452, "Petter");
    		s2.show();
    		System.out.println("================");
    		Student.setCollege("IIT Delhi");//calling static with class name
    		s1.show();
    		s2.show();
    		System.out.println("================");
    		setCollege("IIT Mumbai");//directly calling 
    		s1.show();
    		s2.show();
    		System.out.println("================");
    		s1.setCollege("GLA");//we can call static members on object also
    		s1.show();
    		s2.show();		
    	}
    
    }
    

    Output:
    Name :John
    RollNo. :145
    College :IIT Kanpur
    ================
    Name :Petter
    RollNo. :1452
    College :IIT Kanpur
    ================
    Name :John
    RollNo. :145
    College :IIT Delhi
    Name :Petter
    RollNo. :1452
    College :IIT Delhi
    ================
    Name :John
    RollNo. :145
    College :IIT Mumbai
    Name :Petter
    RollNo. :1452
    College :IIT Mumbai
    ================
    Name :John
    RollNo. :145
    College :GLA
    Name :Petter
    RollNo. :1452
    College :GLA
    

    We have seen , in how many ways we can call static method in above example.

  • Restrictions for static method

    Static methods have several restrictions

    • Static methods can only call other static members. They can not call non-static members directly.

    • They can not refer to this and super in any ways.

  • static : for creating static block.

    Static Block : It is a block of statements which are defined by using static keyword.

    • It is used to defined static data members of the class.

    • static block is implicitly executed by the JRE at the time of class Loading.

    • static block gets executed exactly once.


    Syntax to create static block

    
    static
    {
    //statements 
    }


    Example of static block.

    
    class StaticBlock {
    	static int a;
    	static {
    		System.out.println("This is first static block");
    		System.out.println("Value of a=" + a);
    
    		a = 4;
    	}
    	static {
    		System.out.println("This is second static block");
    		System.out.println("Value of a=" + a);
    		a = 6;
    	}
    
    	public static void main(String[] args) {
    		System.out.println("This is main() method");
    		System.out.println("value of a =" + a);
    	}
    }
    

    Output:
    This is first static block
    Value of a=0
    This is second static block
    Value of a=4
    This is main() method
    value of a =6
    

What next?

The next topic is Introduction to OOPs in java

Share this page