2

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...

kleopatra
  • 51,061
  • 28
  • 99
  • 211
kevinarpe
  • 20,319
  • 26
  • 127
  • 154
  • 1
    It's finished when you reach the line after `super().` You can't even *write* any code before the super() call, so what exactly is the problem here? – user207421 Oct 24 '11 at 05:51
  • 1
    Maybe he is asking what happens if you don't explicitly write the `super()`, or why (where in the docs does it say this) you can't write code before the call? – Konerak Oct 24 '11 at 06:28
  • 1
    please learn java naming conventions and stick to them – kleopatra Oct 24 '11 at 08:40

7 Answers7

3

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.

aroth
  • 54,026
  • 20
  • 135
  • 176
2

How about calling your code after super() finishes like this:

class subclass extends superclass {
    public subclass() {
        super();  // unnecessary, but explicit
        callYourFunctionHereKnowingThatSuperHasFinished();
    }
}
spatulamania
  • 6,613
  • 2
  • 30
  • 26
2

... imagine that super() will also call some class methods. I may override some of these methods in my subclass

The simple answer is "don't do this". It is madness.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • JTable during initialisation will fire events saying table data has changed. It is normal to overide these methods. How can I avoid? – kevinarpe Oct 25 '11 at 04:00
1

Super is finished if the subclass instance was created. If the super() didn't complete, it would never return without exception to finish the constructor of the subclass - i.e. you would never have instantiated the subclass, if the super call didn't complete.

Lionel
  • 428
  • 3
  • 10
1

The super() must be the first statement.

An implied super() is therefore included in each constructor which does not include either the this() function or an explicit super() call as its first statement. The super() statement invokes a constructor of the super class. The implicit super() can be replaced by an explicit super(). The super statement must be the first statement of the constructor.

And since the JVM will wait for the first statement to finish, your second statement is safe to manipulate the object or call super.methods() as it pleases.

Community
  • 1
  • 1
Konerak
  • 39,272
  • 12
  • 98
  • 118
0

If you want to do columns auto size you shoud watch after the dataModel . . The data is in there.

Deian
  • 1,237
  • 15
  • 31
0

The thing you are talking about is called Constructor Chaining.

Every constructor method will call up the chain until the class at the top has been reached and initialized. Then each subsequent class below is initialized as the chain winds back down to the original subclass. This process is called constructor chaining.

So yes, you can safely add your method after the super() call in the constructor of original subclass.

HashimR
  • 3,803
  • 8
  • 32
  • 49