Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java Language
Expertise: Beginner
May 13, 1997

How can I create an array of references to objects?

Question:
I would like to create an array of references to objects. In a previous question you mentioned that the Java asignment "objectX=objectY" does not create a copy of objectX and then assign it to objectY; instead, it just points objectY to the same object as objectX. class FooArray { Object myRefArray[]; public FooArray(Object x, Object y, Object z) { myRefArray= new Object[3]; myRefArray[0]=x; myRefArray[1]=y; myRefArray[2]=z; } } ?

If I call FooArray.myRefArray[0].changeName("Mo") , is it the same as if I called x.changeName("Mo") (where x is the object with which I constructed FooArray)?

Answer:
First some background:

Unlike C++, all Java objects reside on the heap, and all references to objects are implicit pointers to the heap that are automatically dereferenced. This makes generalization and specialization easy in Java. For example, assume the following classes and variables have been declared:

class Surface { ... }
   class Sphere extends Surface { ... }

   Surface x;
   Sphere u, v;
The Surface and Sphere pointers have the same size, which makes the following assignments legal:
   x = u;           // generalization
   v = (Sphere) x;  // specialization
Unfortunately, pointer semantics in an imperative language like Java can lead to aliasing nightmares. For example, because x, u and v are all aliases of the same object:
 u.radius = 2 * u.radius;
doubles the radius of v!

Java's pointer semantics extend beyond assignments. For example, object parameters are passed by reference, so the local reference to u in the function call:

v = doubleRadius(u);
is also an alias for u.

If the definer of Sphere was good enough to implement the Clonable interface, override the protected clone method inherited from Object, or provide a copy constructor, then of course we could produce distinct copies of u:

v = u.clone();  // v != u
or
v = new Sphere(u); // v != u
Unfortunately, we can't implement our own copy function because we may not have access to all the members of the Sphere class.

JDK1.1 offers a solution to this problem. Object streams are object containers that allow us to read and write an object to a file without losing the relationships between the object and its members and generalizations. The following fragment turns a file named "tmp" into an object container, writes u into it, then reads it back into v:

FileOutputStream out = new FileOutputStream("tmp");
   ObjectOutputStream  sout = new ObjectOutputStream(out);
   sout.writeObject(u);
   sout.flush();
   FileInputStream in = new FileInputStream("tmp");
   ObjectInputStream sin = new ObjectInputStream(in);
   v = (Sphere)sin.readObject();   // v != u

(Of course this code works only if Sphere implements
the Serializable interface, which all JDK 1.1 objects
do.) 
Note: there is an example in the book Core Java by Cay Horstmann and Gary Cornell (SunSoft Press/Prentice-Hall) of how JDK 1.0 programmers can implement persistent storage. Also, maybe Object stream APIs can be added to JDK 1.0.2.

The short answer to your question is yes, FooArray.myRefArray[0] is an alias for x. Without extending Object by a clonable subclass, your options are to create a clonable extension of Object, or write x to an Object stream, then read it back into your array.

DevX Pro
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date