Skip to main content

Shallow cloning in Java

Cloning basically refers to creating an identical copy of an existing object. 

The term clone is derived from ÎºÎ»ÏŽÎ½, the Greek word for "twig, branch", referring to the process whereby a new plant can be created from a twig.

The word 'Cloning' in java refers to the same meaning. When we say we have created a clone object in java we mean we have created a new object with identical values in the object.

There are two type of cloning in java as below :

1. Shallow cloning
2. Deep cloning

Since this article is about shallow cloning will give description about that only and will include about the deep cloning in my next post.

Shallow cloning :

So shallow cloning is nothing but creating a clone of the object but not in detail. Now what does that mean?
It means that if you have a object with referenced object in your main object then shallow cloning will not clone them, it will just clone the attribute of the main object. 

Let's take an example :

I have a class
class person
{   
   Long identifier; 
   String firstName; 
   String lastName; 
   Address address;
    // remaining code goes here
}


Address class


class Address
{
     Long identifier; 
     String streetName;   
               String state;
// remaining code goes here
          }

Now shallow cloning can be done easily by using cloneable interface.

Example :

class person implements Cloneable
{  
  Long identifier;  
  String firstName;   
  String lastName;   
  Address address;
  // remaining code goes here
   
    public Object clone() throws CloneNotSupportedException
    {
         super.clone();
    }
}

Now if I call this method from my main method then you will get a shallow clone of the object person.
That means you will get a new object of class Person but this is still pointing to old address object. It will just copy the reference of that object.






















Comments

Popular posts from this blog

Create Table in Liquibase

For creating table using liquibase you can use below code and add it in your liquibase file. <createTable tableName=“employee”>      <column name="id" type="int">      <constraints primaryKey="true" nullable="false"/>   </column>      <column name="first_name" type="varchar(255)"/>   <column name="last_name" type="varchar(255)"/>   <column name="username" type="varchar(255)">      <constraints unique="true" nullable="false"/>   </column> </createTable> The use is pretty simple it's the way it looks : Tag: <createTable></createTable> This is an opening/ending tag for creating a table. These tags will enclose column sub tags which will define columns for the table. Attribute:   tableName : Name of the table which you want to create. (This is a mandatory   ...

How databasechangeloglock and databasechangelog table used by liquibase?

Liquibase takes care of executing the query on the database while maintaining the list of queries executed and also maintaining the locks over the tables simultaneously. The two tables that are used by the liquibase for this purpose are : Databasechangeloglock : This table have following columns ID | LOCKED| LOCKGRANTED | LOCKEDBY. This maintains the locks information granted to the user. The primary purpose of this table is to make sure that two machines don't attempt to modify the data at the same time. Databasechangelog : This table have following columns ID | AUTHOR | FILENAME | DATEEXECUTED | ORDEREXECUTED | EXECTYPE | MD5SUM | DESCRIPTION | COMMENTS | TAG | LIQUIBASE This table maintains the list of the statements that are executed on the database.

Carnivorous Island from "Life of PI"

Yann Martel described about an floating carnivorous Island for symbolizing that something seemingly good in life can turn out to be harmful, so we should keep going with our journey of life... About the carnivorous Island from movie. In the movie 'Life of PI' after PI struggles to live in the pacific with the Bengal tiger suddenly one morning Pi  found himself at a Island. After spending so many days in the middle of the sea he found himself very lucky to find a Island. After spending some time he thought might be this was the end of his suffering and he can spend rest of his life at that island. He makes h is bed on a tree and sleeps there, in the middle of the night he wakes up and find that there were dead fish in the fresh water where he took swim in the day time. While trying to understand the mystery he plucks a fruit from the same tree and peel it off and finds a human tooth inside that fruit and then he realize that it was an carnivorous Island. The...