搜索文档
static
- static 的本质是修饰静态内容的。
- 用 static 修饰的变量称为静态变量。
- 用 static 修饰的方法称为静态方法。
- 在类中,用 static 修饰的成员变量或方法从属于类,生命周期和类相同。普通变量和方法从属于对象。
重点难点
对象可以调用类中的静态属性和静态方法。
javapublic class Index { public static void main(String[] args) { Student stu = new Student(); stu.method_sta(); System.out.println(stu.str); } } class Student { int id = 1001; static String str = "我是静态属性"; public void method_pub() { System.out.println("我是普通方法"); } static void method_sta() { System.out.println("我是静态方法"); } }
在类的静态方法中不可以使用对象的成员变量和方法。
javapublic class Index { public static void main(String[] args) { Student stu = new Student(); } } class Student { int id = 1001; static String str = "我是静态属性"; public void method_pub() { System.out.println("我是普通方法"); } static void method_sta() { System.out.println(id); // 找不到 method_pub(); // 找不到 System.out.println("我是静态方法"); } }
