What is the recommended method of setting the title with setTitle("Title")
or super("Title")
while extending javax.swing.JFrame
in terms of performance?
To explicitly call the superclass constructor from the subclass constructor, we use super() . It's a special form of the super keyword. super() can be used only inside the subclass constructor and must be the first statement.
Note: If a constructor does not explicitly invoke a superclass constructor, the Java compiler automatically inserts a call to the no-argument constructor of the superclass. If the super class does not have a no-argument constructor, you will get a compile-time error.
If we call "super()" without any superclass Actually, nothing will be displayed. Since the class named Object is the superclass of all classes in Java. If you call "super()" without any superclass, Internally, the default constructor of the Object class will be invoked (which displays nothing).
In that case it is necessary to put super(...) and specify the constructor. So it is possible there is not an accessible no arg constructor in which case super is necessary and must have arguments for an accessible constructor.
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")
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With