I have been programming java for like a year or more, and i have always used the this
and super
keywords.
And yesterday my mate read one source of mine, and told me not to over use it unless you are overriding the method.
class A extends JPanel {
public A {
super.setSize(400, 400);// instead of not using super keyword
}
}
But what he meant on how to use it, is this:
class A extends JPanel {
public A {
super.setSize(4,4);
}
public void setSize(int a, int b) {}
}
So the language will know that your constructor calls the parent method and not local.
Should i only use the the keywords in this situation only or use the keywords everywhere for better readibly?
I know that this
keyword is returning the whole local instance including the parent and all of its parents and interfaces.
And the super
keyword returns the instance of the parent class and all of its instances (parents and interfaces).
But my question is, should you always use these keywords?
1 Answer 1
You need to be aware of all the special cases where it is compulsory to use super
or this
. You described one of those corner cases - overridden functions. You will learn more special cases as you go on.
Other than those specific cases, using this
and super
is a matter of preference. They produce the same machine code, and so they are exactly the same as far as the machine is concerned. However, they differ in terms of their readability to a person.
It is important to pick one style and use it consistently. By this, I mean not just you, but your entire team. Everyone decides on one way of doing things and sticks to it. This makes it easy to understand the team's code. This is called a "coding standard".
Check if there are any published coding guidelines or coding standards set by the company or the team. If yes, just follow them. Otherwise, see what the rest of the team normally uses. You may want to discuss this with your team to officially decide on a standard.
-
Uh, no. In his example, he is making
setSize
a dummy function that does nothing, so in order to actually set a size he is calling the parent implementation from the constructor. Not the best of designs, of course, because ifJPanel
has a workingsetSize()
method, its subclasses should have it, too.SJuan76– SJuan762014年07月27日 09:24:12 +00:00Commented Jul 27, 2014 at 9:24 -
@SJuan76 I think that's just some placeholder code to show the corner case. The question is whether to use
super
andthis
for the other more standard cases.metacubed– metacubed2014年07月27日 09:28:10 +00:00Commented Jul 27, 2014 at 9:28 -
@SJuan76 I rephrased some parts for emphasis - does that seem ok now?metacubed– metacubed2014年07月27日 09:38:58 +00:00Commented Jul 27, 2014 at 9:38
-
super.method
is if you then overridemethod
within the same class,method
will no longer be called, but instead the parent implementation will be called, this introduces unnecessary risk into the code which could produce unexpected results. Of course, you could usesuper.method
to skip of the new implementation if you wanted to (I've done this withsetVisible
when I've wanted to do fading animation) - You just need to beware of the risk that usingsuper
can introduce...super.xxx
with) to ensure that you hadn't done something funny with them (espeicallysetVisible
) which you were trying to circumvent...