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

}

```

}

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

```
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