• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Anonymous Inner Class Question

 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

I don't understand something I found this out when I didn't understand the answer to one of the SCJP 1.6 Study Guide, I read through the text but it's not clear to me still.



So, I don't understand this: InnerFun s = new InnerFun(){String s;};
I simply don't understand what the {String s;} bit means.

There's no String member in the innerfun class it can represent, it's not a method override, and it also doesn't mean the InnerFun(String s) constructor is called as the output is twice: "hello innerfun here" which is printed when InnerFun(){} constructor runs.

Hope this makes sense, if not didn't get a lot of sleep last night ;)
 
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Westland, if the body of an anonymous inner class defines a method that's not declared in the superclass, then the method is useless. You cannot call it. Methods in an anonymous inner class are only useful if they override a method of the superclass. Fields declared inside an anonymous inner class can only be accessed from the methods of the anonymous inner class itself. They can't be accessed anyway else (forgetting about reflection) Example
 
J Westland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Yes I know this is useless code, it's just I am trying to understand the answer to question 2 on page 685 in the Sun SCJP 1.6 study guide. After all a lot of exam questions are with USELESS bits of code and things that would get you killed in real life

The InnerFun s = new InnerFun(){String s;}; bugs me, why the heck can I put String s; in there? It is an override, yes but for what? An extra member? I don't get it. Or is this just legal but useless syntax that doesn't do anything?
 
Ankit Garg
Sheriff
Posts: 9707
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is an override, yes but for what?


No, in InnerFun infun = new InnerFun(){String s;}; , s doesn't override anything. This particular field i.e. s is useless. It is actually an extra member, but since the actual type of infun is InnerFun, and since s is not a member of InnerFun, so you cannot use s. But lets say that I was doing something like this

In this particular case, the instance field val is useful. But this is a fabricated example. I cannot think of any real life use of this. Basically as I said before, the fields in an anonymous inner class can only be accessed by the methods of the anonymous inner class itself. If you can find use of it, then you can use them...
 
Ranch Hand
Posts: 504
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I guess you want to know what is 's'......if yes, 's' is an instance variable of type String in the anonymous inner class and it is of no use.

Or is this just legal but useless syntax that doesn't do anything?



yes you are right it is a legal but useless syntax that does nothing but declares the useless string variable 's'.
 
J Westland
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, so it's legal to do an override of a class with an anonymous inner class and add new members to the anonymous inner (like you can do when you extend a normal class) and that is this mysterious String s in InnerFun(){String s;}; I just wasn't sure how to read this.

I heard anonymous inner class can be very handy indeed for passing in Mouse Adapters and all that, this seems to be something that is legal but as you say not very useful ;)

That clears it up thank you
 
reply
    Bookmark Topic Watch Topic
  • New Topic