java 使变量可被所有方法访问

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31189136/
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-11-02 18:15:05  来源:igfitidea点击:

Make a variable accessible by all methods

javavariablesmethods

提问by Jayden Freh

I'm a bit new to java and i recently learned about methods(so cool!). I want to know if its possible to declare a variable in my main method and use it in my other methods.

我对 Java 有点陌生,我最近学习了方法(太酷了!)。我想知道是否可以在我的 main 方法中声明一个变量并在我的其他方法中使用它。

What i am trying to do is create a calculator using methods(just for practice with this new concept) but i dont want to declare the variable every time in each method.

我想要做的是使用方法创建一个计算器(仅用于练习这个新概念),但我不想每次在每种方法中都声明变量。

Here is the skeletal structure of the code:

这是代码的骨架结构:

class GS1{



public static void main (String[]args){
    Scanner input = new Scanner(System.in);
    System.out.println("Enter the math operation to be completed: ");
    String opt = input.nextLine();
    int x,y;  // I tried declaring variables here
    switch(opt){

    case  "addition" :
    // addition method goes here
    break;
    case "subtraction":
    //subtraction method goes here
    break;
    case "multiplication":
    //multiplication method   goes  here
    break;
    case "division":
    //division method goes here
    break;
    }

}

static void addition(){
    System.out.println("Enter first value for addition");
    x=input.nextint(); // i get error stating both "x" and "input" cannot be resolved as a variable


}

static void subtration(){


}

static void Multiplication(){

}

static void Division(){



}

}

}

回答by Malik Brahimi

You should place the variable outside of all methods but within the class, creating global access.

您应该将变量放置在所有方法之外但在类中,从而创建全局访问。

public class ClassName
{
    public int x;
    public int y;

    public void method1()
    {
        x = 3;
    }

    public void method2()
    {
        y = 1;
    } 
}

回答by Luiggi Mendoza

Move the variable at class level, make it a field in the class.

在类级别移动变量,使其成为类中的一个字段。

Since you're learning, it will be better to not use staticfields nor methods except for the mainmethod.

既然你在学习,那么static除了方法之外,最好不要使用字段和方法main

回答by Alejandro Agapito Bautista

Organize better your code, make something like the following code:

更好地组织您的代码,制作类似于以下代码的内容:

class Operation {

    public double addition(double... value) {
        double result = 0;
        for (double i : value) {
            result += i;
        }
        return result;
    }

    public double subtration(.....) {
        // .........................
        return 0.0;
    }

    public double multiplication(.....) {
        // .........................
        return 0.0;
    }

    public double division(.....) {
        // .........................
        return 0.0;
    }
}

public class GS1 {

    public static void main(String[] args) {
        Operation operations=new Operation();


        //read value code 
        double result=operations.addition(value);

        //print result code

    }
}

回答by Walter

You need something like this:

你需要这样的东西:

    class GS1 {

        public static int x;
        public static Scanner input;

        public static void main(String[] args) {
            input = new Scanner(System.in);
            System.out.println("Enter the math operation to be completed: ");
            String opt = input.nextLine();
            int x, y; // I tried declaring variables here
            switch(opt){

            case  "addition" :
            // addition method goes here
            break;
            case "subtraction":
            //subtraction method goes here
            break;
            case "multiplication":
            //multiplication method   goes  here
            break;
            case "division":
            //division method goes here
            break;
            }
        }

        static void addition() {
            System.out.println("Enter first value for addition");
             x=input.nextint(); // i get error stating both "x" and "input" cannot
            // be resolved as a variable

        }

        static void subtration() {

        }

        static void Multiplication() {

        }

        static void Division() {

        }

    }

Remember to use "static" modifier in your field declaration (x and input), you cannot make a static reference to a non static field.

请记住在您的字段声明(x 和输入)中使用“静态”修饰符,您不能对非静态字段进行静态引用。

A better way would be using objects instead of put all your methods in a single class (GS1). For example, create a Calculator class like Marged suggest in your comments

更好的方法是使用对象而不是将所有方法放在一个类 (GS1) 中。例如,在您的评论中创建一个像 Marged 建议的 Calculator 类