This week's giveaway is in the Cloud/Virtualization forum.
We're giving away four copies of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP and have Lucian Maly on-line!
See this thread for details.
Win a copy of Secure Financial Transactions with Ansible, Terraform, and OpenSCAP this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 261
15
Android Angular Framework Spring AngularJS Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 72480
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 175
17
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
What are you saying? I thought you said that Santa gave you that. And this tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic