Jayden Freh
Jayden Freh

Reputation: 99

Make a variable accessible by all methods

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.

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(){



}

}

Upvotes: 0

Views: 34042

Answers (4)

Walter
Walter

Reputation: 279

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.

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

Upvotes: 1

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

    }
}

Upvotes: 1

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85799

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

Since you're learning, it will be better to not use static fields nor methods except for the main method.

Upvotes: 3

Malik Brahimi
Malik Brahimi

Reputation: 16721

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

Upvotes: 5

Related Questions