Java static keyword

The static in java is used for memory management mainly. We can apply static with variables, methods, blocks and class.

1) Java static variable

If you declare variable as static, it is known as a static variable.

  the static variable can be used to refer to the common property of all objects (which is not unique for each object). For example, the company of employees, college name of students, etc.

    the static variable gets memory once in the class area at the time of class loading.

Advantages of static variable

It make your program effecient (i.e., it saves memory).

Understanding program without static

    class Student{  
         int rollno;  
         String name;  
         String college="ITS";  
    }  

 Suppose the are 500 students in my college, now all instance student data members will get memory each time when the object is created. All student have their name, rollno. So instance data member is good in such case. Here, the college refers to the common property of all obeject. If we make it static, the field will get memory only once.

! Java static property is shared to all objects.

Example of static variable

    //Java Program to demonstrate the use of static variable  
    class Student{  
       int rollno;//instance variable  
       String name;  
       static String college ="ITS";//static variable  
       //constructor  
       Student(int r, String n){  
       rollno = r;  
       name = n;  
       }  
       //method to display the values  
       void display (){System.out.println(rollno+" "+name+" "+college);}  
    }  
    //Test class to show the values of objects  
    public class TestStaticVariable1{  
     public static void main(String args[]){  
     Student s1 = new Student(111,"Karan");  
     Student s2 = new Student(222,"Aryan");  
     //we can change the college of all objects by the single line of code  
     //Student.college="BBDIT";  
     s1.display();  
     s2.display();  
     }  
    }  

 

Program of the counter without static variable

In this example, we created a instance variable named count which is incremented in the constructor. SInce instance avariable gets the memory at time of object creation, each object  will have the copy of the instance avariable. If it is incremented, it won't reflect other objects. So each object have the value 1 in the count avariable.

    //Java Program to demonstrate the use of an instance variable  
    //which get memory each time when we create an object of the class.  
    class Counter{  
    int count=0;//will get memory each time when the instance is created  
      
    Counter(){  
    count++;//incrementing value  
    System.out.println(count);  
    }  
      
    public static void main(String args[]){  
    //Creating objects  
    Counter c1=new Counter();  
    Counter c2=new Counter();  
    Counter c3=new Counter();  
    }  
    }  

Output:

1
1
1

 Program of the counter with static variable

//Java Program to demonstrate the use of static variable  
class Student {
    static int count = 0;

    Student() {
        count++;
        System.out.println(count);
    }

    
}

//Test class to show the values of objects  
public class TestStaticVariable1 {
    public static void main(String args[]) {
        Student s1 = new Student();
        Student s2 = new Student();
        Student s3= new Student();
    }
}

Output:

1
2
3

2) Java static method

If you apply static keyword with method, it is known as static method.

  a static method is belong to the class rather than the object of a class.

    

猜你喜欢

转载自www.cnblogs.com/chenqr/p/10381502.html
今日推荐