Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

methods

for example and is this legal:

class NAME {
method {
     method {} 
}
} 

and what would the effect be? is there any specialy syntax involved?

like image 985
David Avatar asked Apr 14 '10 01:04

David


People also ask

Can methods be nested in Java?

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.

What is a nested method?

What is a nested method. If a method is defined inside another method, the inner method is said to be nested inside the outer method or it is called Nested method. All languages do not support nesting a method inside another method but python allows you to do so.

Can methods in all nested classes can be declared static?

Methods in all nested classes can be declared static. All nested classes can be declared static. Static member classes can contain non-static methods.

What happens when a method is called in Java?

The current method call halts. The arguments of the newly called method are pushed to the stack. The method code runs. After the method finished running, the stack is again emptied and the old stack contents is again restored.


5 Answers

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

like image 120
OscarRyz Avatar answered Oct 04 '22 04:10

OscarRyz


That is invalid syntax; no such feature is supported. (Although it's being considered for Java 7)

Instead, you can use nested classes, which you just asked about.

like image 31
SLaks Avatar answered Oct 04 '22 02:10

SLaks


No.

One solution is just to declare the methods you want to call as private methods outside the "parent" method-- if you were really bothered, you could use some naming convention to indicate that they "belong" to the "parent" method.

Another thing that you could consider-- and doesn't appear to be widely known among programmers-- is that you can declare any arbitrary scope block and label it, then use break to break out of that block.

So the following is perfectly legal Java:

void method() {
      myInnerMethod : {
        // do some stuff

        if (condition) {
            break myInnerMethod;
        }

        // do some more stuff
      }
}

Of course, the scope block is not really a method, but in some cases, it can be used to do what you'd want an "inner method" for.

like image 45
Neil Coffey Avatar answered Oct 04 '22 04:10

Neil Coffey


No. It is invalid syntax. And that's a shame - it is one of the things I miss from Ada. Not having the ability to define nested methods does create a lot of problems in organizing classes with substantial amounts of private methods. From there it's a slippery slope into lack-of-cohesion land.

You could use nested classes, but they come at a price. Each nested class generates a $named class. Each has a toll in terms of open file handles (if not pulled from an archive) as well memory taken by its class definition. Some systems have a cap on the number of files (thus total generated classes) that they can deploy (Google Apps for example.)

In other words, I would not use nested classes to mimic nested methods (just in case you decide to try that.)

Now, assuming that you could use nested methods (as in Ada), those methods would only be visible within the enclosing method (they'd be more restricted than typical private methods.) They would be able to see variables and parameters defined in the outer scope (but probably only if defined as final.)

It would allow you to organize your methods and algorithms in nested namespaces. When used intelligently, nested methods really help in organizing your code.

like image 45
luis.espinal Avatar answered Oct 04 '22 04:10

luis.espinal


Without checking, I'd say that this isn't accepted by the compiler because as of this time methods need to be defined within a class. However, you could define an inner class inside the method (see "Local and Anonymous Inner Classes").

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html

like image 45
James P. Avatar answered Oct 04 '22 02:10

James P.