小编Sha*_*016的帖子

为什么静态方法不能直接访问由非静态方法更改的值

public class JustPractice {

    public int points=0;

    public static void main(String args[]) {
        JustPractice ha = new JustPractice();
        ha.end();
        happy();
    }

    public void end() {
        this.points=100;
        System.out.println(points);
    }

    public static void happy() {
        JustPractice object = new JustPractice();
        System.out.println(object.points);
        return;
    }

}
Run Code Online (Sandbox Code Playgroud)

以上是显示:

100
0

而它应该显示:

100
100

java

5
推荐指数
2
解决办法
101
查看次数

标签 统计

java ×1