• 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

Constructor

 
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


In the above code I was expecting 30 to be displayed on both the occasions.
But it displayed 20 and 30
How come ??
Can anyone please please explain what is happening here ???

Thanks in advance for the help !!
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If u see the code, it is very clear that 2 different constructors have been used.
Test1 y = new Test1(); will assign 20 to i and
Test1 z = new Test1(y); will assign 30 to i.

GetVar will simply return the value of i which is 20 in first case and 30 in second as y and z are two different objects.

Just wanted to know like what was ur approach to say that it was just 30 and not 20 and 30?
 
Jugal Hans
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This was my interpretation of the code.
When y was created "i" gets the value 20, no problem
Then when z is created it passes the copy of the memory reference stored in y to the constructor, "a" is pointing to the same object as y.
then this() calls the no arg constructor which makes the value of i to 20 and then value of i is again changed to 30 (code i = 30).
All this time a,y and z are pointing to the same memory location.
so when GetVar() method is called it will display 30.
Can u tell me where did i go wrong ??

Thank you
[ December 22, 2004: Message edited by: Jugal Hans ]
 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"All this time a,y and z are pointing to the same memory location."

Note that you used the new operator to construct z, and z is pointing to a different location in memory. Also note that although z is constructed with the constructor that takes a Test1 parameter, the argument itself is never used in the constructor.
 
Greenhorn
Posts: 16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
its simple,

(1) first, a call to this() means calling a constructor which takes no parameter, i.e. Test1() which assigns a value of 20 to i
(2) very next statement being modification of same value i to 30 (note that you are still in constructor so same copy of i for object Z will be modified)
(3) hence the results are
System.out.println("Value of y:" + y.GetVar());//20 here
System.out.println("Value of z:" + z.GetVar());//30 here(as explained)
 
That's my roommate. He's kinda weird, but he always pays his half of the rent. And he gave me this 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