zoukankan      html  css  js  c++  java
  • 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.

        

  • 相关阅读:
    Struts2获取参数的几种方式
    Struts2的Action中访问servletAPI方式
    struts2中常用配置
    struts2发送ajax的几个问题(不使用struts2-json-plugin的情况下)
    深入Struts2的过滤器FilterDispatcher--中文乱码及字符编码过滤器
    Ironic 裸金属实例的部署流程
    Ironic 裸金属管理服务的底层技术支撑
    Cinder AZ 与 Nova AZ 的同步问题
    OpenStack 对接 Ceph 环境可以创建卷但不能挂载卷的问题
    OpenStack 节点重启后无法联网的问题
  • 原文地址:https://www.cnblogs.com/chenqr/p/10381502.html
Copyright © 2011-2022 走看看