Variable reuse

Good evening all,

I would just like to find out how and why the following code is possible. By my thought process I would have thought that using the counter variable assigned to the two different attributes would lead to the same result in each but I can also see that each one is instantiated separately.

Counter counter =new Counter();

context.setAttribute("hitCounter",counter);

counter =new Counter();

context.setAttribute("orderCounter",counter);

would this be the same as saying:

context.setAttribute("hitCounter",new Counter());

[762 byte] By [tarmenela] at [2007-11-26 21:29:40]
# 1

> Good evening all,

>

> I would just like to find out how and why the

> following code is possible. By my thought process I

> would have thought that using the counter variable

> assigned to the two different attributes would lead

> to the same result in each but I can also see that

> each one is instantiated separately.

> Counter counter = new Counter();

> context.setAttribute("hitCounter",counter);

> counter = new Counter();

> context.setAttribute("orderCounter",counter);

> would this be the same as saying:

> context.setAttribute("hitCounter", new

> Counter());

I have a child, I name him fred. Time goes on.

I have a second child, I also name him fred.

How many children do I have ?

New makes a new object.

*Note* hopefully this explanation helps. If not I am sure there are others that will correct this.

Aknibbsa at 2007-7-10 3:10:16 > top of Java-index,Java Essentials,Java Programming...
# 2

You do understand that the variable "counter" does not contain the object but rather contains a "reference" an object of that type (or null) correct? The expression "new Counter()" constructs a new instance of Counter using the no-arg constructor and returns a reference to this newly created instance. This reference is then assigned to the variable "counter". Then you pass that to the setAttribute method where it's used for whatever it's used for. Following that a completely new instance of Counter is created, a reference to it returned, that reference assigned to the variable (thus overwriting the old one) and so it now points to a completely different object. That is then passed to setAttribute where it uses it however it uses it, this time it is a completely different reference that points at a completely different object.

kablaira at 2007-7-10 3:10:16 > top of Java-index,Java Essentials,Java Programming...
# 3

How about you have a piece of paper that you call "counter". You write an address down on the piece of paper. You give that to someone else who copies that address down. Then you change the address on that piece of paper, but you still call that piece of paper "counter" and pass that piece of paper to someone again and they again write down the address. The fact that you called the piece of paper "counter" is irrelevant, the person you passed it to only knows that you passed them two different addresses which they copied down and did whatever they needed to with them.

kablaira at 2007-7-10 3:10:16 > top of Java-index,Java Essentials,Java Programming...
# 4

Thanks for the useful responses. Would I be right in then assuming that the first counter that was created is no longer avaiable for use other than through the context object? I mean after creating the second "Fred" how would I be able to use the first instance if they both have the same name?

tarmenela at 2007-7-10 3:10:16 > top of Java-index,Java Essentials,Java Programming...
# 5
correct
dmbdmba at 2007-7-10 3:10:16 > top of Java-index,Java Essentials,Java Programming...