Results 1 to 4 of 4
  1. #1
    tngo is offline Member
    Join Date
    Jan 2013
    Rep Power

    Question Deep Copy Constructor

    I need some help in implementing deep copy constructor and not using the clone method. I understand that in order for the deep copy to work, you are making a copy of the object and not just the address pointing to that object. Otherwise if you make changes to one the original object will also observe the change. However, I am having problem with putting it into practice.

    Java Code:
      * This is the main method.
      * It creates a Team object and then tests the copy constructor.
      public static void main(String[] args)
    		Team team1 = new Team("The Java Studs", "Bill", "Ted", "Carol", "Alice", 
    					 new Competition("ACM Programming Contest","The Java Studs", "Team University Java", 2008),
    					 new Competition("Antarctic Programming Contest","The Java Studs", "Frigid South", 2010) );
    		Team team2 = new Team(team1);	// Copy constructor
    		// Change competition information for team2
    		team2.getCompetition1().setWinner("Mmm Java");
    		team2.getCompetition1().setRunnerup("Java By Night Java By Day");
    		// Output information for both team1 and team2.
    		// Team1's should be unchanged from the original values.
    Java Code:
    	* Team copy constructor;  Performs a deep copy from the Competition class
    	* instead of just copying a reference to the same Competition objects.
     	public Team(Team otherTeam)
     		Team copyTeam = new Team();
    This is my output from command line when I try to run it.
    Exception in thread "main" java.lang.NullPointerException
    at Team.main(

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

    Default Re: Deep Copy Constructor

    You have things back-to-front in your copy constructor. You need to copy/set the attributes of the passed-in Team to the current instance ("this")

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  3. #3
    tngo is offline Member
    Join Date
    Jan 2013
    Rep Power

    Default Re: Deep Copy Constructor

    It could be that I need coffee this morning but could you help me understand this with an example? I tried fixing the codes but I feel that I'm only referencing the address because the changes are being observed by both team1 and team2.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Deep Copy Constructor

    It's a constructor, not a clone() method, so there is no copyTeam object.
    You need to do:
    Java Code:
    etc etc
    That will give you a second Team object that references the same data that the original Team object references.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. deep copy in java
    By kmm1977 in forum New To Java
    Replies: 1
    Last Post: 12-09-2011, 05:59 PM
  2. [SOLVED] Deep copy
    By thorne_ in forum New To Java
    Replies: 5
    Last Post: 06-05-2009, 03:51 PM
  3. Deep copy?!
    By deepthought015 in forum New To Java
    Replies: 3
    Last Post: 05-07-2009, 06:31 PM
  4. Deep Copy Test
    By Java Tip in forum java.lang
    Replies: 0
    Last Post: 04-16-2008, 11:05 PM
  5. Replies: 0
    Last Post: 12-28-2007, 12:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts