简体   繁体   English

从一类访问静态变量到另一类,但获得不正确的值

[英]Accessing Static variable from one class to another but getting incorrect value

This is my first question on stackoverflow, 这是我关于stackoverflow的第一个问题,

I want to access static int variable of one class in another class but when i access this variable in another class it always gives me "zero" . 我想在另一类中访问一个类的静态int变量 ,但是当我在另一类中访问此变量时,它总是给我“零”

This is First class : 这是头等舱

package kk;

public class ag {
    public static int n=0;

    public static int as()
    {
        return n;
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        n=3;
        n=n*5;
        System.out.println(n);
    }


}

Here the output is 15 ie n=15 here. 此处的输出为15,即此处n = 15。

Second class: 第二类:

package kk;

public class ah extends ag {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //ag aa =new ag();
int k =ag.as();
System.out.println(k);
    }

}

In this am trying to access the static variable n from First class but getting 0 as output but i want 15. 在这种情况下,我试图从头等舱访问静态变量n,但得到0作为输出,但我想要15。

You are getting 0 because that is the initial value of n in the first class ag . 您将获得0,因为这是第一类agn的初始值。 n is only changed to 15 when ag 's main() method is executed, but you are currently not doing this. 仅当执行agmain()方法时, n才更改为15,但是您当前不执行此操作。

You could call the ag.main() method "manually" as part of the main() method of ah , eg:- 您可以“手动”调用ag.main()方法作为ahmain()方法的一部分,例如:

ag.main(null); // <- new code
int k =ag.as();
System.out.println(k);

This will set n to 15, but is not the best way to do this. 这会将n设置为15,但这不是最好的方法。

Java keeps the values maintained for variables only for execution. Java保留为变量保留的值,仅用于执行。 When 1st program finishes execution, this object no longer exists. 当第一个程序完成执行时,该对象不再存在。 Hence for 2nd class execution this value will not be present & initialized to 0 according to definition. 因此,对于第二类执行,此值将不存在且根据定义初始化为0。

If you really want it to be 15 then initialize it to 15. 如果您确实希望将其设置为15,则将其初始化为15。

package kk;

public class ag {
    public static int n=3*5;

    public static int as()
    {
        return n;
    }
}

OR 要么

call the main() of ag before calling as() 在调用as()之前先调用ag的main as()

package kk;

public class ah extends ag {

    public static void main(String[] args) {
        ag.main(args);
        int k =ag.as();
        System.out.println(k);
    }
}

Your calculations: 您的计算:

n=3; n=n*5;

are evaluating in main() method of class ag, so it has no impact on class ah where you have second main() method - it looks like you have to programs, not just one. 是在ag类的main()方法中求值的,因此它对具有第二个main()方法的class ah没有影响-看起来您必须编程,而不仅仅是编程。

@anju, One more thing to consider ... @anju,还有一件事要考虑...

When creating static variables, the compiler sometimes substitutes the value to save time. 创建静态变量时,编译器有时会替换该值以节省时间。 Especially when you use the keyword final. 特别是当您使用关键字final时。 That means when you compile, it will substitute the current value in your secondary (or usage) classes. 这意味着您进行编译时,它将在辅助(或用法)类中替换当前值。 If you change that value, you need to recompile ALL usages or compile everything, otherwise, they will have the old value. 如果更改该值,则需要重新编译所有用法或编译所有用法,否则它们将具有旧值。

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

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