Can methods in java be nested and what is the effect? [closed]

UPDATE
Since Java 8 methods can be nested using lambdas, see this other question.

This answer is valid for Java versions prior to Java 8

Original answer follow:

Can methods in java be nested[…]?

No, that’s not possible.

The closest thing you can get is:

class Name {
    void methodOne() {
        class InnerClass {
           void methodTwo() {
           }
         }
     }
 }

That is, a second method defined in a inner class defined in a method.

You can declare the method static inside the inner class, so you do not have to call new

Leave a Comment