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

Mupltiple or single object(s) can be createdof a class: contradiction in this statement

 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
declaration and instantiation :-



Test test - declares the reference variable.

new Test() - using new operator makes a call to the construtor and creates an object.

Test test = new Test(); - puts the object reference in the reference variable test

----------------------------------------

Now if i create another reference variable :-



test2 - this is another reference variable. My question is, does test2 pointing to the same object created before or poiting to another newly created pointer just now.

Don't you think that there is only 1 object of a class but we are creating multiple reference variables for that same object only, any contradiction,if yes ?
please clear my doubt with some examples.




 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each usage of new creates a unique instance.
 
byronc Ozzie
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
test and test2 will have different addresses in them pointing to different instances of your object Type "Test"
 
Al Fraelich
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think of test as an object, like a ford, mustang. there are many "instances" of ford, mustang in the world. But each is "unique".

You identify each "unique" instance of ford, mustang by a vin number, where as in java each instance of an object is identified by a name.

In your example, one instance of Test was named "test" and the other instance was named "test2", if I remember correctly.

so test is a completley different, but identical, instance of test2.

Hope that helps
Al
 
Bobby Smallman
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Now try to think about what happens when you go:
 
Vinod Vinu
Ranch Hand
Posts: 217
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
think of test as an object, like a ford, mustang. there are many "instances" of ford, mustang in the world. But each is "unique".

You identify each "unique" instance of ford, mustang by a vin number, where as in java each instance of an object is identified by a name.

In your example, one instance of Test was named "test" and the other instance was named "test2", if I remember correctly.

so test is a completley different, but identical, instance of test2.


Thank you very much Al Fraelich. You have resolved my doubt....really appreciated
[URL=http://faq.javaranch.com/java/SayThanks]Thanks[URL]
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that something like this would have been trivially easy to answer yourself, simply by creating some Test objects, setting a property on each, and seeing if they end up different, or all the same.

(Or just printing out using the standard toString(), if you trust that.)
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic