-2

What does the makeDeepCopy Method mean? Also is it a constructor? and why is the data type the same as the class name. I assumed any method with the same name as the class would be a constructor?

public class Name {

    // private instance => visible in Name class only!
    private String firstName;
    private String lastName;

    // constructor
    public Name(String firstName, String lastName) {
        // this keyWord differentiates instance variable from local variable
        // refers to the current object
        this.firstName = firstName;
        this.lastName = lastName;
    }


    public Name(Name name) {
        // Copy constructor
        this.firstName = name.getFirstName();
        this.lastName = name.getLastName();
    }

    public static Name makeDeepCopy(Name name) {
        // copy method
        return new Name(name);
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String toString() {
        return this.firstName + " " + this.lastName;
    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
JustForLong
  • 109
  • 8
  • 1
    No it is not a constructor, but it does call the constructor. It creates a new object with the same info as the object passed in. – GBlodgett Feb 05 '19 at 23:38
  • `static` just means you don't need an instance of the object to call the method - arguable if it's needed here, but since you have the copy constructor it's one in the same thing – MadProgrammer Feb 05 '19 at 23:41
  • Perhaps duplicate: [Deep copy, shallow copy, clone](https://stackoverflow.com/questions/6182565/deep-copy-shallow-copy-clone) – GBlodgett Feb 05 '19 at 23:41
  • So its just an ordinary method? What would you call it? object method, since it takes in an object – JustForLong Feb 05 '19 at 23:42
  • 1
    It's called a static factory method, and in this case it's probably used so the method name can convey extra information about what it does. See e.g. https://stackoverflow.com/a/929193/2891664 – Radiodef Feb 05 '19 at 23:43
  • Ya its an example code for Deep copy/ shallow copy. I was just confused about that method because I've never seen one like it before. – JustForLong Feb 05 '19 at 23:43
  • Don't be confused. Just read the code. It does what it does in pretty straight-forward way. It just happens that for this class, deep and shallow copies basically mean the same thing. (Which is probably what the explanation accompanying the example code says!) – Stephen C Feb 06 '19 at 00:21

1 Answers1

1

It does not have the same name as the class, the name of the class is Name, while the name of the method is makeDeepCopy. The Name that you see is just the return type.

makeDeepCopy is ingesting a Name object and creating a new Name object with the same values. The Name constructor right above it (that ingests Name name) is called by makeDeepCopy and creates a new Name object with the same data as the Name object passed to makeDeepCopy.

Sam
  • 74
  • 1
  • 4