• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.




 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Each usage of new creates a unique instance.
 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
test and test2 will have different addresses in them pointing to different instances of your object Type "Test"
 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Now try to think about what happens when you go:
 
Vinod Vinu
Ranch Hand
Posts: 220
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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]
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic