If it does not, Java will insert a call to the default super constructor. If it does not exist, you get a compile time error.
So, what if you need to do things, before the super call takes place?
As the object is in the process of being constructed (that's why it's called a constructor), the object is in a non-valid state. This likely explains why the language designers felt that a call to another constructor should be the first part of the body of a constructor.
Well, apparently the only solution is to do everything you need inside the expressions that take the place of the super constructor arguments.
I was forced to do just that in my MudWebException.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.ws.rs.WebApplicationException; | |
import javax.ws.rs.core.Response; | |
/** | |
* | |
* @author mr.bear | |
*/ | |
public class MudWebException extends WebApplicationException | |
{ | |
/** | |
* Create a new MudWebException. | |
* | |
* @param name the name of the player. | |
* @param message the message to send to the player. | |
* @param status the HTTP status code. | |
*/ | |
public MudWebException(String name, String message, Response.Status status) | |
{ | |
super(message, (new ErrorDetails(name, message)).getResponse(status)); | |
} | |
/** | |
* Create a new MudWebException. | |
* | |
* @param name the name of the player. | |
* @param message the message to send to the player. | |
* @param status the HTTP status code. | |
* @param errormessage the error message to log in GlassFish (which can provide additional information too sensitive for the user). | |
*/ | |
public MudWebException(String name, String message, String errormessage, Response.Status status) | |
{ | |
super(errormessage, (new ErrorDetails(name, message)).getResponse(status)); | |
} | |
/** | |
* Create a new MudWebException caused by a different exception. | |
* | |
* @param name the name of the player. | |
* @param message the message to send to the player. | |
* @param status the HTTP status code. | |
* @param e the underlying exception that was thrown. | |
*/ | |
public MudWebException(String name, String message, Throwable e, Response.Status status) | |
{ | |
super(e, (new ErrorDetails(name, message, e)).getResponse(status)); | |
} | |
/** | |
* Create a new MudWebException caused by a different exception. | |
* | |
* @param name the name of the player. | |
* @param status the HTTP status code. | |
* @param e the underlying exception that was thrown. | |
*/ | |
public MudWebException(String name, Throwable e, Response.Status status) | |
{ | |
super(e, (new ErrorDetails(name, e)).getResponse(status)); | |
} | |
/** | |
* Create a new MudWebException caused by a different exception. | |
* | |
* @param status the HTTP status code. | |
* @param e the underlying exception that was thrown. | |
*/ | |
public MudWebException(Throwable e, Response.Status status) | |
{ | |
super(e, (new ErrorDetails(e)).getResponse(status)); | |
} | |
} |
So, there's an easy workaround, but it feels clumsy.
Take care not to access any of the methods or properties of the object that is being constructed, as it is in a non-valid state. As a matter of fact, I think it's a really bad idea to call methods in the object from within your constructor.
References
- [1] JLS 8.0 Section 8.8.7. Constructor Body
- https://docs.oracle.com/javase/specs/jls/se8/html/jls-8.html#d5e14100
- StackOverflow - Why does this and super have to be the first statement in a constructor?
- http://stackoverflow.com/questions/1168345/why-does-this-and-super-have-to-be-the-first-statement-in-a-constructor
No comments:
Post a Comment