Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A call to a static method within the parameter list of super() is valid in Java. Why?

Tags:

java

static

Let's look at the following code snippet in Java.

package trickyjava;

class A
{
    public A(String s)
    {
        System.out.println(s);
    }
}

final class B extends A
{
    public B()
    {
        super(method());      // Calling the following method first.      
    }

    private static String method()
    {
        return "method invoked";
    }
}

final public class Main
{
    public static void main(String[] args)
    {
        B b = new B();
    }
}

By convention, the super() constructor in Java must be the first statement in the relevant constructor body. In the above code, we are calling the static method in the super() constructor parameter list itself super(method());.


It means that in the call to super in the constructor B(), a method is being called BEFORE the call to super is made! This should be forbidden by the compiler but it works nice. This is somewhat equivalent to the following statements.

String s = method();
super(s);

However, it's illegal causing a compile-time error indicating that "call to super must be first statement in constructor". Why? and why it's equivalent super(method()); is valid and the compiler doesn't complain any more?

like image 409
Lion Avatar asked Nov 11 '11 22:11

Lion


People also ask

Can we call static method using super?

Where the "super" keyword in Java is used as a reference to the object of the superclass. This implies that to use "super" the method should be invoked by an object, which static methods are not. Therefore, you cannot use the "super" keyword from a static method.

Can super and this be used in static methods Java?

We cannot use this and super keyword in the body of the static method because this keyword is the reference of the current object but without creating an object we can access the static method this will cause an error and in the case of the super keyword, we know very well super is the reference of the parent class but ...

What does super () do in Java?

The super() in Java is a reference variable that is used to refer parent class constructors. super can be used to call parent class' variables and methods. super() can be used to call parent class' constructors only.

Can we use this () and super () in a constructor in Java?

Both super and this keywords in Java can be used in constructor chaining to call another constructor. this() calls the no-argument constructor of the current class, and super() calls the no-argument constructor of the parent class.


2 Answers

The key thing here is the static modifier. Static methods are tied to the class, instance methods (normal methods) are tied to an object (class instance). The constructor initializes an object from a class, therefore the class must already have been fully loaded. It is therefore no problem to call a static method as part of the constructor.

The sequence of events to load a class and create an object is like this:

  1. load class
  2. initialize static variables
  3. create object
  4. initialize object <-- with constructor
  5. object is now ready for use

(simplified*)

By the time the object constructor is called, the static methods and variables are available.

Think of the class and its static members as a blueprint for the objects of that class. You can only create the objects when the blueprint is already there.

The constructor is also called the initializer. If you throw an exception from a constructor and print the stack trace, you'll notice it's called <init> in the stack frame. Instance methods can only be called after an object has been constructed. It is not possible to use an instance method as the parameter for the super(...) call in your constructor.

If you create multiple objects of the same class, steps 1 and 2 happen only once.

(*static initializers and instance initializers left out for clarity)

like image 131
Barend Avatar answered Oct 28 '22 12:10

Barend


Yep, checking the JVM spec (though admittedly an old one):

In the instance init method, no reference to "this" (including the implicit reference of a return) may occur before a call to either another init method in the same class or an init method in the superclass has occurred.

This is really the only real restriction, so far as I can see.

like image 43
Hot Licks Avatar answered Oct 28 '22 12:10

Hot Licks