2

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?

4
  • The risk of using super.method is if you then override method 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 use super.method to skip of the new implementation if you wanted to (I've done this with setVisible when I've wanted to do fading animation) - You just need to beware of the risk that using super can introduce... Commented Aug 5, 2014 at 0:10
  • There's also the risk of confusing other developers (as you did to me ;)) as I spent quite a bit of time trying to verify if you have actually overriding these methods (you were using super.xxx with) to ensure that you hadn't done something funny with them (espeically setVisible) which you were trying to circumvent... Commented Aug 5, 2014 at 0:13
  • One of the reasons I use this and super, is because eclipse automatically gives me the available suggestions ;)
    – Ben Beri
    Commented Aug 5, 2014 at 14:40
  • Just beware of the potential issues it might cause you or other uses Commented Aug 5, 2014 at 21:15

1 Answer 1

7

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.

4
  • 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 if JPanel has a working setSize() method, its subclasses should have it, too.
    – SJuan76
    Commented 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 and this for the other more standard cases.
    – metacubed
    Commented Jul 27, 2014 at 9:28
  • @SJuan76 I rephrased some parts for emphasis - does that seem ok now?
    – metacubed
    Commented Jul 27, 2014 at 9:38
  • Yes it seems better.
    – SJuan76
    Commented Jul 27, 2014 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.