类方法中只能访问静态变量 或静态方法
非静态方法 可以访问静态成员和非静态成员
public class StaticMethodDetail {
public static void main(String[] args) {
//静态方法 通过类名.方法 直接调用
D.hi();
//非静态方法 不能通过类名调用
//D.say(); 需要先创建对象再调用
new D().say();
}
}
class D{
private int n1=100;
private static int n2=200;
public void say(){ //非静态方法 普通方法
}
public static void hi(){ //静态方法 类方法
//类方法中不允许使用和对象有关的关键字 比如this super
//System.out.println(this.n1);
}
//类方法中只能访问静态变量 或静态方法
//口诀 静态方法只能访问静态成员
public static void hello(){
System.out.println(n2);
hi();
// say(); 无法访问
}
//普通成员方法 既可以访问 非静态 也可以访问静态
//小结 非静态方法 可以访问静态成员和非静态成员
public void ok(){
//非静态成员
System.out.println(n1);
say();
//静态成员
System.out.println(n2);
hello();
}
}