Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

The value is not what i expected

 
Binar Guardinho
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why output is "30" ? Everytime it does , and value of e1.count is "0", it should print "2" in the end, can't understand how it works. Please someone to explain me. Thank You
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
e2 = e1 is just a distraction. Imagine that there is only one object. Will that help to understand the result ?
 
Binar Guardinho
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:e2 = e1 is just a distraction. Imagine that there is only one object. Will that help to understand the result ?


Can't understand. I was tested before something like this

The result was 3, so e2 recive value of e1, result that e2.count should be "0" every time on the main example. Any idea?
 
Christophe Verré
Sheriff
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Imagine your have two baskets where you put your cloths in. If you put the first basket into the second basket and put cloths in it, in which basket are the cloths ? If put the first basket in the second basket again (although it was already in there) and add some more cloths, in which basket are the cloths ?

What I'm trying to say (in a very confusing way ), is that e2 is always e1. The Echo() instance represented by "Echo e2 = new Echo();" will be discarded as soon as e2 = e1. Only e1 is alive. Everything happens in e1. e2 is e1. So e2.count = e2.count + 1; is actually e1.count = e1.count + 1;. System.out.println(e2.count); is actually System.out.println(e1.count);...
 
Binar Guardinho
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christophe Verré wrote:Imagine your have two baskets where you put your cloths in. If you put the first basket into the second basket and put cloths in it, in which basket are the cloths ? If put the first basket in the second basket again (although it was already in there) and add some more cloths, in which basket are the cloths ?

What I'm trying to say (in a very confusing way ), is that e2 is always e1. The Echo() instance represented by "Echo e2 = new Echo();" will be discarded as soon as e2 = e1. Only e1 is alive. Everything happens in e1. e2 is e1. So e2.count = e2.count + 1; is actually e1.count = e1.count + 1;. System.out.println(e2.count); is actually System.out.println(e1.count);...

Now it's clear . Thnx man
 
J Kim
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 baskets sample is very good!
Thank you very much Christophe Verré !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic