Can methods in java be nested and what is the effect? [closed]
for example and is this legal:
class NAME {
method {
method {}
}
}
and what would the effect be? is there any specialy syntax involved?
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
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.
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.
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.
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
Not accepted in Java. Looks like you want to do recursion, simply call the same method again.
method(){
method();
}
精彩评论