What does “this()” method mean?

懵懂的女人 提交于 2019-11-28 17:46:35
Avi

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

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.

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.

It's nearly the same

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

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

}

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).

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

Digraph(int param) { /*  */ }
LMK

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 
    }

}

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:)

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!