Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8924697
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T07:29:16+00:00 2026-06-15T07:29:16+00:00

Shallow copy means a copy of an object with same values of their attributes

  • 0

Shallow copy means a “copy” of an object with same values of their attributes whether primitive or reference values.

While performing shallow copy is it necessary to “create a new instance” ? as:

public class A {
    int aValue;
    B bObj;

    ...

    public A createShallow(A a1Obj) {
        A aObj = new A();
        aObj.aValue = a1Obj.aValue;
        aObj.bObj = a1Obj.bObj;

        return aObj;
    }
}

Or copy by assignment is also considered as shallow copy:

B b = new B(10);
A a = new A(1, b);

A a1 = a;

This article at wikipedia defines shallow copy as reference variables sharing same memory block. So according to this copy by assignment will also be a shallow copy.

But is not it a variables pointing to same object instead of “copy” of an Object ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T07:29:17+00:00Added an answer on June 15, 2026 at 7:29 am

    While performing shallow copy is it necessary to “create a new
    instance” ?

    Yes, you must create an instance to create a copy (either shallow or deep) of your object. Just doing the assignment of reference just creates a copy of reference which points to the same instance.

    You have used a non-static method that is creating a copy. But generally I prefer two ways: –

    Either use a copy-constructor: –

    public A(A obj) {
        copy.aValue = obj.aValue;
    }
    

    And use it like: –

    A first = new A();
    A copy = new A(first);
    

    Or, use a public static method which takes an instance and returns a copy of that.

    public static A createCopy(A obj) {
        A copy = new A();
        copy.aValue = obj.aValue;
        return copy;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a shallow copy (new instance) of an object, without
How do I create a copy of a class object without any reference? ICloneable
java.util.Calendar.clone() returns ...a new Calendar with the same properties and returns a shallow copy
I need a semi-shallow copy of an object. Under my original design I used
Does SqlCommand.Clone() create a deep copy or shallow copy? Also, is it safe to
MemberwiseClone() will create shallow copy, which will do a bit-by-bit copy of a value
I need a shallow copy of an java ArrayList , should I use clone()
I have read the answer for difference between deep copy and shallow copy from
Just to make sure I'm understanding shallow copies of reference types correctly and that
Possible Duplicates: Deep copy vs Shallow Copy In Java, what is a shallow copy?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.