简体   繁体   English

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

[英]why cannot a static method directly access a value changed by a non static method

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;
    }

}

The above is displaying: 以上是显示:

100 100
0 0

whereas it should be displaying: 而它应该显示:

100 100
100 100

You are looking at two different instances of your class. 您正在查看班级的两个不同实例。

Every instance gets their own copy of the instance fields, and they are completely independent from each-other. 每个实例都获得自己的实例字段副本,并且它们完全独立于彼此。

JustPractice ha = new JustPractice();
ha.end();    // this one has "100"

JustPractice object = new JustPractice();  // this one has "0"
System.out.println(object.points);   

The static method can only access ha 's instance fields if you provide it with a ha as a parameter. 如果你提供ha作为参数,静态方法只能访问ha的实例字段。

Make points static . 使points static Then you got what you want. 然后你就得到了你想要的东西。

public static int points=0;

make points static will keep only one variable for all instance of your class. make points static将只保留一个类的所有实例的变量。

Else each initialization will create separate individual variable and will assign the value to 0 否则,每个初始化将创建单独的单个变量,并将值赋值为0

its because of when method is making new object then that time it will having another copy as of the reference object will be having independent copy per object of the class remember the java basic? 它因为当方法正在制作新对象时那么它将具有另一个副本作为reference object将具有独立副本的每个对象的类记住java基础?

and if u will make int as static object then it will give your output what u want and what u asking for simple example from yours is 如果你将int作为static对象,那么它将为你的输出提供你想要的东西以及你从中寻求简单例子的东西

public static int points = 0;


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

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

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

hope it helpful 希望它有所帮助

A static method can access a non-static object, but only if it is passed to the method. 静态方法可以访问非静态对象,但仅限于将其传递给方法。 Given that the static method in question is the method in which the program begins, you have no ability to pass anything to it. 鉴于所讨论的静态方法是程序开始的方法,您无法向其传递任何内容。

What it cannot do, within the scope of a non-static class, is access any of the instance members or functions. 在非静态类的范围内,它不能做的是访问任何实例成员或函数。 The problem is not that you're instantiating ha from a static method, it's that you're accessing the member points, which is an instance member that belongs to every instantiation of your class. 问题不在于您是从静态方法实例化ha,而是您正在访问成员点,这是一个属于您的类的每个实例的实例成员。

In order to fix this, you can either pass points to the instance of JustPractice (ha) that you're creating, and have one of JustPractice's methods return the final value, or you can make points, end() and happy() static as well, in which case all can work happily with one another. 为了解决这个问题,你可以将点传递给你正在创建的JustPractice(ha)实例,并让JustPractice的方法之一返回最终值,或者你可以创建点,结束()和happy()静态同样,在这种情况下,所有人都可以彼此愉快地工作。

it is displaying as 100 0 instead of 100 100 because you are creating a new instance of class JustPractice in your happy() method, instead of passing reference ie ha of existing instance. 它显示为100 0而不是100 100,因为您正在happy()方法中创建类JustPractice的新实例,而不是传递引用,即现有实例的ha。

new newly created instance is showing the default value that that u have given in your class, ie 0. 新创建的新实例显示您在类中给出的默认值,即0。

You set value of the points instance variable in end method.and for the object in happy method you don't initialize the points instance variable of object. 您可以在end方法中设置points实例变量的值。对于happy方法中的对象,您不会初始化object的points实例变量。

So it gets the default value 0. 因此它获得默认值0。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM