• 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

How many objects are created ?

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I am a little puzzled about the anonymous inner class. How many objects are created in the following code?

We know that instance variable p of type Popcorn references to an instance of the anonymous inner subclass class of Popcorn. Do you think that an instance of Popcorn has to be created for p? How many objects are created from line 2 to 6?
Thanks!
[ April 16, 2004: Message edited by: Serena Zhou ]
 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only one object is created from lines 2-6. That's what the "new" operation does. As far as object allocation is concerned, in this case the fact that it is an anonymous class is not important. It is not different from "String p = new String;".
Now, if you had simply done an inner class (not anonymous with the new operator), or a static inner class, then the object allocation wouldn't happen until explicitly called by a client:
 
Serena Zhou
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I see. Thanks!
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Of course more objects can be created if the constructor for PopCorn does so, but unless that constructor is specifically shown or mentioned you cannot know what it does so the exam will assume it does nothing that influences the answer of the question.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic