Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30758
| Path | csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
| Newsgroups | comp.lang.java.programmer |
| Subject | Re: java referencing question |
| Date | Tue, 20 Jan 2015 10:23:01 -0500 |
| Organization | A noiseless patient Spider |
| Lines | 72 |
| Message-ID | <m9lrrp$b7b$1@dont-email.me> (permalink) |
| References | <37e63a38-a2ec-452d-81c3-da09d5999f89@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset=windows-1252; format=flowed |
| Content-Transfer-Encoding | 7bit |
| Injection-Date | Tue, 20 Jan 2015 15:22:33 +0000 (UTC) |
| Injection-Info | mx02.eternal-september.org; posting-host="bd4e1cba8d8592bec5d5ed9e4537fbfd"; logging-data="11499"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/DnnxcICCzcXd2yEKlQxp1" |
| User-Agent | Mozilla/5.0 (Windows NT 6.1; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.4.0 |
| In-Reply-To | <37e63a38-a2ec-452d-81c3-da09d5999f89@googlegroups.com> |
| Cancel-Lock | sha1:pPN2UUlT/D2eCvBEYe3f31IIZIM= |
| Xref | csiph.com comp.lang.java.programmer:30758 |
Show key headers only | View raw
On 1/20/2015 9:17 AM, John wrote:
>
> I ran into a "strange" problem. If I understood the problem correctly, my summary is here:
>
> public class Team {
> private Person teamLeader;
>
> public Team(Person theLeader) {
> teamLeader = theLeader;
> }
>
> public void doIt() {
> teamLeader.speak();
> ...
> }
>
> }
>
>
> public class Demo {
> public static void main(String[] args) {
> Person leader = null;
> Team dreamTeam = new Team(leader); //teamLeader inside dreamTeam is referencing an object which is also referenced by 'leader'. Right now, the object is null
>
> leader = new Person(); //the object being referenced by leader is a good object now. So I thought teamLeader inside dreamTeam is referencing this good object too, since the two are referencing the same object
> dreamTeam.doIt(); //crashes at teamLeader.speak() due to teamLeader is null -- I don't understand it. Please help.
> }
> }
>
> The design is not good. I will change the design. Here I am puzzled by what I saw. Thank you.
The thing you're missing is that `leader' is not a Person object,
but a reference that can point to Person objects. At the time of
`new Team(leader)', the `leader' variable is null: It has a value
(null) that refers to no object. The constructor copies this null
value to `teamLeader', so `teamLeader' also refers to no object.
Later, you create a new Person and make `leader' refer to it.
But the fact that `leader' now points to a Person does not cause
`teamLeader' also to point to that Person: Changing the value of
the variable `leader' does not magically change the value of every
other variable that was initialized from `leader's former value.
Let's try a simplified version:
public class Value {
private int number;
public Value(int theNumber) {
number = theNumber;
}
public void doIt() {
System.out.println("The number is " + number);
}
}
public class TryIt {
public static void main(String[] unused) {
int howMuch = 10;
Value value = new Value(howMuch);
value.doIt(); // Prints "The number is 10"
howMuch = 42;
value.doIt(); // What does this print? Why?
}
}
Giving a new value to `howMuch' after its old value has already
been copied to `value.number' does not also change `value.number'.
It's exactly the same with your `leader' and `dreamTeam.theLeader'.
--
esosman@comcast-dot-net.invalid
"Don't be afraid of work. Make work afraid of you." -- TLM
Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread
java referencing question John <xsli2@yahoo.com> - 2015-01-20 06:17 -0800 Re: java referencing question Jeff Higgins <jeff@invalid.invalid> - 2015-01-20 10:03 -0500 Re: java referencing question Eric Sosman <esosman@comcast-dot-net.invalid> - 2015-01-20 10:23 -0500
csiph-web