-1

It might sound like a dumb question, but all anonymous classes must be defined and instantiated within an existing class; therefore, they must be inner classes at the same time.

user2864740
  • 60,010
  • 15
  • 145
  • 220
OnTheFly
  • 2,059
  • 5
  • 26
  • 61

2 Answers2

3

Anonymous classes are types of inner classes. See http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html

Quoting from there:

"You can also declare an inner class within the body of a method without naming the class. These classes are known as anonymous classes."

Menelaos
  • 23,508
  • 18
  • 90
  • 155
  • 1
    Looking at some Java docummentation, yes: **An anonymous class is always an inner class** (§8.1.3) – 3yakuya Dec 26 '13 at 00:57
  • @maythesource.com - I think the answer to that is pretty clear, judging from his other questions .... – Stephen C Jan 08 '14 at 03:33
0

This is quite true. Your anonymus class could not be implemented outside of other classes, as a seperate class, because as it is anonymus you would not be even able to refer to it in any way.

Additional information: From JLS: An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

3yakuya
  • 2,622
  • 4
  • 25
  • 40
  • 1
    That doesn't answer the question, unless you're incorrectly assuming that all nested classes are inner classes. It isn't the same thing. No citation either. – user207421 Jan 03 '14 at 18:44
  • The question is: are anonymus classes always inner classes, and the docummentation says directly: "An anonymous class is always an inner class ", what is wrong with my answer? – 3yakuya Jan 03 '14 at 21:52
  • Anonymus classes are always not only nested classes, but inner classes, as they can't be static according to Java Language Specification. I therefore believe my answer does answer the question and my example gives some intuition about why they must be defined inside other classes (I did not write about them always being static, but this does not change the intuition, I believe). Is my logic incorrect? – 3yakuya Jan 03 '14 at 22:21
  • 1
    Your answer didn't say any of those things, and didn't cite the relevant parts of the JLS. It only said an anonymous class is a nested class, which we knew already, and appeared to conflate inner and nested, as I said above. – user207421 Jan 03 '14 at 23:11
  • My answer said "This is true" on question "are anonymus classes a subset of inner classes" and gave an intuition about why it must be implemented inside another class to make sense. In the question author stated: "but all anonymous classes must be defined and instantiated within an existing class" and I confirmed it giving example. How does this not answer the question? ("which we knew already" is not really true, as the question was nearly about it). – 3yakuya Jan 03 '14 at 23:15
  • 3
    Your answer said 'true', which is correct, but the explanation you gave also applies to static classes, which are not inner, so it was not correct. The correct explanation is what it says in the JLS. – user207421 Jan 04 '14 at 04:42