Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Variables in different classes that reference the same object?

 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Michael Bruesch
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic