Declaration of a method inside another | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 4

Declaration of a method inside another

can we declare a method inside another method?

10th Mar 2018, 12:15 PM
De Vinci
4 Answers
+ 19
U cannot define a method inside a method in JAVA but what U can do is to make method call() to another method or same method inside a method //thats why recursion works
10th Mar 2018, 12:26 PM
Gaurav Agrawal
Gaurav Agrawal - avatar
10th Mar 2018, 12:27 PM
Hannah Grace
Hannah Grace - avatar
+ 4
No, that would be illegal. The closest you can get are anonymous inner classes: https://stackoverflow.com/questions/355167/how-are-anonymous-inner-classes-used-in-java
10th Mar 2018, 12:23 PM
apex137
apex137 - avatar
+ 2
No it will generate a error because methods cannot be nested. they are defined separately and compile together.
31st Mar 2018, 3:31 PM
Kisa Batool