Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java, how can we know when super() is finished in constructor?

AFAIK: When a subclass is created, all constructors implicitly or explicitly call super().

Example:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
    }

How can I know when super() is finished? I am creating a subclass of JTable that needs to auto-fit columns, but can only do so safely after super() is finished.

-- Edit --

To clarify based on comments below, imagine that super() will also call some class methods. I may override some of these methods in my subclass. I want to modify behaviour in these methods during base class construction. Perhaps certain required members will not yet be (completely) initialised...

like image 386
kevinarpe Avatar asked Jul 08 '26 01:07

kevinarpe


1 Answers

super() is finished when the call to super() returns (the same applies to any other method invocation, as well).

If you are calling it explicitly as per your example, then super() has finished executing when the line immediately after the super() call is reached. If you are allowing it to be called implicitly, then super() has finished by the time execution reaches the first line in your constructor.

Of course, it may be that case that super() is spawning one or more background threads which perform various tasks and which are still executing at the time when the call to super() returns. But that really has nothing to do with the call to super(). In such a case the time when the other tasks finish has nothing whatsoever to do with the time when the invocation of super() finishes.

like image 124
aroth Avatar answered Jul 10 '26 13:07

aroth