0

Based on the following example :

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;

public class ExempleJFrame extends JFrame {

    public ExempleJFrame() {
        super("JFrame example");
        setLocation(50, 50);
        setSize(200, 200);
        getContentPane().add(new JLabel("This is a text label!", SwingConstants.CENTER));
        setDefaultCloseOperation(EXIT_ON_CLOSE);
    }

    public static void main(String[] args) {
        ExempleJFrame frame = new ExempleJFrame();
        frame.setVisible(true);
    }

}

Is there a way to make it more explicit that I am calling a member method (see setLocation() and setSize())?

I am looking for something in the lines of this.setLocation() to make it explicit that I call a member method and not something from outer space.

JF Dion
  • 4,014
  • 2
  • 24
  • 34
  • 3
    so what prevents you from doing like you say => this.setX()? – Juvanis Oct 26 '12 at 13:10
  • My question is : is it possible to do it, since all examples I have found don't do it like that – JF Dion Oct 26 '12 at 13:11
  • 3
    If `setLocation()` is a member inherited form `JFrame`, use `super.setLocation()`. If it is defined in `ExempleJFrame` use `this.setLocation()`, simple as that. – Less Oct 26 '12 at 13:11
  • @Less : is it considered a good practice to do it like this? or am I looking to add useless noise? – JF Dion Oct 26 '12 at 13:13
  • Are you actually worried that using `this` pointer for invocation can introduce some overhead? This would be the only "noise" I could care about, and that is not the case, AFAIK. So, you are completely free to use `this` or `super`, and make your code more readable and nice looking. – Less Oct 26 '12 at 13:28

3 Answers3

2

If you're using Eclipse, you can set a Save Action (Additional Save Actions -> Member Accesses -> Use 'this' qualifier for method accesses'), which will automatically convert setLocation to this.setlocation on save.

Personally I don't find it particularly useful, because a non-prefixed method call can only be either

  • a static method call (which will be italic in Eclipse)
  • a this member call
  • a super member call

The last two rarely need to be distinguished IMHO, because of run time dispatch.

artbristol
  • 32,010
  • 5
  • 70
  • 103
1

The good practice is to favour composition over inheritance: don't extend JFrame but delare a JFrame yourFrame; member in your class instead. Then you call the JFrame methods naturally with:

yourFrame.someMethod();

ps: and call your GUI methods from the EDT:

public static void main(String[] args) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            yourFrame = new JFrame("JFrame example");
            initFrame();
            yourFrame.setVisible(true);
        }
    }
}
Community
  • 1
  • 1
assylias
  • 321,522
  • 82
  • 660
  • 783
0

as other said in the comments you could use this.setwatever(). usually it is considered a good practice to use this.membervariable but never really came accross code using this.method().but it works perfectly fine.

PermGenError
  • 45,977
  • 8
  • 87
  • 106