Java Nested Methods

+1 vote
obj.anymethod().anyothermethod()

how we can call  anyothermethod after calling anymethod that class

because we have no method in method
Aug 20, 2019 in Java by anonymous
13,009 views

2 answers to this question.

0 votes

JAVA does not support “directly” nested methods, but you can still achieve nested method functionality in Java 7 or older version by defining local classes, class within method so that it will compile. In java 8 and newer version you achieve it by lambda expression. 

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.

// Java program implements method inside method
public class Hello {
interface myInterface {        //create a local interface with one abstract method run()
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("Hello");};
  }; 
r.run(); 
  } 
public static void main(String[] args) { 
Foo(); 
  } 
} 

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.

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

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.

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

Hope this helps!

Check out this Java Course to learn more.

Thanks!

answered Aug 20, 2019 by Sirajul
• 59,230 points

edited Jul 5, 2023 by Khan Sarfaraz
+1 vote

public class ObjClass {

    ObjClass anyMethod() {

        System.out.println("anyMethod()");

        return this;

    }

    void anyOtherMethod() {

        System.out.println("anyOtherMethod()");

    }

    public static void main(String[] args) {

         ObjClass obj = new ObjClass();

          obj.anyMethod().anyOtherMethod();

     }

}

answered Jan 19, 2020 by zemiak

Related Questions In Java

0 votes
2 answers

When to use Static Methods in Java?

A static method has two main purposes: For ...READ MORE

answered Aug 10, 2018 in Java by samarth295
• 2,220 points
15,729 views
0 votes
1 answer

Can Static methods be inherited in java?

A subclass inherits all of the public ...READ MORE

answered Apr 30, 2018 in Java by Daisy
• 8,120 points
1,735 views
0 votes
2 answers

How to break the nested loop in Java?

You can use break with a label for the ...READ MORE

answered Sep 20, 2018 in Java by Sushmita
• 6,910 points
1,042 views
0 votes
1 answer

Java Static nested class

Hi, to understand their usage, you must ...READ MORE

answered Jun 8, 2018 in Java by v.liyyah
• 1,300 points
573 views
0 votes
1 answer

Static methods can't be abstract in Java

Well, Java doesn't allow this because of ...READ MORE

answered Oct 12, 2018 in Java by v.liyyah
• 1,300 points
933 views
+5 votes
4 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,450 points

edited Nov 7, 2018 by Omkar 80,295 views
+1 vote
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,500 points
8,099 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points
17,184 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,500 points

edited Aug 4, 2023 by Khan Sarfaraz 4,541 views
0 votes
6 answers

How can we define global variables in java?

To define Global Variable you can make ...READ MORE

answered Dec 15, 2020 in Java by Gitika
• 65,910 points
116,570 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP