How can I create an array of references to objects?

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 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
or
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.

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.

Share the Post:
XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes

ransomware cyber attack

Why Is Ransomware Such a Major Threat?

One of the most significant cyber threats faced by modern organizations is a ransomware attack. Ransomware attacks have grown in both sophistication and frequency over the past few years, forcing

data dictionary

Tools You Need to Make a Data Dictionary

Data dictionaries are crucial for organizations of all sizes that deal with large amounts of data. they are centralized repositories of all the data in organizations, including metadata such as