1

Source:

import java.util.Scanner;

class Test1{
    public static void main(String[] args){
        int x,y,ir;
        x = 10;
        y = 5;
        Scanner scan = new Scanner(System.in);
        ir = scan.nextInt();

        switch(ir){
            case 1: y++;
                break;
            case 2: y--;
                break;
            case 3: x++;
                break;
            case 4: x--;
                break;
        }
    }
}

I'd like to repeat this part several times:

switch(ir){
    case 1: y++;
        break;
    case 2: y--;
        break;
    case 3: x++;
        break;
    case 4: x--;
        break;
}

But I don't wanna write it more than once. That's why I'd like write it like a procedure or a method. Is there any way to write a method which can modify a variable in my main method?

greg-449
  • 109,219
  • 232
  • 102
  • 145
  • Possible duplicate of http://stackoverflow.com/questions/4600974/passing-primitive-data-by-reference-in-java? – Jashaszun Aug 14 '14 at 22:54
  • 1
    No, you can't put one method inside another (short of some weird things with anonymous classes). However, the *class* that contains the *main* method can have as many other methods as one wishes. – Hot Licks Aug 14 '14 at 22:54
  • An alternative solution is to make `x` and `y` static fields so that you can modify them from another static method in `Test1` that you call from `main`. – Jashaszun Aug 14 '14 at 22:55
  • No, you can't create a "procedure in the main method", but you could create a static method within Test1 class wch can be called from the main method – MadProgrammer Aug 14 '14 at 22:56
  • (The important thing to note here is that `main` is simply a particular method in a class (the `Test1` class in your case). That class is like any other class and can contain other methods.) – Hot Licks Aug 14 '14 at 23:54
  • 1
    Please don't vandalize your questions – greg-449 May 13 '17 at 06:38

3 Answers3

3

In your case you can't since you are working with primitive types. If you use a reference type, however, you can modify its contents when passing it to another method.

The solution to your problem is to either define your variables on a higher level (which would be instance scope here) or to pass a wrapper type to your method (for example int has the corresponding reference type Integer). The latter approach is something I would not suggest you to do though.

Another solution, which is perhaps more interesting, is to just wrap it in a loop inside your main method. It is not entirely clear to me what you are trying to do but you should definitely see if it can be solved like this.

Note that you are working from your main method so any method you want to call would have to be static. Proper code wouldn't do this but instead work with an object and instance methods.

But no, you cannot nest methods.

Jeroen Vannevel
  • 43,651
  • 22
  • 107
  • 170
2

You can do that by creating a class inside your main method which provides that functionality.

A quick example:

class Test1 {
    public static void main(String[] args) {
        int x = 0;
        int y = 0;        

        class MyClass {
            public int x;
            public int y;

            MyClass() {
                x = 0;
                y = 0;
            }

            public void doSomething() {
                x++;
                y++;
            }
        }

        MyClass myClass = new MyClass();

        myClass.doSomething();
        myClass.doSomething();
        myClass.doSomething();

        x = myClass.x;
        y = myClass.y;
    }
}
Gio
  • 3,242
  • 1
  • 25
  • 53
  • I agree, it's not a beautiful solution and best avoided. – Gio Aug 14 '14 at 23:13
  • Huh, I didn't know you could create a class like this inside a method. I know about anonymous inner classes of course, but this was a new one for me. – Simon Forsberg Aug 16 '14 at 10:28
0

It's working with static variables. My form:

import java.util.Scanner;

class a{
    public static int x = 10;
    public static int y = 10;


    public static void lep(int ir){
        switch(ir){
            case 1: y++;
                break;
            case 2: y--;
                break;
            case 3: x++;
                break;
            case 4: x--;
                break;
        }
    }

    public static void main(String[] args){
        Scanner scan = new Scanner(System.in);
        int ir = scan.nextInt();
        lep(ir);
        System.out.println("x: " + x + " y: " + y);

    }
}

Solved. Thank you very much!

  • That's not exactly what you asked for, but I'm glad you figured it out. – Isaiah van der Elst Aug 14 '14 at 23:17
  • The aim of the question was: how can I modify a variable which is in my main method? Sorry if the question was bad or not understandable, but you could also help me. –  Aug 14 '14 at 23:25