Sugihara
Sugihara

Reputation: 1101

What does "this()" method mean?

I ran into this block of code, and there is this one line I don't quit understand the meaning or what it is doing.

public Digraph(In in) {
    this(in.readInt()); 
    int E = in.readInt();
    for (int i = 0; i < E; i++) {
        int v = in.readInt();
        int w = in.readInt();
        addEdge(v, w); 
    }
}

I understand what this.method() or this.variable are, but what is this()?

Upvotes: 48

Views: 45615

Answers (8)

Avi
Avi

Reputation: 21858

This is constructor overloading:

public class Diagraph {

    public Diagraph(int n) {
       // Constructor code
    }


    public Digraph(In in) {
      this(in.readInt()); // Calls the constructor above. 
      int E = in.readInt();
      for (int i = 0; i < E; i++) {
         int v = in.readInt();
         int w = in.readInt();
         addEdge(v, w); 
      }
   }
}

You can tell this code is a constructor and not a method by the lack of a return type. This is pretty similar to calling super() in the first line of the constructor in order to initialize the extended class. You should call this() (or any other overloading of this()) in the first line of your constructor and thus avoid constructor code duplications.

You can also have a look at this post: Constructor overloading in Java - best practice

Upvotes: 57

Antimony
Antimony

Reputation: 39451

It's nearly the same

public class Test {
    public Test(int i) { /*construct*/ }

    public Test(int i, String s){ this(i);  /*construct*/ }

}

Upvotes: 4

N&#225;ndor Kr&#225;cser
N&#225;ndor Kr&#225;cser

Reputation: 1128

An other constructor of the class Digraph with an int parameter.

Digraph(int param) { /*  */ }

Upvotes: 3

user27
user27

Reputation: 146

this(); is constructor which is used to call another constructor in a class, for example:-

class A{
  public A(int,int)
   { this(1.3,2.7);-->this will call default constructor
    //code
   }
 public A()
   {
     //code
   }
 public A(float,float)
   { this();-->this will call default type constructor
    //code
   }
}

Note: i did not use this() constructor in default constructor because it will lead to deadlock state.

Hope this will help you:)

Upvotes: 2

LMK
LMK

Reputation: 2952

Constructor Overloading:

ex:

public class Test{

    Test(){
        this(10);  // calling constructor with one parameter 
        System.out.println("This is Default Constructor");
    }

    Test(int number1){
        this(10,20);   // calling constructor with two parameter
        System.out.println("This is Parametrized Constructor with one argument "+number1);
    }

    Test(int number1,int number2){
        System.out.println("This is Parametrized  Constructor  with two argument"+number1+" , "+number2);
    }


    public static void main(String args[]){
        Test t = new Test();
        // first default constructor,then constructor with 1 parameter , then constructor with 2 parameters will be called 
    }

}

Upvotes: 2

user2228462
user2228462

Reputation: 619

Calling this essentially calls the class Constructor. For example, if you're extending something, than along with add(JComponent), you could do: this.add(JComponent).

Upvotes: 3

Boris the Spider
Boris the Spider

Reputation: 61148

This code snippet is a constructor.

This call to this calls another constructor of the same class

public App(int input) {
}

public App(String input) {
    this(Integer.parseInt(input));
}

In the above example we have a constructor that takes an int and one that takes a String. The constructor that takes a String converts the String to an int and then delegates to the int constructor.

Note that a call to another constructor or a superclass constructor (super()) must be the first line in a constructor.

Maybe take a look at this for a more detailed explanation of constructor overloading.

Upvotes: 10

Sinkingpoint
Sinkingpoint

Reputation: 7624

Using this() as a function like that, essentially calls the Constructor of the class. This allows you to all the generic initializations in one constructor and have specializations in others. So in this piece of code for example, the call to this(in.readInt()) is calling the Digraph constructor that has one int argument.

Upvotes: 11

Related Questions