Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "this()" do in a constructor?

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!!!

like image 450
user200340 Avatar asked Mar 03 '10 19:03

user200340


People also ask

What does this refer to in a constructor function?

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.

Can we use this () and super () in a constructor?

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.

Can we use this () in a method?

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.

What does this :: mean in Java?

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.


2 Answers

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.

like image 177
Andy White Avatar answered Sep 23 '22 12:09

Andy White


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.
    }
}
like image 28
JasCav Avatar answered Sep 20 '22 12:09

JasCav