• 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

problem to understand d1.d=d2

 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class dog
{
dog d;
public static void main(String[] arg)
{
dog d1=new dog();
dog d2=new dog();
// code insertion.............

}
}
if in above code sample at comment part if I do
d1=d2; means now d1 has lost the object it was referring and now d1 is referring the same object as being referred by d2.

But i've problem to understand if i do
d1.d=d2;
What does above line stands for???
Thanks
Deepak
 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Since d is an attribute that every dog object has, i think that d1.d=d2 would imply that you are trying to make the d1's (member) d to refer to the same dog object that d2 refers to. This should run perfectly well though I think the code is poorly crafted in terms of design. You should generally use get/set methods to change the attributes of objects to prevent client code from indiscriminately altering them.

By the way,where did you get such cryptic looking code from?was it an exam question?I'm also preparing for scjp5.0
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Legunsen
Similar kind of code is given on page 249 of K & B under Isolating a Reference(for Garbage Collection).That is hard to understand What exactly happening there.
 
owolabi legz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will look it up and get back to you. I hope you are not stuck in your preparations because of this.
 
owolabi legz
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
By the way, did you find my previous post useful?
 
Deepak Bobal
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Castor
No doubt that was useful and i was thinking in the same way...
 
reply
    Bookmark Topic Watch Topic
  • New Topic