Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Does reflection always give the correct information re anonymous classes?  RSS feed

 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5
An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

If I try and test that using reflection


output:

reflection says that an anonymous class can be static.

So, is the information from reflection not always correct, or I am I misunderstanding what's going on here?
 
Mark Spencers
Ranch Hand
Posts: 51
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anonymous class is created with keyword new, name of interface or class. Example:
 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mark Spencers wrote:Anonymous class is created with keyword new, name of interface or class

Yep, that's what the code I posted does.
It creates an anonymous implementation of the ReportMyself interface.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Hayward wrote:
reflection says that an anonymous class can be static.


Hmmm.... I always wondered about this ... have a cow for looking into it...

Anyway, read the specification again. The specification says that the anonymous inner class (and method inner classes too) can't be declared as static, as that section is for declarations.

If an anonymous inner class is declared in an instance method, it is obviously an inner class, and it's outer class is the instance containing the method... but .... if the anonymous inner class is declared in a static method, it just works too -- even though, technically, there isn't an outer class instance.

I had meant to look into this, but never got around to it. I guess the reflection API can be used to distinguish where it was declared. And whether there is an outer class instance. Good to know.

Henry
 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your observations Henry.

Henry Wong wrote:If an anonymous inner class is declared in an instance method, it is obviously an inner class, and it's outer class is the instance containing the method

Yes, I can demonstrate that:
output:

Henry Wong wrote: if the anonymous inner class is declared in a static method, it just works too -- even though, technically, there isn't an outer class instance.

Well, it works in the way I'd expect a static class to work. Reflection says it's static and the compiler will prevent you from accessing any non-existent outer this instance:

Guess I'd have no reason to feel unsettled if the compiler required one to declare an anonymous class declared in a static context as static, but it doesn't and attempting to do so isn't allowed syntax.

If I'm understanding you correctly, I can see the jls says you can't declare an anonymous class as static, which isn't quite the same as saying that the object that gets created actually is static. Think I'll classify this behaviour as a one-off oddity.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Hayward wrote:Thanks for your observations Henry.


And thank you for confirming everything ... have a couple of cows.

Henry
 
Richard Hayward
Ranch Hand
Posts: 176
11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just as a matter of interest, the same is true for the issue as to if an anonymous class is final.

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.9.5
An anonymous class is always implicitly final (§8.1.1.2).


Reflection says that an anonymous class, at least in this situation, is not final:


output:

Although I can't find any syntax, that I can sneak past the compiler, enabling me to sub-class the anonymous sub-class of X.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!