简体   繁体   English

如何在不同方法中设置的主方法中获取私有变量?

[英]How do I get a private variable in the main method that was set in a different method?

I am trying to change private variables in a method and then access them with a getter method in the main method (as shown below), but when I get the private variable in the main method it is always 0. How can I set a private variable in a method other than the main, but still be able to access that variable in the main method?我正在尝试更改方法中的私有变量,然后在主方法中使用 getter 方法访问它们(如下所示),但是当我在主方法中获取私有变量时,它始终为 0。如何设置私有主方法以外的方法中的变量,但仍然能够在主方法中访问该变量?

public class Example {

    private int testNumber;

    public static void main(String[] args) {
        Example tester = new Example();
        System.out.println(tester.getTestNumber());
        tester.TheTestExample();
        System.out.println(tester.getTestNumber());
    }

    public int getTestNumber(){
        return testNumber;
    }

    public void setTestNumber(int x){
        this.testNumber = x;
    }

    public void TheTestExample(){
        Example MyTester = new Example();
        MyTester.setTestNumber(4);
        System.out.println(MyTester.getTestNumber());
    }
}

There are two separate instances of Example created by your application.您的应用程序创建了两个单独的Example实例。 One is created by main , and the other by TheTestExample一个由main创建,另一个由TheTestExample

What you seem to be asking is how main can get the reference to the Example instance in the MyTester variable in your version of the TheTestExample method.您似乎要问的是main如何在您的TheTestExample方法版本中的MyTester变量中获取对Example实例的引用。

The answer is that it can't.答案是不能。 You cannot access a local variable outside of its scope.您不能访问其范围之外的局部变量。 The method needs to return the reference in that variable, and the caller needs to save it or use it directly.该方法需要返回该变量中的引用,调用者需要保存它或直接使用它。 For example.例如。

    tester = tester.theTestExample();
    System.out.println(tester.getTestNumber());

public Example theTestExample(){
    Example myTester = new Example();
    MyTester.setTestNumber(4);
    System.out.println(MyTester.getTestNumber());
    return myTester;
}

By the way:顺便一提:

  • MyTester is not a private variable. MyTester不是私有变量。 It is a local variable.它是一个局部变量。 Local variables don't / cannot have an access modifier.局部变量没有/不能有访问修饰符。 Calling them "private" is technically incorrect, and confusing.称它们为“私有”在技术上是不正确的,并且令人困惑。 (For others, and probably for you as well.) (对于其他人,可能也适用于您。)

  • MyTester and TheTestExample are egregious violations of Java style conventions. MyTesterTheTestExample严重违反了 Java 风格约定。 Method and variable names should always start with a lower case letter.方法和变量名称应始终以小写字母开头。

In your code your are creating 2 objects for Test class one inside main method and another on in TheTestExample() method.在您的代码中,您正在为 Test 类创建 2 个对象,一个在 main 方法中,另一个在 TheTestExample() 方法中。 And you are setting value 4 in side TheTestExample () for the private variable.并且您正在 TheTestExample () 侧为私有变量设置值 4。

If you want to print the value which was set in TheTestExample() in main () you can return the newly set value from this method.如果您想打印在 main() 中 TheTestExample() 中设置的值,您可以从此方法返回新设置的值。 public class Example {公共类示例{

private int testNumber;

public static void main(String[] args) {
    Example tester = new Example();
    System.out.println(tester.getTestNumber());


    **System.out.println(tester.TheTestExample());** // it prints the value return from the method
}

public int getTestNumber(){
    return testNumber;
}

public void setTestNumber(int x){
    this.testNumber = x;
}

public int TheTestExample(){
    Example MyTester = new Example();
    MyTester.setTestNumber(4);
    System.out.println(MyTester.getTestNumber());
    **return MyTester.getTestNumber();**
}

} }

public class Example {
private int testNumber;

public static void main(String[] args) {
    Example tester = new Example();
    System.out.println(tester.getTestNumber()); //prints "0"
    tester.TheTestExample(); //print "4"
    System.out.println(tester.getTestNumber()); //print "0" Why? ;) See the method TheTestExample -> print4() for now it is a better name
}

public int getTestNumber(){
    return testNumber;
}

public void setTestNumber(int x){
    this.testNumber = x;
}

public void TheTestExample(){
    Example MyTester = new Example(); //local instance, @Answer we lost  this object. If you want to change testNumber here. If you want to have access to this object please return and use in main method 
    MyTester.setTestNumber(4); //setFour
    System.out.println(MyTester.getTestNumber()); //result in line 8
}

} }

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

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