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!!!
The JavaScript “this” keyword has a special meaning inside of that object: it refers to itself. In other words, when you create your constructor function, you can use the “this” keyword to reference the object that WILL be created when the constructor is instantiated.
Both super and this keywords in Java can be used in constructor chaining to call another constructor. this() calls the no-argument constructor of the current class, and super() calls the no-argument constructor of the parent class.
this() can be used to invoke current class constructor. this can be passed as an argument in the method call. this can be passed as argument in the constructor call. this can be used to return the current class instance from the method.
The double colon (::) operator, also known as method reference operator in Java, is used to call a method by referring to it with the help of its class directly. They behave exactly as the lambda expressions.
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.
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.
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With