×

Discussion Board

Results 1 to 2 of 2
  1. #1
    Registered User
    Join Date
    Mar 2003
    Location
    Malaysia-Johor-Pontian Kecil
    Posts
    163

    Impact between reference call and static call of methods.

    Hi,

    I've one question here:

    ****************************************************
    public class MyMidlet extends MIDlet {
    ...
    public void startApp() {
    ...
    MyCanvas canvas = new Canvas(this);
    canvas.init();
    display.setCurrent(canvas);
    ...
    }

    public void methodA() {
    System.out.println("I'm not a static method.");
    }

    public static void methodB() {
    System.out.println("I'm a static method.");
    }
    ...
    }

    ****************************************************

    public class MyCanvas extends Canvas {
    ...
    private MyMidlet midlet;

    public MyCanvas(MyMidlet midlet) {
    super();
    this.midlet = midlet;
    }

    public void init() {
    // called by reference: myMidlet
    midlet.methodA();
    // direct call
    MyMidlet.methodB();
    }
    ...
    }

    ****************************************************

    Inside the init() of MyCanvas, I invoke both of methodA() and methodB(). They are doing the same job. The differences between them is that methodB() is a static method. That's why I've to pass the reference of MyMidlet into the construction of MyCanvas in order to call methodA() by the reference. I'm wondoring which way is better. I've read more examples which are like to pass the reference instead of using static method. But according to the tips from http://developer.sonyericsson.com/si..._java_0104.jsp . Static is the fastest way.

    Please advice me. Thanks

  2. #2
    Super Contributor
    Join Date
    Mar 2003
    Location
    Israel
    Posts
    2,280
    If a method doesn't access any of the fields of it's class, then there is nothing to stop you from making it static. static methods are usually (a tiny bit) faster. The difference is small though, so it becomes more a matter of "taste" (I wouldn't go back and change all you 100 MIDlets that you've developed until now to use static methods, unless you really beleive that the small increase in speed will make a difference, ie, the method is called a lot in a loop in a part where speed is critical).

    shmoove

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×