0

I'm working with a class and getting a call to this class with it as input.

 professor = new Professor(EEEE, AAAA, YEARS_90, DEP);

 depProf.addProfessor(professor)

Professor has a constructor of (String, String, Integer, String), but in a test I want to call it from another place with (Professor) as input.

public Professor(String name, String surname, Integer yearOfBirth, String department) {

    this.department = department;
    Person curPerson = new Person(name, surname, yearOfBirth);

}

How can I make a constructor of Professor(Professor)? If that make any sense.

I'm thinking about calling 1st constructor when second is called but don't actually know how to make it work.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
Rancio
  • 155
  • 1
  • 3
  • 15
  • You just write the constructor, what is confusing you specifically? `public Professor(Professor other) { ... }` and then copy his state or call the other constructor `this(other.getName(), other.getSurname(), other.getYearOfBirth(), other.getDepartment());` – Zabuzard Sep 10 '20 at 19:28
  • Did you try searching the Internet for the words ___java copy constructor___ ? – Abra Sep 10 '20 at 19:29
  • See also this answer to "How do I call one constructor from another in Java?" https://stackoverflow.com/questions/285177/how-do-i-call-one-constructor-from-another-in-java/15348070#15348070 – Christian Fries Sep 10 '20 at 19:32
  • My problem with that is Professor only have department. Name, surname and year are inputs for another class, I can't use this.name cause it will be like Person.name and I don't want to call the object made by Professor – Rancio Sep 10 '20 at 19:54

3 Answers3

5

You can call another constructor in the same class for example:

public Professor(String name, String surname, Integer yearOfBirth) {
   this.name = name;
   this.surname = surname;
   this.yearOfBirth = yearOfBirth;
}

public Professor(String name, String surname, Integer yearOfBirth, String department) {
  this(name, surname, yearOfBirth);
  this.department = department;
}

public Professor(Professor p) {
  this(p.name, p.surname, p.yearOfBirth, p.department);
}

k1r0
  • 542
  • 1
  • 5
  • 12
  • What if my Professor class doesn't actually have name, surname and yearOfBirth? Professor constructor calls Person(String, String, Integer) Professor only have department. – Rancio Sep 10 '20 at 19:51
  • Nevermind, didn't think about the superclass thingy. – Rancio Sep 10 '20 at 20:16
1

You can use this to call another constructor.

public Professor(Professor professor) {
    this(professor.name, professor.surname, professor.yearOfBirth, professor.department);
}
Unmitigated
  • 76,500
  • 11
  • 62
  • 80
0

If I get this right, you are trying to use a copy constructor. This constructor is generated by default in C++, but you have to manually implement one in Java, in your Professor class :

public Professor(Professor p) { 
    this(p.name, p.surname, p.yearOfBirth, p.department);
} 
  • 1
    Note that C++ does not generate _such_ a constructor. It is different. More similar to Javas `clone` mechanism - black voodoo magic. – Zabuzard Sep 10 '20 at 19:32