GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Basic object count doubt bothering me 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 "Basic object count doubt bothering me" Watch "Basic object count doubt bothering me" New topic
Author

Basic object count doubt bothering me

Vimal Kumar Venugopal
Ranch Hand

Joined: Aug 20, 2011
Posts: 43

Hi Ranch,

While preparing for SCJP, I have been troubled by one question about the number of objects eligible for Garbage collection.

Consider,


In this case how many objects are eligible for GC? Is it one or two? Does a separate object for Parent class get created when instantiating Child class?

Are Strings also considered Objects?
For example,

System.out.println("Hello"+ "World");

After the print statement has executed can I say that 2 objects are eligible for GC?

Please help me clarify this doubt and save my computer being thrown out of the window..
Thanks.

"Value has a Value only if its value is valued!"
Nam Ha Minh
Ranch Hand

Joined: Oct 31, 2011
Posts: 498

Lamiv Ramuk wrote:


In this case how many objects are eligible for GC? Is it one or two? Does a separate object for Parent class get created when instantiating Child class?



No, there is only one object created.


Lamiv Ramuk wrote:

Are Strings also considered Objects?
For example,

System.out.println("Hello"+ "World");

After the print statement has executed can I say that 2 objects are eligible for GC?



Yes, Strings are objects of type String. However String literals may be cached in a String pool, so they may not be eligible for GC.


PM Me If You Want to get Urgent Help on Java Programming
Vimal Kumar Venugopal
Ranch Hand

Joined: Aug 20, 2011
Posts: 43

Hi Nam,

Thank you for the reply. I am convinced for the second one.

  • Yes, Strings are objects of type String. However String literals may be cached in a String pool, so they may not be eligible for GC.


  • Yeah.. Since the strings are getting created in the String pool, no need to worry of GC.

  • No, there is only one object created.


  • But am not convinced for the reply you posted for my first question. When you instantiate a Child object, you can access all "instance" variables and "instance" methods of the parent class. Above all, the Parent classes initialization block runs, Parent CONSTRUCTORS run.. What else!
    Doesnt this seem that when you instantiate a Child, a Parent object is created implicitiy.

    If that is the case, then what about Object - The mother of all objects! Does it also get created somewhere implicitly.

    If that is not the case, then how is it possible to access INSTANCE members of a class by just instantiating a Child class. Doesn't it sound like violation of the whole concept of Object orientation.. My brain seems like disorienting.. Veritgo

    Somebody please help me.

    John Jai
    Bartender

    Joined: May 31, 2011
    Posts: 1776
    The call to a super class constructor is just a flow of code execution when an object is created for a class.

    When it executes the Parent class constructor, the object is still that of the Child.

    When it executes the Object class constructor, the object is still that of the Child.

    A new operator creates only a single instance of the class it works on.
    Vimal Kumar Venugopal
    Ranch Hand

    Joined: Aug 20, 2011
    Posts: 43

    Thank you John
    Nam Ha Minh
    Ranch Hand

    Joined: Oct 31, 2011
    Posts: 498

    Ron Fields wrote:

    But am not convinced for the reply you posted for my first question. When you instantiate a Child object, you can access all "instance" variables and "instance" methods of the parent class. Above all, the Parent classes initialization block runs, Parent CONSTRUCTORS run.. What else!
    Doesnt this seem that when you instantiate a Child, a Parent object is created implicitiy.

    If that is the case, then what about Object - The mother of all objects! Does it also get created somewhere implicitly.

    If that is not the case, then how is it possible to access INSTANCE members of a class by just instantiating a Child class. Doesn't it sound like violation of the whole concept of Object orientation.. My brain seems like disorienting.. Veritgo

    Somebody please help me.



    The object created is a full-fledged object which inherits everything from its parent, parent of its parent, ... up to the last parent: Object. It maybe the work of the compiler or JVM, I am not sure. But still there is only one object created. Things would be very complicated if there's a separate object created for each parent class in the object's inheritance tree.
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    on the real exam, objects of type String are never used for GC questions.

    hth,

    Bert


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    Vimal Kumar Venugopal
    Ranch Hand

    Joined: Aug 20, 2011
    Posts: 43

    Thank you Bert,

    BTW your book was awesome. Uncomparable!!

    Regards,
    Ron.
     
     
    subject: Basic object count doubt bothering me