Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Java support inner / local / sub methods?

Tags:

java

methods

This is my code.

public class SubFunction {     private String drawTribleX(){         return trible("X");     }     private String trible(String t){         return t + t + t;     }     public static void main(String[] args){         SubFunction o = new SubFunction();         System.out.println(o.drawTribleX());     } } 

Can I do something like this ?

public class SubFunction {     private String drawTribleX(){         // *** move trible(t) inside drawTribleX() ***         private String trible(String t){             return t + t + t;         }         return trible("X");     }     public static void main(String[] args){         SubFunction o = new SubFunction();         System.out.println(o.drawTribleX());     } } 

Thank you.

like image 444
diewland Avatar asked Mar 22 '11 08:03

diewland


People also ask

Does Java support inner methods?

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.

Can we write method inside main method Java?

No, you can't declare a method inside another method. If you look closely at the code you provided it is just a case of bad formatting, the main method ends before the max method is declared. He can however take the code from the method and put it in the main method.

What is a local method in Java?

In Java, we can write a class within a method and this will be a local type. Like local variables, the scope of the inner class is restricted to the method. A method-local inner class can be instantiated only within the method where the inner class is defined.

How do you call a method within a method in Java?

Call a Method Inside main , call the myMethod() method: public class Main { static void myMethod() { System.out.println("I just got executed!"); } public static void main(String[] args) { myMethod(); } } // Outputs "I just got executed!"


1 Answers

Update 2014-02-09:

JDK 8 introduced lambdas (anonymous function expressions) which allow you to solve it like this:

Function<String, String> trible = s -> s+s+s; System.out.println(trible.apply("X"));           // prints XXX 

(JDK 7 and below)

No, Java does not support "directly" nested methods. (Most functional languages do though, including some JVM languages such as Scala and Clojure!)

Just for reference though; You can define local classes (classes within methods) so this does compile

class SubFunction {     private String drawTribleX(){          // *** move trible(t) inside drawTribleX() ***         class Trible {             private String trible(String t){                 return t + t + t;             }         }          return new Trible().trible("X");     }     public static void main(String[] args){         SubFunction o = new SubFunction();         System.out.println(o.drawTribleX());     } } 

Note though that there are some restrictions on local classes

3.11.2. Restrictions on Local Classes

Local classes are subject to the following restrictions:

  • A local class is visible only within the block that defines it; it can never be used outside that block.

  • Local classes cannot be declared public, protected, private, or static. These modifiers are for members of classes; they are not allowed with local variable declarations or local class declarations.

  • Like member classes, and for the same reasons, local classes cannot contain static fields, methods, or classes. The only exception is for constants that are declared both static and final.

  • Interfaces cannot be defined locally.

  • A local class, like a member class, cannot have the same name as any of its enclosing classes.

  • As noted earlier, a local class can use the local variables, method parameters, and even exception parameters that are in its scope, but only if those variables or parameters are declared final. This is because the lifetime of an instance of a local class can be much longer than the execution of the method in which the class is defined. For this reason, a local class must have a private internal copy of all local variables it uses (these copies are automatically generated by the compiler). The only way to ensure that the local variable and the private copy are always the same is to insist that the local variable is final.

So, as you can see, your first option (without nested methods) is preferable in these situations.

like image 72
aioobe Avatar answered Sep 22 '22 20:09

aioobe