GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes How many objects created? Their promiscuous procreation dilemma... Ouch! 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 "How many objects created? Their promiscuous procreation dilemma... Ouch!" Watch "How many objects created? Their promiscuous procreation dilemma... Ouch!" New topic
Author

How many objects created? Their promiscuous procreation dilemma... Ouch!

Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Hello everybody! This is an awesome forum.

I am brand new here. I'm preparing for SCJP (have some programming background in Java and in C already). Please forgive me if the question I'm putting down here has been questioned and answered before. Also forgive my lack of knowledge. I seem to be missing a fundamental concept on the subject of object creation.

Here it goes: I'm studying from Sierra/Bates book; an "Exam Watch!" box (in the Arrays section on page 212) admonishes us to think carefully about how many objects are created. Well, that's wonderful, and perhaps I've been thinking about it too much... Isn't the superclass constructor chain called anytime a new object comes to existence (because super() MUST always be called from the child after all)? And since every child class in Java (with the exception of Object) is polymorphic, then at least another new Object object must always be created for any child at the very bottom of the inheritance chain. So suddenly, say on SCJP, the answer to how many objects were created might become not 4 objects, but at least 8 (or very likely many more).

So either all the SCJP exam questions of object counting disregard the superclass objects creation, or my knowledge is amiss. I am assuming the latter of course. The book does not answer my question (at least not at my point where I am now). So could somebody please straighten me up so I can sleep calmly at night once again and count my objects in peace? Much appreciate it.

Cheers,
Marcus J.
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Hi Marcus,

Good job thinking through the issues! However, when you instantiate a class in Java, you really are only instantiating one object. Although it's true that the superclass constructors get called, this doesn't mean that the superclasses get instantiated as well. Basically, the JVM will allocate a memory space sufficient to hold all of the class's own instance variables as well as all the instance variables of its superclasses... but these really do form just a single object.

You should think of chained superclass constructor calls as little more than normal method invocations for initializing the instance variable values. The memory for all of an object's instance variables (including inherited ones) would already have been allocated before a single superclass constructor call. In Java, "instantiation" basically just means the use of the "new" keyword in expressions like "new MyClass()". Essentially, the superclass constructor calls are initialization steps, not instantiation steps.


SCJP 5.0
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Ok great, that makes much more sense, thank you Kelvin. So let me just make sure I understand it.

The call to super(), as you say, is not a method call per se -- since constructors are not methods (that is clear). So expanding further on your explanation, all of those parent objects (superclasses) may or may not exist on the heap? Is that so? For example, the father (or mother) of all objects: the Object object ... Is it possible that this guy (gal) does not exist at all? Or is it true that at the very least a single instance of the Object object must exist for any running java program?

I am trying to understand if the existence of superclasses is really a more abstract concept. In other words, they may or may not exist literally... Or perhaps all the superclass objects do exist explicitly but only when they are specifically instantiated. As in: Object o = new Object()
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
Hmm... I am beginning now to see the picture. Never mind my previous post. I talked myself through it, with your help.

The word you used "initialize" brings the point clearly to light. So now the way I understand it is that the chained constructor call is the process of initializing any object before it is created. The superclasses participate in the initialization of the object in question, BUT they never need to be instantiated explicitly.

I think I got it now! Thanks much! More coffee!
Mark Uppeteer
Ranch Hand

Joined: Mar 02, 2004
Posts: 159

think of it as an appartement building, you might have a lot of floors , but there is always just one building!


I know where my towel is. (SCJP 5, OCPJWCD)
[Free Quiz Tips for a fun night with friends or family] Flash games
Kelvin Chenhao Lim
Ranch Hand

Joined: Oct 20, 2007
Posts: 513
Originally posted by Marcus Jastrebowski:
The word you used "initialize" brings the point clearly to light. So now the way I understand it is that the chained constructor call is the process of initializing any object before it is created. The superclasses participate in the initialization of the object in question, BUT they never need to be instantiated explicitly.

Yes, your understanding is correct, and you just explained it very well yourself! Of course, you're also right in saying that an "object" is arguably more of an abstract language concept rather than a precisely-defined entity at the bits 'n' bytes level. But the key thing to realize here is that there's only one heap allocation step involved when you instantiate a class, regardless of how many superclasses it has.

Just for fun, let me now mix things up a little: your intuition that a class object also requires superclass objects is actually quite close to the truth. However, in Java, this happens not during object instantiation, but rather during class loading. In order for the JVM to successfully load a class from a .class file, it must already have previously loaded all its superclasses. In this sense, you do need an in-memory data structure for each superclass in order to "construct" (or rather, load) an in-memory data structure for a class.

(Note to all who may read this: class loading/linking/etc are fairly advanced Java topics, so please don't let the above paragraph confuse you if you're just interested in passing the SCJP!)
[ November 15, 2007: Message edited by: Kelvin Lim ]
Marcus Jastrebowski
Ranch Hand

Joined: Nov 15, 2007
Posts: 55
All right! I was thinking that there must be something funky (and more complicated) going on behind the scenes to make all this machinery run. And that's why I see that a discussion such as this one here would not be quite appropriate for the SCJP study guide. After all, no need to know the engine combustion process to make sure that the car goes from point A to point B as the maker intended it to...

So I'm just going to let the fine points of JVM runtime class construction slide over the top of my head . But thanks for the background. Very interesting indeed.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: How many objects created? Their promiscuous procreation dilemma... Ouch!