for example and is this legal:
class NAME {
method {
method {}
}
}
and what would the effect be? is there any specialy syntax involved?
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. 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.
Methods in all nested classes can be declared static. All nested classes can be declared static. Static member classes can contain non-static methods.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With