zoukankan      html  css  js  c++  java
  • Android-Java-静态变量

    描述Person对象:

    package android.java.oop09;
    
    // 描述Person对象
    public class Person {
    
        private String name;
        private int age;
        private double height;
    
        private static String country = "中国";
    
        private Person() {}
    
        public Person(String name, int age, double height) {
            this.name = name;
            this.age = age;
            this.height = height;
        }
    
        public void showValue() {
            System.out.println("name:" + name + " age:" + age + " height:" + height + " country:" + country);
        }
    
    }

    main测试方法:

    package android.java.oop09;
    
    public class Demo01 {
    
        public static void main(String[] args) {
    
            Person person1 = new Person("李双贱", 30, 17.0);
            person1.showValue();
    
            Person person2 = new Person("李双2B", 32, 16.0);
            person2.showValue();
    
            Person person3 = new Person("李双SB", 31, 17.0);
            person3.showValue();
    
            Person person4 = new Person("李双shit", 33, 16.0);
            person4.showValue();
    
            Person person5 = new Person("李双38", 32, 17.0);
            person5.showValue();
    
            Person person6 = new Person("李双骗子", 99, 16.0);
            person6.showValue();
    
            Person person7 = new Person("李双女骗子", 99, 17.0);
            person7.showValue();
    
            Person person8 = new Person("李双骚", 99, 17.0);
            person8.showValue();
    
            // ....
    
        }
    
    }

    以上案例对应内存图:

     

    1.静态变量随着类的加载而加载,随着类的消失而消失 (必须是共享数据才使用 静态, 静态生命周期很长)

    2.非静态变量随着对象的加载二加载,随着对象的消亡而消亡(如果是共享数据,还使用非静态的话,会导致每一个对象都有 此变量 浪费内存空间)

  • 相关阅读:
    CF459E Pashmak and Graph
    cf478D Red-Green Towers
    cf255C Almost Arithmetical Progression
    准备做,但是还没做的
    CF219C hoosing Capital for Treeland
    最小中间和
    【NO.9】jmeter
    【设计用例】站内信
    【有意思的BUG】客户端无厘头 已连网的场景初始化太慢 未连网的场景异常崩溃
    禅道BUG系统
  • 原文地址:https://www.cnblogs.com/android-deli/p/10351246.html
Copyright © 2011-2022 走看看