my dog learned polymorphism*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes InnerClass question from Ch8 SierraBates 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 "InnerClass question from Ch8 SierraBates" Watch "InnerClass question from Ch8 SierraBates" New topic
Author

InnerClass question from Ch8 SierraBates

Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
here is the question...
8. Given the following,
public class Foo {
Foo() {System.out.print("foo");}
class Bar{
Bar() {System.out.print("bar");}
public void go() {System.out.print("hi");}
}
public static void main (String [] args) {
Foo f = new Foo();
f.makeBar();
}
void makeBar() {
(new Bar() {}).go();
}
}
what is the result?
A. Compilation fails.
B. An error occurs at runtime.
C. foobarhi
D. barhi
E. hi

Now, i had the right answer for this... until i spun a tyre on the phrasing of the method makeBar(). What is going on here??? i know there must be a rational answer...maybe. But i am just a little frazzed by the confusingness of the InnerClasses chapter exam (i understood the chapter, didnt go so well on the end-of-chapter-exam ).
okay, so it is a method-local anonymous inner class?
void makeBar() {
(new Bar() {}).go();
}
the bracketing is throwing me off.


giddee up
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203

okay, so it is a method-local anonymous inner class?
void makeBar() {
(new Bar() {}).go();
}
the bracketing is throwing me off.

Yes it ia an annonymous inner class that is extending Bar and at the same time creating the instance of the newly extended class and calling the method on this newly created object.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
Even if you remove those confusing two braces you get the same result except that you are creating an instance of Bar class defined above.
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
thankyou Sarma
so...what are the two braces there for?
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
jas,
Welcome to Javaranch, a friendly place for Java greenhorns
The two braces indicate that we are creating an anonymous inner class. Between those braces you could, for instance, override a method declared in class Bar (method go()), or declare new attribute members or whatever else fits in a class declaration.
We ain't got many rules 'round these parts, but we do got one. Please change your displayed name to comply with the JavaRanch Naming Policy.
Thanks Pardner! Hope to see you 'round the Ranch!
PS: Jasper Vader would be fine
[ March 03, 2003: Message edited by: Valentin Crettaz ]

SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
yes, i have been wondering about that, but in my frenzied rush towards SCJP-dom i haven't figured out what is going on. on my mac, i am jas, yet on the toshiba laptop, i am jasper vader.... i tried joining twice, but i have used the same password as far as i can tell, and same login name... hmmm.... the strange things that go on when more than one computer is used. heaven knows [B]what[\B] will happen when i start studying network programming!
i will have a look at the preferences i have set...
Jasper Vader
Ranch Hand

Joined: Jun 10, 2002
Posts: 57
the thing is, the way the
(new Bar() {}).go();
is presented, it looks more like the flavour of the anonymous class that is defined in an argument WITHIN a method call...i cant seem to figure out how else this syntax would work...although i do accept it is legal, i have compiled the code and you good ppl have told me its okay.
Sarma Lolla
Ranch Hand

Joined: Oct 21, 2002
Posts: 203
(new Bar()).go(); would also do the same except that you are not extending the Bar class but just instantiating it.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: InnerClass question from Ch8 SierraBates