Reputation: 155
I am learning Java for the first time, and in the tutorial I am watching (https://www.youtube.com/watch?v=grEKMHGYyns), the instructor writes this code:
private Name personName;
public Person(Name personName){
this.personName = personName;
}
Does anyone know what the object 'personName' of the constructor class 'Name' does?
How would I be able to set a value to 'personName' using the constructor 'Name' of class 'Name'?
I would show you the code for 'Name,' but I don't know how to set it up so that the object of 'Name' would hold any value.
Thanks for your help!
Upvotes: 2
Views: 181
Reputation: 4543
Name
can be anything really. I haven't watched the video (wow, it is long) but in this context I guess it could contain a String
value for that person's name.
Based on the information given I assume the class could look like this:
public class Name {
private final String name;
public Name(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
Then to instantiate your Person
, you need an instance of Name
first:
Name name = new Name("AnyName");
With that you can create a new Person
:
Person person = new Person(name);
Upvotes: 2