Obtaining a Reference to an Object

Obtaining a Reference to an Object

In Java, you can obtain a reference to an object in three ways. First, you can obtain it from the new operation. The new operation instantiates an object of a particular class, and returns a reference to it. This reference is a handle to the location where the object resides in memory.

 SomeObject aRef = new SomeObject();

The reference is stored in the variable “aRef.”

Second, you can obtain a reference by assignment. The reference to an existing reference obtained by assignment is just a duplicate reference to the object (and not to a copy of the object).

 SomeObject anotherRef = aRef;

The variable anotherRef duplicates aRef. Both references point to the same instance of the object of type SomeObject.

Third, you can obtain a reference by cloning an object. The reference that is returned by cloning an object is a new reference to a copy of the object.

 SomeObject anotherObjectRef = aRef.clone();

The clone() method is defined in the Object class. It is a protected method so that derived classes of the Object class (which, in effect, is every other Java class) can override it. Once an object is cloned, two copies of the object exist with independent references. Changing one object does not automatically change the other. You can only call the clone() method on an object that extends the Clonable interface.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

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