• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Basic object count doubt bothering me

 
Vimal Kumar Venugopal
Ranch Hand
Posts: 43
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Nam Ha Minh
Ranch Hand
Posts: 504
Eclipse IDE Firefox Browser Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Vimal Kumar Venugopal
Ranch Hand
Posts: 43
Eclipse IDE Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Rancher
    Posts: 1776
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 43
    Eclipse IDE Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you John
     
    Nam Ha Minh
    Ranch Hand
    Posts: 504
    Eclipse IDE Firefox Browser Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 8900
    5
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    on the real exam, objects of type String are never used for GC questions.

    hth,

    Bert
     
    Vimal Kumar Venugopal
    Ranch Hand
    Posts: 43
    Eclipse IDE Java Oracle
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you Bert,

    BTW your book was awesome. Uncomparable!!

    Regards,
    Ron.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic