Reputation: 3371
I have two questions about the following code. 1. How to constructor the third constructor without using setter? 2. what does this() do in the last constructor.
public class Person { private String name; private String address; Person(){} Person(String name){ this.name = name; } Person(String address){ //Person(java.lang.String) is already defined. } Person(String name,String address){ this(); this.name = name; this.address = address; } }
My solution for question is Person(Object address){ this.address = (String)address; } However, i am not sure about this.
and i think this(); in the last constructor calls constructor Person(){}, but if it does, is it mean that two Person objects are created when i call
Person p = new Person("myName","myAddress");
Thanks!!!
Upvotes: 8
Views: 6395
Reputation: 45606
Use factory methods to construct this object
public class Person {
private String name;
private String address;
public Person(String name, String address) {
this.name = name;
this.address = address;
}
public static Person createHomelessPerson ( String name )
{
return new Person( name, null );
}
public static Person createNamelessPerson ( String address )
{
return new Person( null, address );
}
}
Upvotes: 1
Reputation: 22741
You are right about the last statement, but two Person objects are not created. Internally, this is just like a method call. I don't understand what you're trying to achieve with this code. To invoke the last constructor, new Person("1", "2")
, or internally this("1", "2")
in a constructor. There are also no setters here. Setters are methods like setName(String)
, setAddress(String)
and so on. Getters are methods like String getName()
, String getAddress()
.
As another answer said, you also can't have two constructors with the same argument types. Just create one constructor with everything you need to set in it.
Upvotes: 0
Reputation: 34652
Something to think about though - do you WANT a Person object created that doesn't have a name or address? If you don't, why offer those constructors (not that you can anyway - they have the same parameters, so the compiler can't differentiate them)? I would think every person would have a name. Maybe someone wouldn't have an address.
Think about your object before creating your constructors.
Another possibility is:
public class Person {
private String name;
private String address;
Person(String name) {
this(name, "");
}
Person(String name, String address) {
this.name = name;
this.address = address;
// TODO - Other initializations.
}
}
Upvotes: 4
Reputation: 88475
The problem with Person(String name)
and Person(String address)
is that you can't have two constructors with the same parameters. The compiler will not know which one to call when you want to call something like this: new Person("Joe Blow");
You could do something like this instead:
Person(String name)
{
this.name = name;
}
Person(String name, String address)
{
this(name);
this.address = address;
}
The "this()
" in your last constructor is just telling that constructor to call the default constructor as part of the process of constructing the object. It does not create two objects, it just runs the code in the def. constructor, which in your case, does nothing.
Upvotes: 12
Reputation: 60105
Upvotes: 0