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();

        // ....

    }

}

 

以上案例对应内存图:

Android-Java-静态变量

 

 

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

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

 

Android-Java-静态变量

上一篇:HTML标签常用属性


下一篇:【pygame游戏编程】第二篇-----移动图像