Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Objects and References....Constructors and new

 
Sumesh Kumar T N
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please forgive my ignorance. But I would like to know the intricacies related to memory allocation and construction in java. please explain the relation between construction and memory allocation. also tell about the steps that are taken for an object to get created on the heap. what would happen if i simply assign the fields in an object reference like in case of structures in C. Also tell me the relevance of new operator. include the difference between the actual object content and the reference variable and the confusion scenarios that may crop up when we thinki deep into them......waiting for a reply that would erase all my doubts.....i am sorry if the question went too long
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's all automatic.
And no, you can't do anything with an object that hasn't been created.
 
Pauline McNamara
Sheriff
Posts: 4012
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sumesh, welcome to javaranch.

That's a lot of questions all at once. How about you pick out just one to start.

Which one would you most like to get help with?


Cheers,
Pauline


Asking Questions 101
 
David Harkness
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll give you an overview from which you can use Google to get more information.
  • The new operator is used to instantiate an object.
  • Instantiation involves allocating memory on the heap, initializing fields, calling the object's constructor, and returning a reference.
  • A reference is just a pointer to the object in memory (like a pointer in C). The object in memory (the instance) holds the field values.

  • what would happen if i simply assign the fields in an object reference like in case of structures in C[?]
    A NullPointerException would be thrown by the JVM (similar to a core dump in C, but you can catch it and take action if you want). You must always set references to a non-null value before using them to access fields or call methods.
     
    Sumesh Kumar T N
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Pleased to hear from you, Pauline. I would go for a single question. I would like to know about everything that happens right from when the new is called till creation of an object...........


    Thanks to everyone who cast light on the subject
     
    Sumesh Kumar T N
    Greenhorn
    Posts: 10
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Group one:
    Integer one = new Integer(1);
    Integer two = new Integer(1);

    Group two:
    String one = new String("Hello");
    String two = new String("Hello");

    Please explain the above code for me as to how many objects are created on heap.Is there any difference between the two groups of statements???
     
    Lionel Badiou
    Ranch Hand
    Posts: 140
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi Sumesh,

    The JVM manage the second group in a special way. Look at this thread for details.

    Best regards,
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic