How can I create an array of references to objects?

How can I create an array of references to objects?

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)?

First some background:

Unlike C++, all Java objects reside on the heap, and all referencesto objects are implicit pointers to the heap that areautomatically dereferenced. This makes generalization andspecialization easy in Java. For example, assume thefollowing 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 thefollowing assignments legal:
   x = u;           // generalization   v = (Sphere) x;  // specialization
Unfortunately, pointer semantics in an imperativelanguage 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 implementthe Clonable interface, override the protected clonemethod inherited from Object, or provide a copyconstructor, then of course we could produce distinct copies of u:

v = u.clone();  // v != u
v = new Sphere(u); // v != u
Unfortunately, we can’t implement our own copyfunction because we may not have access to all themembers of the Sphere class.
See also  Comparing different methods of testing your Infrastructure-as-Code

JDK1.1 offers a solution to this problem. Objectstreams are object containers that allow us to read and write an object to a file without losingthe relationships between the object and itsmembers and generalizations. The followingfragment turns a file named “tmp” into an objectcontainer, writes u into it, then reads it backinto 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 implementsthe Serializable interface, which all JDK 1.1 objectsdo.) 
Note: there is an example in the book Core Java by Cay Horstmann and Gary Cornell (SunSoft Press/Prentice-Hall) of how JDK 1.0programmers can implement persistent storage. Also, maybeObject 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 clonablesubclass, your options are to create a clonable extension of Object,or write x to an Object stream, then read it back into yourarray.


About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist