Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.java.programmer > #30756 > unrolled thread
| Started by | John <xsli2@yahoo.com> |
|---|---|
| First post | 2015-01-20 06:17 -0800 |
| Last post | 2015-01-20 10:23 -0500 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.java.programmer
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
| From | John <xsli2@yahoo.com> |
|---|---|
| Date | 2015-01-20 06:17 -0800 |
| Subject | java referencing question |
| Message-ID | <37e63a38-a2ec-452d-81c3-da09d5999f89@googlegroups.com> |
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.
[toc] | [next] | [standalone]
| From | Jeff Higgins <jeff@invalid.invalid> |
|---|---|
| Date | 2015-01-20 10:03 -0500 |
| Message-ID | <m9lqrr$6lp$1@dont-email.me> |
| In reply to | #30756 |
On 01/20/2015 09: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.
>
4.3 Reference Types and Values
There are three kinds of reference types: class types (§8), interface
types (§9), and array types (§10). Reference types may be parameterized
(§4.5) with type arguments (§4.4).
4.3.1 Objects
An object is a class instance or an array.
The reference values (often just references) are pointers to these
objects, and a special null reference, which refers to no object.
[toc] | [prev] | [next] | [standalone]
| From | Eric Sosman <esosman@comcast-dot-net.invalid> |
|---|---|
| Date | 2015-01-20 10:23 -0500 |
| Message-ID | <m9lrrp$b7b$1@dont-email.me> |
| In reply to | #30756 |
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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.java.programmer
csiph-web