Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Copying objects through constructors?

  1. #1

    Default Copying objects through constructors?

    I have a class named Cash shown below
    public class Cash 
    {
    	//Quanity Field and RetailItem Object field
    	private int total_units;
    	private Retail myRetail;
     
     
    	//Create first constructors
    	public Cash()
    	{
    		this(0,null);
     
    	}
    	//Second constructor
    	public Cash(int total_units)
    	{
    		this(total_units,null);
    	}
     
    	//Create third constructor
    	public Cash(Retail myRetail)
    	{
    		this(0,myRetail);
    	}
     
    	//Create a a constructor
    	public Cash(int total_units,Retail myRetail)
    	{
    			this.total_units=total_units;
    			this.myRetail=new Retail(myRetail);
    	}

    I'm seriously need help to understand the concept of making shallow copies and deep copies. My book has shown me that its better to perform deep copies and I have followed that method. if you guys look in my constructor of the cash class
    //Create a a constructor
    	public Cash(int total_units,Retail myRetail)
    	{
    			this.total_units=total_units;
    			this.myRetail=new Retail(myRetail);

    Apparently I have field in Cash Class named
    Retail myRetail
    when I pass in an argument from my demo, I'm making a copy of that object from the demo class.

    In my retail class, I have the following copy constructor
    //Make a copy constructor
    		public Retail(Retail Object1)
    		{
    			Item_Name=Object1.Item_Name;
    			Item_Number=Object1.Item_Number;
    			// if I use this then my program would work//this.cost=Object1.cost;
     
    //if I use this part of code below, my program won't work at all and I would get an error saying Exception in thread "main" java.lang.NullPointerException
    			this.cost.Item_Cost=Object1.cost.Item_Cost;
    			this.cost.Wholesale_Cost=Object1.cost.Wholesale_Cost;
    		}

    My question is why can't I perform a deep copy there. I know if I do
    this.myRetail=myRetail
    in my cash constructor it would work, but then the book says its not a good method; however, even my teacher uses this so I'm confused here wahts going on.
    Last edited by fahman_khan75@yahoo.com; April 4th, 2014 at 05:34 PM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    4,364
    My Mood
    Mellow
    Thanks
    143
    Thanked 598 Times in 585 Posts

    Default Re: Copying objects through constructors?

    This:

    this.cost.Item_Cost=Object1.cost.Item_Cost;

    is an odd looking statement. Is there a Cost class? If so, please show the whole Retail and Cost classes. If there is not a Cost class, specify what cost, Item_cost, and Wholesale_Cost are. (The correct naming per Java convention should be itemCost and wholesaleCost.)

Similar Threads

  1. How to use constructors and class objects together.
    By IkeIII in forum Object Oriented Programming
    Replies: 5
    Last Post: April 1st, 2013, 11:16 AM
  2. Copying a File
    By ubiByte in forum What's Wrong With My Code?
    Replies: 10
    Last Post: October 19th, 2012, 06:55 PM
  3. Copying Objects
    By MethodMan in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2011, 02:41 AM
  4. Copying Arrays
    By AnnexTrunks in forum What's Wrong With My Code?
    Replies: 30
    Last Post: October 24th, 2011, 10:04 PM
  5. [SOLVED] Overloading constructors(Multiple Constructors)
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 11th, 2011, 12:55 PM