静态方法访问类的实例成员

在静态方法内部创建对象
public class Example {
private String Field = "实例字段";
public void Method() {
System.out.println("实例方法被调用");
}

// 静态方法,内部创建对象并访问实例成员
public static void staticMethod() {
    Example obj = new Example();
    System.out.println("静态方法访问实例字段: " + obj.Field);
    obj.Method();
}

public static void main(String[] args) {
    staticMethod();
}

}

posted @ 2025-10-16 13:33  clo3o  阅读(4)  评论(0)    收藏  举报