File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

InnerClass question from Ch8 SierraBates

 
Jasper Vader
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sarma Lolla
Ranch Hand
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thankyou Sarma
so...what are the two braces there for?
 
Valentin Crettaz
Gold Digger
Sheriff
Posts: 7610
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Jasper Vader
Ranch Hand
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 57
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 203
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic