this() method in java | Java Training

This Java Training  “this() method in java” post explains clearly what is the use of this() method and how to connect constructors using this() method in java application.


 

this():

  • this() is used to invoke current(same) class constructor explicitly.
  • Used to initialize an object via multiple constructors.
  • Connecting constructors using this(), is called “Constructor Chaining”.
  • We must use this() only inside another constructor of same class.
  • Call to this(), must be the first statement in another constructor.

 

  • We can connect number of constructors in the application.
  • The process of connecting constructors is the concept of ‘constructor chaining’.

 

  • Recursive constructor invocation is not allowed.
  • Recursion is the concept of calling constructor from the same constructor.
  • In case of recursion, JVM keep on creating objects in the heap memory in never ending process.

 

Share this