This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between using this() and new() in java  RSS feed

 
Joy Vergis
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HI,

Please help me in understanding the difference in execution in below 2 codes. I believe both try to perform recursive constructor call but then.

Code1: Results in runtime error.


Code2: Results in Compile time error.
 
L Foster
Ranch Hand
Posts: 242
14
Android Angular Framework AngularJS Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Joy,
The "this()" call (with parens!) is a call to the current object's no-args constructor. The "this(12)" calls a different, one-argument constructor, and passes in 12. You'll notice this is called from within Test1's constructor? Well, it is just being called to make sure that all the functionality in the one-arg constructor, is carried out when someone calls the no-arg Test1 constructor. The reason you would write both of these, is that you want the object (an instance of Test1) to be created consistently, no matter how many arguments are used. Also, the this(12) call can be thought of as supplying a default.

When you call 'new Test1()' from the main method, you are requesting that a new instance of Test1 be constructed. This is where the caller creates a Test1 object. The "this()" call is how the construction of the object is carried out. In fact "this()" can only be called from a constructor.

To repeat differently, and briefly, new Test1() is called from outside of Test1, to get a Test1 object. this(12) is called from one of Test1's constructors, to make sure all the same code is used, as if new Test1(12) had been called.

I hope it helps.
 
Campbell Ritchie
Marshal
Posts: 55678
161
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
L Foster wrote:. . . In fact "this()" can only be called from a constructor. . . .
More precisely, as the first statement in a constructor.
You cannot get new() to compile. I presume you meant the use of the new operator.

It is no good saying runtime error without providing all the details of the error. I think I can guess what the error was however.
In the second case you are calling each constructor from the other and making the compiler go round in circles, so you cannot give a meaning to those two this() calls. You must have at least one constructor without this() so you can get to the superclass' constructor.
 
Joe Bishara
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joy Vergis wrote:...I believe both try to perform recursive constructor call...

Yes they both perform a recursive constructor call, however, the compiler cannot detect every recursion that can possibly lead to a stack overflow.

Before Java 1.4.1, it was a compile-time error when a constructor directly invoked itself, not when a constructor indirectly invoked itself, so Code2 wouldn't have caused a compile-time error before Java 1.4.1.

Enhancements and Changes in J2SE 1.4.1 Platform
As of Java 2 SDK 1.4.1, the compiler detects all cases of mutually recursive constructors. Previously, the compiler only detected a self-recursive constructor.

JLS
It is a compile-time error for a constructor to directly or indirectly invoke itself through a series of one or more explicit constructor invocations involving this.

Code1 does not meet this criteria.
Code2 meets this criteria, hence the compile-time error.

Maybe a future Java version may provide further enhancements.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!