1

I am learning java. I am getting a lot of errors when I try to keep the methods, which I am trying to invoke, inside the main method.

I am trying to declare a couple of variables x,y. However, I want to invoke the math operations when specific methods are called, such as addMethod, subtractMethod, so on.

When I try to include the methods within the public method, I am getting an error.

 package exampleclass;

public class MathLearning {
    //declaring variables
    static int x = 9;
    static int y = 2;

    public static void main(String[] args) {

        int resu=0;
        additMethod(resu);
        subtMethod(resu);
        multMethod(resu);
        divMethod(resu);    

    private static void divMethod(int resu) {
        resu = x+y;
        System.out.println(resu);

    }

    private static void multMethod(int resu) {
        resu = x-y;
        System.out.println(resu);

    }

    private static void subtMethod(int resu) {
        resu = x*y;
        System.out.println(resu);

    }

    private static void additMethod(int resu) {
        resu = x/y;
        System.out.println(resu);

    }

    }
}

When I keep the methods outside the main method, I do not get an error.

    package exampleclass;

public class MathLearning {
    //declaring variables
    static int x = 9;
    static int y = 2;

    public static void main(String[] args) {

        int resu=0;
        additMethod(resu);
        subtMethod(resu);
        multMethod(resu);
        divMethod(resu);


    }

    private static void divMethod(int resu) {
        resu = x+y;
        System.out.println(resu);

    }

    private static void multMethod(int resu) {
        resu = x-y;
        System.out.println(resu);

    }

    private static void subtMethod(int resu) {
        resu = x*y;
        System.out.println(resu);

    }

    private static void additMethod(int resu) {
        resu = x/y;
        System.out.println(resu);

    }


}
2
  • 2
    You can't have methods inside methods. Commented May 1, 2014 at 17:35
  • For future reference, you could have easily figured this out by searching google for why can't I put a method inside another method java. You will save a lot of time in the future if you learn how to ask good questions on google. Commented May 1, 2014 at 17:38

2 Answers 2

5

When I try to include the methods within the public method, I am getting an error.

You cannot have nested methods in Java1. Indeed, your second snippet has the methods in an appropriate location; outside of main.


1 (Aside) Well, I guess you technically can since you can declare classes within methods, meaning you can ultimately declare a method within another method:

void foo1() {
    class X {
        void foo2() {
            ...
        }
    }
    ...
}

You should very rarely have to do something like that, though.

Sign up to request clarification or add additional context in comments.

1 Comment

+1 I would be very suprise to see a real use case for this kind of situations
2

In java, you cannot declare methods inside other methods (like you can, e.g., in ). Methods are defined under classes, like you do in your second code example.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.