wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Variables in different classes that reference the same object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Variables in different classes that reference the same object?" Watch "Variables in different classes that reference the same object?" New topic
Author

Variables in different classes that reference the same object?

Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Take the following:

Are listA and listB now pointing to the same List object even though they're in different classes? And say a method is called in class A that changes listA, then a method is called in class B that accesses listB, will it be changed too then? If that is true, my life will be much easier. It's a very basic question I know, but I never thought about what if the reference variables were in different classes. I can explain my particular situation as to why I ask this question if you need further details to answer the question. Thanks!

Michael J Bruesch<br /><i>I code, therefore I am.</i>
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

This may seem like a simple question, but there's an important revelation behind this code: there's one object in memory and it's been "mapped" by two different objects (assuming there's One instantiation of each). Changes made to the object through one reference of course chaneg what the other reference sees.
If you've ever wonder how one object can "require" the existence of another before it may be constructed, well, this is it!
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
Thanks Michael, I feel better about asking the question then.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
http://www.geocities.com/mjbruesch
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Variables in different classes that reference the same object?
 
Similar Threads
Iterator better for that?
Searching for a list element without having to iterate
ArrayList issues?
Reusability Tutorial: The way I do it.
Generics Question: