• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

The value is not what i expected

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
2 baskets sample is very good!
Thank you very much Christophe Verré !
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic