File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes super(x,y) and this() in one constructor Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "super(x,y) and this() in one constructor" Watch "super(x,y) and this() in one constructor" New topic

super(x,y) and this() in one constructor

nimo frey
Ranch Hand

Joined: Jun 28, 2008
Posts: 580
I have this:

You see, I want to call a super-constructor and a this-constructor within a constructor of a sub-class. Why does this not work?

The debugger says:

"Constructor call must be the first statement in a constructor"

Changing the order of super() and this() within the constructor does not help.
John Jai

Joined: May 31, 2011
Posts: 1776
You can either have a super() or this() not both. The error states why. Both calls another constructor and that should be the first line in your constructor code.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4542

nimo frey wrote:

You can get what appears to be the effect you want like this, without violating the rules:
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46320
But in real life, avoid passing nulls as far as possible.
I agree. Here's the link:
subject: super(x,y) and this() in one constructor
It's not a secret anymore!