static的使用

37 阅读1分钟
package com.wxt1.static的使用;

/**
 * static可以修饰成员(成员变量,成员方法)
 * 被static修饰的成员变量的特点:
 * 1、随着所属类字节码文件的加载就在方法区中的静态区开辟内存了
 * 2、被static修饰的变量可以通过类名来访问
 */

class Student {
    String name;
    static String city = "中国";
}

public class demo {
    public static void main(String[] args) {
//        被static修饰的变量可以通过类名来访问
        System.out.println(Student.city); // 中国
    }
}