Results 1 to 4 of 4
  1. #1
    shantharam is offline Member
    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Default objects instantiation

    Hi. There is a question in scjp preparation and I wish to know more about the explained answer:

    Given:
    3. class Dozens {
    4. int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
    5. }
    6. public class Eggs {
    7. public static void main(String[] args) {
    8. Dozens [] da = new Dozens[3];
    9. da[0] = new Dozens();
    10. Dozens d = new Dozens();
    11. da[1] = d;
    12. d = null;
    13. da[1] = null;
    14. // do stuff
    15. }
    16. }
    Which two are true about the objects created within main(), and eligible for garbage collection
    when line 14 is reached?
    A. Three objects were created
    B. Four objects were created
    C. Five objects were created
    D. Zero objects are eligible for GC
    E. One object is eligible for GC
    F. Two objects are eligible for GC
    G. Three objects are eligible for GC
    Answer:
    ✓ C and F are correct. da refers to an object of type "Dozens array," and each Dozens object
    that is created comes with its own "int array" object. When line 14 is reached, only the
    second Dozens object (and its "int array" object) are not reachable.

    Can anyone tell me why its mentioned that there are 5 objects are reported created here.
    Also doesn't defining the array of objects create the objects? E.g., in the line number 8, I see three element array is created, doenst this mean 3 objects are instantiated?


    Thanks

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: objects instantiation

    You didn't bother to reply to Junky's response in your earlier thread: garbage collection

    Why should we expect any better behavior this time round?

    Also, go through the Guide For New Members and BB Code List - Java Programming Forum - Learn Java Programming and edit your post accordingly.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: objects instantiation

    Quote Originally Posted by shantharam View Post
    Can anyone tell me why its mentioned that there are 5 objects are reported created here.
    Because that is exactly how many are created.
    Also doesn't defining the array of objects create the objects? E.g., in the line number 8, I see three element array is created, doenst this mean 3 objects are instantiated?
    Absolute not! Imagine you want to go and pick apples(objects) from an orchard but before you can do that you must make a wooden crate (array) to hold them. Once you have made the crate how many apples are in it? They do not automagically appear.
    Java Code:
    String[] words = new String[5];
    According to you that should also create 5 Strings. Please tell me what those 5 Strings will be. The compiler/JVM has absolutely no idea what values the objects will hold so how can it create them?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: objects instantiation

    Quote Originally Posted by DarrylBurke View Post
    You didn't bother to reply to Junky's response in your earlier thread: garbage collection
    Ahhh!

    Same person.

Similar Threads

  1. XML instantiation by xpath
    By freaksta in forum XML
    Replies: 1
    Last Post: 11-22-2009, 10:25 AM
  2. Instantiation order
    By Jeremy720 in forum New To Java
    Replies: 3
    Last Post: 07-17-2009, 03:19 PM
  3. instantiation
    By Camden in forum New To Java
    Replies: 1
    Last Post: 03-02-2008, 04:44 PM
  4. Instantiation
    By kavithas in forum New To Java
    Replies: 2
    Last Post: 12-05-2007, 04:56 PM
  5. object instantiation and arrayList
    By lockmac in forum New To Java
    Replies: 5
    Last Post: 08-09-2007, 06:25 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •