Calling a superclass with super or just setTitle?

What is the recommended method for setting a header with setTitle("Title") or super("Title") when expanding javax.swing.JFrame in terms of performance?

+6
source share
3 answers

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

So, since implicit super() added if you do not specify a call to any super constructor yourself, it would be (albeit very little) more efficient to call super("Title") .

+4
source

If you are in your constructor, try delegating as many functions to the super constructor as possible. Perhaps you can save him from doing any work.

For example, the default super constructor may create some internal objects that will simply be overwritten when the setter is called. If you pass the correct data right away, you will give him the opportunity to be more efficient.

But I think that in this particular case it does not really matter.

+1
source

It is good practice to always call the appropriate super (.....) when extending a class. Since you never know what magic a super constructor does behind the scenes.

You don't need to just

call super ();

What will happen if you do not specify anything else. You need to specify a constructor to call if:

  • You want to call the superclass constructor that has parameters
  • You want to bind to another constructor in the same class instead of the superclass constructor
0
source

Source: https://habr.com/ru/post/927954/


All Articles