Java Static keyword

Static is a keyword in Java used to represent the class members. It can be used with variables, methods, initializer blocks, and nested classes.

Types of class members:

  1. Static data members.
  2. Static initializer block.
  3. Static method.
  4. Static nested class.

1. Static data members:

Data members declared with static keywords are known as static data members. These are mainly used to represent those properties that are common to every object. At the time of class loading a single copy is created for static data members, which is shared by all objects.

Memory division in a Java program execution.

In a Java program execution memory is divided into three parts:

  1. Stack: Stack is used to local variables of the methods.
  2. Heap: A heap is used to store objects.
  3. Class Area: The class area is used to store static data members.

Static data members are used to represent those properties that are common to every object.

class MCAStudent{
    //name and rollNo are not common for all students
    //so keep them as non-static data members.
    String name;
    int rollNo;
    //As course offered is same for all students
    //so keep it as static.
    String courseName = "MCA";
    
    //constructor
    MCAStudent(String n, int r){
        name = n;
        rollNo = r;
    }
    
    //display all values
    public void display(){
        System.out.println("Name = " + name);
        System.out.println("RollNo. = " + rollNo);
        System.out.println("Course Name = " + courseName);
        System.out.println("");
    }	
}

public class StaticExample1 {
    public static void main(String args[]){
        //create object of MCAStudent class.
        MCAStudent stu1 = new MCAStudent("Roy", 6);
        MCAStudent stu2 = new MCAStudent("Alex", 15);
        
        //method call
        stu1.display();
        stu2.display();
    }
}

Output

Name = Roy
RollNo. = 6
Course Name = MCA
Name = Alex
RollNo. = 15
Course Name = MCA

 

Non-static data members have different memory locations for different objects.

class Test{
    int num = 0;
    
    //constructor
    Test(){
        num = num + 10;
        System.out.println("Number = " + num);
    }
}

public class StaticExample2 {
    public static void main(String args[]){
        Test obj1 = new Test();
        Test obj2 = new Test();
        Test obj3 = new Test();
        Test obj4 = new Test();		
    }
}

Output

Number = 10
Number = 10
Number = 10
Number = 10

 

Static data members use the same memory locations for all objects.

class Test{
    static int num = 0;
    
    //constructor
    Test(){
        num = num + 10;
        System.out.println("Number = " + num);
    }
}

public class StaticExample3 {
    public static void main(String args[]){
        Test obj1 = new Test();
        Test obj2 = new Test();
        Test obj3 = new Test();
        Test obj4 = new Test();		
    }
}

Output

Number = 10 
Number = 20 
Number = 30 
Number = 40

 

Note: Constructor is not used to initialize the static data members because constructor initializes many times but static data members only once. So instead of a constructor static initialize block is used to initialize static data members.

Static methods:

Static methods represent the behavior of the whole class. An instance of a class is not required to execute static methods. They can be called using class names.

Syntax:

ClassName.methodName
class Display {
    //static method
    public static void display(){
        System.out.println("Hello W3schools360.com!");
    }
}
public class StaticExample4 {
    public static void main(String args[]){
        //No need for object to call static method.
        Display.display();
    }
}

Output

Hello W3schools360.com!

 

Note: We can execute a program without the main method.

public class WithoutMain {
       static{
               System.out.println("Hello W3schools360.com!");
               System.exit(0);
       }
}

Output

Hello W3schools360.com!

 

Limitations of static methods and static initialized blocks.

  1. Non-static data members can’t be accessed in static methods and static initialized blocks.
  2. Non-static methods can’t be invoked in static methods and static initialized blocks.
  3. This or the super keyword can’t be referred to in static methods and static initialized blocks.

1. Non-static data members can’t be accessed in static methods and static initialized blocks.

class Test {
    //non-static data member
    int num = 10;
    
    static{
        //error because non-static data members can't be 
        //accessed in static initializer block.
        System.out.println("Num = " + num);
    }
    
    public static void display(){
        System.out.println("Hello w3spoint.com");
        //error because non-static data members can't be 
        //accessed in static method.
        System.out.println("Num = " + num);
        show();
    }
    
}

public class StaticExample5 {	
    public static void main(String args[]){
        //creating object of Test class
        Test obj = new Test();
        //method call
        obj.display();
    }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cannot make a static reference to the non-static field num 
Cannot make a static reference to the non-static field num 
The method show() is undefined for the type Test at com.w3schools.business.Test.
(StaticExample5.java:15) at com.w3schools.business.StaticExample5.main (StaticExample5.java:31)

2. Non-static methods can’t be invoked in static methods and static initialized blocks.

class Test {
    //non static method	
    public void show(){
        System.out.println("Hello world.");
    }
    
    static{
        //error because non-static methods can't be 
        //accessed in static initializer block.
        show();
    }
    
    public static void display(){
        System.out.println("Hello w3spoint.com");
        //error because non-static methods can't be 
        //accessed in static method.
        show();
    }
    
}

public class StaticExample6 {	
    public static void main(String args[]){
        //creating object of Test class
        Test obj = new Test();
        //method call
        obj.display();
    }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
Cannot make a static reference to the non-static method show() from the type 
Test Cannot make a static reference to the non-static method show() from the type Test 
at com.w3schools.business.Test.(StaticExample6.java:17) at com.w3schools.business.StaticExample6.main (StaticExample6.java:32)

 

3. This or the super keyword can’t be referred to in static methods and static initialize blocks.

class Show {
    int num = 10;
}

class Display extends Show {
    int num = 20;
    
    public static void show(){
        //error because super can't be refers in static 
        //method or static initializer block.
        System.out.println("num of super class = " + super.num);
    }
    
    public static void display(int num){
        //error because this can't be refers in static 
        //method or static initializer block.
        this.num = num;
    }
    
}

public class StaticExample7 {
    public static void main(String args[]){
        Display obj = new Display();
        obj.show();
        obj.display(20);
    }
}

Output

Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot use super in a static context at com.w3schools.business.Display.show
(StaticExample7.java:18) at com.w3schools.business.StaticExample7.main (StaticExample7.java:32)

 

 

 

Please follow and like us:
Content Protection by DMCA.com