30

for example and is this legal:

class NAME {
method {
     method {} 
}
} 

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

David
  • 14,569
  • 34
  • 78
  • 107
  • 3
    invalid stuff! I am risking negative voting : what are you looking for? just SO votes? What is this question about? what are you trying to achieve? – ring bearer Apr 14 '10 at 01:44
  • 6
    @EJP, the compiler will only give me the correct answer if the compilers answer is yes. I could try it and it could not work but the syntax could be wrong, i could be in error. thered be no way of knowing. – David Apr 14 '10 at 20:09
  • 3
    I'm sorry but that is a meaningless quibble. You asked whether it is legal. The compiler answers that either in the negative or the affirmative. – user207421 Apr 15 '10 at 08:24
  • 1
    see my previous response to that complain. If i try it and teh compiler says "no" it might not mean "no". It could eather mean "no" or "you have a syntax error". The only condition under which the compiler can give me a definate answer is if it says yes and everything works how i expect it to. – David Apr 15 '10 at 13:29
  • If the compiler says 'no' it means 'no'. If you have other syntax errors, I suggest you remove them first. – user207421 Apr 26 '10 at 03:02
  • No, I am not 'incapable of understanding your point'. I am telling you that it doesn't make sense. There is a difference, and casting aspersions on my intelligence doesn't make it go away. If you have syntax errors, remove them. If the last remaining syntax error is the construction you are testing, it is a syntax error. If there are no errors it isn't a syntax error. – user207421 May 02 '10 at 04:52
  • 1
    right but i don't know, in this case, wheather the error the compiler gives me is due to a syntax error or the lack of a feature. Consider mythical program language X. if i try to write 'int b = 43' and it gives me an error then one of two things are true: eather 1) i have a syntax error or 2) X doesn't suport integer variables. Theres no way for me to know from the presence of an error which of these cases is true. So if the compiler says "no" it might mean "no" or it might mean "wrong". – David Jun 10 '10 at 17:59
  • Another pointless quibble. The question you actually asked is a syntax question, period. Your new example isn't helpful. Either 'int b = 43' is legal syntax or it isn't. If it is illegal, *why* is irrelevant. – user207421 Sep 20 '13 at 04:30
  • I get why he's asking that. In python, you could nest functions, and call a recursive function inside a function. I was trying to do that in java, and stumbled across here. – Yolomep Oct 09 '20 at 11:19

6 Answers6

47

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

Community
  • 1
  • 1
OscarRyz
  • 196,001
  • 113
  • 385
  • 569
  • You have to remove the parenthesis on the inner class declaration: `class InnerClass {` – Mindwin Remember Monica Feb 13 '13 at 11:26
  • 2
    Yes this is old and closed but for the readers' sake, you can't declare static members in an inner class because they need an instance of the outer class. You can't avoid the new operator. – 1der Sep 05 '13 at 03:07
  • Java 8 provides a good syntax sugar for this. http://stackoverflow.com/a/28576581/354144 – Neal Ehardt Feb 18 '15 at 05:36
14

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.

Community
  • 1
  • 1
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
8

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.

Neil Coffey
  • 21,615
  • 7
  • 62
  • 83
  • 2
    +1, I never knew that, but I just verified it... I guess you can't call the block, but as pointed out above you can jump to it. (In not really sure what the uses of this feature are, TBH.) – Edmund Apr 14 '10 at 01:49
  • @Edmund you cannot **jump to** labelled blocks. You can only **break out of** them. Which makes them next to useless as ersatz methods. – Stephen C Apr 14 '10 at 04:56
  • @Stephen -- thanks for the correction. I've never used labelled breaks and kind of assumed they meant "break to this label" and not "break out of this label". – Edmund Apr 14 '10 at 06:38
  • 1
    They're not VERY useful, but *occasionally* they're useful where otherwise a method would be the most logical solution (imagine a method containing e.g. loop and you want to return from the method inside the loop). There are one or two examples inside the JDK source if you look carefully. – Neil Coffey Apr 15 '10 at 00:24
  • Is there any way to reuse them one them one time inside any method? – Arefe May 24 '18 at 14:17
4

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.

luis.espinal
  • 10,331
  • 6
  • 39
  • 55
2

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

James P.
  • 19,313
  • 27
  • 97
  • 155
0

Not accepted in Java. Looks like you want to do recursion, simply call the same method again.

method(){

  method();

}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
flopex
  • 434
  • 3
  • 6
  • 11