call to super() must be the first statement in constructor body

10,042

Java enforces that the call to super (explicit or not) must be the first statement in the constructor. This is to prevent the subclass part of the object being initialized prior to the superclass part of the object being initialized.

In your case, you don't do anything but local "trivial computation", so all things considered, it would be okay. However, Java's compiler doesn't go to the level of determining whether statements before a call to super actually do any initialization. It just disallows all statements before super().

Share:
10,042
matteo
Author by

matteo

Updated on July 24, 2022

Comments

  • matteo
    matteo almost 2 years

    I'm writing the constructor of a LoginRequest class which extends a class called JsobObjectRequest (from the Volley framework in Android, but that's completely irrelevant to the question)

    With this code :

     public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
            Boolean hasCredentials=(username!=null && password!=null);
            int method=hasCredentials? Method.POST:Request.Method.GET;
            super(method, API_URL_LOGIN, null, responseListener, errorListener);
    
            this.username=username;
            this.password=password;
    
        }
    

    I get the error: call to super() must be the first statement in constructor body

    Instead, this code compiles just fine:

     public LoginRequest(String username, String password, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
            super((username!=null && password!=null)? Method.POST:Request.Method.GET, API_URL_LOGIN, null, responseListener, errorListener);
    
            this.username=username;
            this.password=password;
    
        }
    

    But isn't it effectively the exact same thing? In both cases, a couple of trivial computation are made prior to calling the super constructor, based on the values of the parameters passed to the subclass constructor. Why shouldn't the compiler be able to compile the first example given that it can compile the second?

    Is the calling-super-constructor-must-be-first-statement specification more simplistic than it would need to be, or am I missing something?

    EDIT: this has been wrongly marked as duplicate of Why do this() and super() have to be the first statement in a constructor?. That question is much more generic and asks why super() have to be the first statement at all. The question here is why a case like the one I've posted would defeat those requirements (and it has been satisfactorily answered in this very question)