Java object-oriented 3-static keyword

static keyword

  1. concept

    Static is called static and can be used to modify the attributes, methods, code blocks, and internal classes of a class.

    Features:

    1. Load as the class loads
    2. Priority to the existence of the object
    3. Decorated members, shared by all objects
    4. Can not create an object, directly called by the class
  2. The difference between static and non-static

    Static variable: The variable modified by static is created when the class is loaded, and does not change with the creation of the object. The static variable is located in the method area, and the corresponding variable disappears when the class disappears.

    Non-static variables: Variables that are not modified by static. Each time an object is created, memory is allocated for the variable and stored in the heap memory. The variable corresponding to the disappearance of the object disappears.

    The order of initialization in JAVA:

    Load class

    Static variable initialization

    Static code block; [It can only schedule static, not non-static]

    Member variables

    Structure code block

    Construction method

    Normal code block

  3. static keyword

    static attribute

    Static properties are shared by all objects of the class, that is, no matter how many objects are created, there is only one static property in memory.

    public class Chinese {
          
          
        /**
         * 成员变量,创建对象时复制一份
         */
        String name ;
        int age ;
        //country1 成员变量 ,每个中国人的国籍都是中国,每当有对象创建时都复制一份到堆中,浪费内存空间.
        String country1 = "中国";
        /**
         * 当所有对象中的某个属性值都一样时,这个属性值可以采用static静态修饰,在内存中只有一份(这个属性放在方法区中),对所有对象共享,
         */
        static String country2 = "中国";
    }
    
    public static void main(String[] args) {
          
          
        Chinese zs = new Chinese();
        zs.name = "张三";
        zs.age = 18;
        System.out.println(zs.country1);//只能通过对象来进行调用
        System.out.println(zs.country2);//country2被static修饰,可以通过对象调用
        System.out.println(Chinese.country2);//被static修饰,可以通过类调用,省去创建对象,例如 Math.PI
    }
    

    The static method can be called with an object or directly with the class name. It is recommended to use the class name to call directly

    Inside the static method, you can only access the static properties of the class, and you cannot access the non-static properties of the class.

    Member methods can access member variables, as well as static variables and static methods.

    public class StaticMethod {
          
          
        String  mame;
        static  String country = "中国";
        //被static修饰的方法也是存在方法区
        public static void testStatic(){
          
          
            System.out.println("静态方法"+country);//静态方法只能访问 静态的变量
        }
        public void test(){
          
          
            System.out.println(mame);  //成员方法可以访问 成员变量
            testStatic();
            System.out.println(country);// 成员方法可以访问静态方法 还可以访问静态变量
        }
        public static void main(String[] args) {
          
          
            StaticMethod.testStatic();//可以直接通过类名调用
        }
    }
    
    public class TicketSeller {
          
          
        //所有的窗口票数都是同样的
        static int ticket=11;
        //出票的方法只有一个
        public static void sellticket(){
          
          
            System.out.println("卖票的静态方法"+ticket);
            ticket=ticket-1;
        }
        public static void main(String[] args) {
          
          
            TicketSeller.sellticket();
            TicketSeller.sellticket();
            TicketSeller.sellticket();
            TicketSeller.sellticket();
        }
    }/*输出结果为
    卖票的静态方法11
    卖票的静态方法10
    卖票的静态方法9
    卖票的静态方法8*/
    

Guess you like

Origin blog.csdn.net/qdzjo/article/details/109214548