Johan Lindskogen
Johan Lindskogen

Reputation: 1306

Calling superclass constructor with super or just setTitle?

What is the recommended method of setting the title with setTitle("Title")or super("Title") while extending javax.swing.JFrame in terms of performance?

Upvotes: 7

Views: 1193

Answers (3)

Frank
Frank

Reputation: 15661

It is a good practice to always call the corresponding super(.....) when you extend a class. As you never know what magic the super constructor does behind the scene.

You never need just

call super();

That's what will be there if you don't specify anything else. You only need to specify the constructor to call if:

  • You want to call a superclass constructor which has parameters
  • You want to chain to another constructor in the same class instead of the superclass constructor

Upvotes: 0

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17445

If you're in your constructor, try to delegate as much functionality as possible to the super constructor. It's possible that you can save it from doing some work.

For instance, the default super constructor might create some inner objects that will just get overwritten when you call the setter. If you pass the correct data immediately, you give it the opportunity to be more efficient.

But I think in this specific case, it does not matter much.

Upvotes: 1

Keppil
Keppil

Reputation: 46239

If you grepcode JFrame (in OpenJDK 6-b14), and dig a bit, you see that the constructor JFrame() calls the constructor Frame(), which calls Frame("") (link).

So, since an implicit super() is added if you don't specify a call to any super constructor yourself, it would be (although very slightly so) more effective to call super("Title").

Upvotes: 5

Related Questions