aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Anonymous Inner Class Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Anonymous Inner Class Question" Watch "Anonymous Inner Class Question" New topic
Author

Anonymous Inner Class Question

J Westland
Ranch Hand

Joined: Apr 06, 2009
Posts: 40
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 ;)


Woohoo passed SCJP 1.6, that's the theory exam passed now for the practice ;)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

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


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
J Westland
Ranch Hand

Joined: Apr 06, 2009
Posts: 40
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

Joined: Aug 03, 2008
Posts: 9293
    
  17

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...
Neha Daga
Ranch Hand

Joined: Oct 30, 2009
Posts: 504
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'.


SCJP 1.6 96%
J Westland
Ranch Hand

Joined: Apr 06, 2009
Posts: 40
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
 
 
subject: Anonymous Inner Class Question