Method within method in java

Java does not support “directly” nested methods. Many functional programming languages support method within method. But you can achieve nested method functionality in Java 7 or older version by define local classes, class within method so this does compile. And in java 8 and newer version you achieve it by lambda expression. Let’s see how it achieve.

Method 1 (Using anonymous subclasses)
It is an inner class without a name and for which only a single object is created. An anonymous inner class can be useful when making an instance of an object with certain “extras” such as overloading methods of a class or interface, without having to actually subclass a class. for more in detail about anonymous inner class click here

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program implements method inside method
public class GFG {
  
    // create a local interface with one abstract
    // method run()
    interface myInterface {
        void run();
    }
  
    // function have implements another function run()
    static void Foo()
    {
        // implement run method inside Foo() function
        myInterface r = new myInterface() {
            public void run()
            {
                System.out.println("geeksforgeeks");
            };
        };
        r.run();
    }
    public static void main(String[] args)
    {
        Foo();
    }
}

chevron_right


Output:

geeksforgeeks

Method 2 (Using local classes)
You can also implement a method inside a local class. A class created inside a method is called local inner class. If you want to invoke the methods of local inner class, you must instantiate this class inside method.

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program implements method inside method
public class GFG {
  
    // function have implementation of another 
    // function inside local class
    static void Foo()
    {
        // local class
        class Local {
            void fun()
            {
                System.out.println("geeksforgeeks");
            }
        }
        new Local().fun();
    }
    public static void main(String[] args)
    {
        Foo();
    }
}

chevron_right


Output:

geeksforgeeks

Method 3 (Using a lambda expression)
Lambda expressions basically express instances of functional interfaces (An interface with single abstract method is called functional interface. An example is java.lang.Runnable). lambda expressions implement the only abstract function and therefore implement functional interfaces. for more about expression click here

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java program implements method inside method
public class GFG {
    interface myInterface {
        void run();
    }
  
    // function have implements another function
    // run() using Lambda expression
    static void Foo()
    {
        // Lambda expression
        myInterface r = () ->
        {
            System.out.println("geeksforgeeks");
        };
        r.run();
    }
    public static void main(String[] args)
    {
        Foo();
    }
}

chevron_right


Output:

geeksforgeeks


My Personal Notes arrow_drop_up

Strategy Path planning and Destination matters in success No need to worry about in between temporary failures

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.