aspose file tools*
The moose likes Beginning Java and the fly likes Mupltiple or single object(s) can be createdof a class: contradiction in this statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Mupltiple or single object(s) can be createdof a class: contradiction in this statement" Watch "Mupltiple or single object(s) can be createdof a class: contradiction in this statement" New topic
Author

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

Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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.





Vinod Kumar Nair
"Any fool can write code that a computer can understan. Good programmers write code that humans can understand."
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60082
    
  65

Each usage of new creates a unique instance.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
byronc Ozzie
Greenhorn

Joined: Jul 12, 2010
Posts: 7
test and test2 will have different addresses in them pointing to different instances of your object Type "Test"


Byron
website design - my immigration to Sydney
Al Fraelich
Greenhorn

Joined: May 25, 2009
Posts: 20
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

Joined: Sep 09, 2010
Posts: 107
Now try to think about what happens when you go:


Everyday in every way, we get a little better.
Vinod Vinu
Ranch Hand

Joined: Aug 30, 2009
Posts: 217
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

Joined: Sep 29, 2008
Posts: 12617

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.)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Mupltiple or single object(s) can be createdof a class: contradiction in this statement
 
Similar Threads
How many objects eligible for GC?
garbage collection
Is this a Has-a ?
Objects eligible for garbage collection
HFJ doubt