1. 使用static修饰的变量
public class ClassTest { public static void main(String[] args) { User obj = new User(); // obj.func(); 使用对象调用static修饰的func, 虽也能执行, 但不建议这么做 User.func(); System.out.println(User.i); // System.out.println(User.x); 此处的x没有使用static修饰, 不能用类名来调用 System.out.println(obj.x); // 使用对象调用 } } class User{ static int i = 10; int x = 10; public static void func() { System.out.println("run func......"); System.out.println(User.i); // System.out.println(User.x); 此处的x没有使用static修饰, 不能用类名来调用 } }
原文:https://www.cnblogs.com/quzq/p/13675235.html