Results 1 to 4 of 4
  1. #1
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default Re: What is the point of a copy constructor?

    You need to clarify your question because it is rather confusing, and you should paste the relevant code in the post rather than paste a link to your code.

    What I've made of your question is this: What happens if the parameter objects Instructor inst and TextBook text are not initialised when you create a new Course object. The answer is that they will become a null pointer until they are initialised. If you try to access methods or fields before the object is initialised it will cause a NullPointerException.

    Java Code:
    //some code to try out your class
    ...
    Instructor instructor1; //not initialiased: value is 'null'
    TextBook text1; //null
    Course science = new Course("Science", instructor1, text1);
    ...
    
    //the Course class
    class Course {
    //variables
    ...
    public Course(String name, Instructor inst, TextBook text) {
        //try to access the fields
        instructorName = inst.getLastName(); //NullPointerException is thrown
    }
    I don't know what you mean by a 'copy' constructor. Perhaps you mean this:

    Java Code:
    class SomeClass {
        SomeClass() {
            //a blank constructor
        }
        SomeClass(String str) {
            //another constructor
            someVariable = str;
        }
    }
    If thats what you mean, it is known as Polymorphism in Java and maybe other languages.
    Both methods are distinguished by their 'method signature' - The first one has no params, but the second one has a String param.
    So long as you have different combinations of parameters you can create as many as you like to suit your convenience.

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,574
    Rep Power
    12

    Default Re: What is the point of a copy constructor?

    My question is this: What problems might occur if the constructor of the Course class does not call the
    copy constructors for its instructor and textbook instance variables?
    I think the question might getting at the fact that someone with an instance of one course could get the textbook and set that textbook's title to something else. Likewise they could give the instructor a new and amusing name. The point is that these changes be seen in all the courses involving this textbook or instructor if they weren't "defensive" and clone the instances.

    Copy constructors are no panacea! What problems might occur if the constructor of the Course class does call the copy constructors for its instructor and textbook instance variables?

  3. #3
    bayan.rafeh is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: What is the point of a copy constructor?

    To clarify the question, what is the difference between the two implementations of the Course constructors

    Java Code:
    public Course(String name, Instructor inst, Textbook text){
          courseName = name;
          instructor = new Instructor(inst);
          textBook = new Textbook(text);
    }
    Java Code:
    public Course(String name, Instructor inst, Textbook text){
          courseName = name;
          instructor = inst;
          textBook = text;
    }
    All i found was that it results in unnecessary object creation which means less pointers and I don't really see how that would help even on a large scale

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,185
    Rep Power
    20

    Default Re: What is the point of a copy constructor?

    pbrockway has pretty much explained that, though.

    The implication of the first constructor is that the Course should not be able to make changes to the original Instructor or TextBook objects. However, in that instance I would have made both of them immutable and passed those in instead.

    You would normally see a copy constructor in getters, to be honest.
    Java Code:
    Date getDate() {
        return new Date(date.getTime());
    }
    which will ensure anyone getting the date cannot change the value of it in my object.

Similar Threads

  1. Replies: 5
    Last Post: 12-14-2011, 02:47 PM
  2. array copy in class constructor.
    By Juukamen in forum New To Java
    Replies: 2
    Last Post: 10-29-2011, 01:07 AM
  3. Replies: 16
    Last Post: 07-08-2010, 10:13 PM
  4. copy constructor
    By shadow7 in forum New To Java
    Replies: 6
    Last Post: 12-29-2009, 07:56 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
  •