如何在java中的方法之间传递变量?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19109390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:11:09  来源:igfitidea点击:

How do I pass variables between methods in java?

javavariablesmethods

提问by Victoria Martell

In the main method of my program I have a bunch of scanner input which I have passed into various methods using parameters. In those various methods I have done calculations, creating new variables. In my final method I need to add those new variables together, but the compiler will not recognize the new variables because they only exist in those other methods. How would I go about passing the new variables to my final method?

在我的程序的主要方法中,我有一堆扫描仪输入,我使用参数将它们传递给了各种方法。在这些不同的方法中,我进行了计算,创建了新变量。在我的最终方法中,我需要将这些新变量添加在一起,但编译器不会识别新变量,因为它们只存在于其他方法中。我将如何将新变量传递给我的最终方法?

回答by SpringLearner

Instead of creating local variables create class variables.The scope of class variables is that they are global meaning you can access those variables anywhere in the class

不是创建局部变量而是创建类变量。类变量的范围是它们是全局的,这意味着您可以在类中的任何位置访问这些变量

回答by Suresh Atta

Variables created in methods are local to methods and scopeis restricted to methods only.

在方法中创建的变量是方法的局部变量,作用域仅限于方法。

So go for instance members, which you can share among methods.

所以去instance members,你可以在方法之间共享。

If you declare so, you don't need to pass them among methods, but you can access and update those members in methods.

如果这样声明,则不需要在方法之间传递它们,但可以在方法中访问和更新这些成员。

Consider,

考虑,

public static void main(String[] args) {
    String i = "A";
    anotherMethod();
}

You get a compiler error in the below method if you try to access i, because iis a local variable of the main method. You cannot access in other methods.

如果您尝试访问i,则会在以下方法中遇到编译器错误,因为i是 main 方法的局部变量。您无法通过其他方式访问。

public static void anotherMethod() {
    System.out.println("    " + i);
}

What you can do is, pass that variable to where you want.

您可以做的是,将该变量传递到您想要的位置。

public static void main(String[] args) {
    String i = "A";
    anotherMethod(i);
}

public static void anotherMethod(String param){
    System.out.println("    " + param);
}

回答by yegorich

You can create a Listand pass it to each method as parameter. At the end all you need is to iterate through the list and handle the results.

您可以创建一个List并将其作为参数传递给每个方法。最后,您只需要遍历列表并处理结果。

回答by Alexander

Create an object with new variables and the method returning their sum. In your methods use this object for new variables calculation. Then use the object method for getting sum of new variables

创建一个带有新变量的对象和返回它们总和的方法。在您的方法中,使用此对象进行新变量计算。然后使用对象方法获取新变量的总和

回答by Wesley Egbertsen

You could do something like this:

你可以这样做:

public void doStuff()
{
    //Put the calculated result from the function in this variable
    Integer calculatedStuff = calculateStuff();
    //Do stuff...
}

public Integer calculateStuff()
{
    //Define a variable to return
    Integer result;

    //Do calculate stuff...
    result = (4+4) / 2;

    //Return the result to the caller
    return result;
}

You also could do this(You can then retrieve the variable calculatedStuff in any function in the class):

你也可以这样做(然后你可以在类中的任何函数中检索变量calculatedStuff):

public class blabla {

    private Integer calculatedStuff;

    public void calculateStuff()
    {
        //Define a variable to return
        Integer result;

        //Do calculate stuff...
        result = (4+4) / 2;

        //Return the result to the caller
        this.calculatedStuff = result;
    }

}

But as everyone else suggest, I also higly recommend to do a basic Java tutorial.

但正如其他人所建议的那样,我也强烈建议您做一个基本的 Java 教程。