2

I have a class say ClassOne. The ClassOne has two methods say method1() and method2() and both of these methods are static and will be called right one after other(like this):

ClassOne.method1();
ClassOne.method2();

This sequence is guaranteed not to change.

Now I wanted to know if there is any performance difference in the above case and the following case:

Second Case:

method1(){
    method2();
}
2
  • i dont think there will be any performance difference b/w them, although former approach is more practiced than later. Commented Jan 18, 2013 at 15:37
  • I agree. Using the second case leads to a 'side effect' (as I recall), which isn't recommended.... Commented Jan 18, 2013 at 16:06

5 Answers 5

4

In terms of performance, making the method calls in either way would produce the same bytecode and does not offer any performance benefits.

Have a look at this article for a better understanding http://www.codeproject.com/Articles/30422/How-the-Java-Virtual-Machine-JVM-Works

In terms of which style to use, it depends on what function each of the method performs. If method1() relies on the task performed by method2(), then you couple it iside, but if it doesn't, and method2() does something which needs to be performed after method1() finishes then you keep them separate to maintain the separation of concerns.

Sign up to request clarification or add additional context in comments.

Comments

2

I doubt there would be any difference mainly because ClassOne is a pointer to a message receiver and in the second case the receiver of the message is the this object in scope which is the class object itself. However if you like to prove this write a simple app that calls two empty methods for a couple of million times and compare the average time between the two approach.

Comments

2

You can look through generated bytecode and make conclusion by yourself:

source code #1

public class Clazz {

    public static void main(String[] args){
        Clazz.method1();
        Clazz.method2();
    }

    public static void method1() {

    }

    public static void method2() {

    }
}

bytecode #1

Compiled from "Clazz.java"

public class Clazz {
  public Clazz();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #16                 // Method method1:()V
       3: invokestatic  #19                 // Method method2:()V
       6: return

  public static void method1();
    Code:
       0: return

  public static void method2();
    Code:
       0: return
}

source code #2

public class Clazz {

    public static void main(String[] args){
        Clazz.method1();
    }

    public static void method1() {
        method2();
    }

    public static void method2() {

    }
}

bytecode #2

Compiled from "Clazz.java"

public class Clazz {
  public Clazz();
    Code:
       0: aload_0
       1: invokespecial #8                  // Method java/lang/Object."<init>":()V
       4: return

  public static void main(java.lang.String[]);
    Code:
       0: invokestatic  #16                 // Method method1:()V
       3: return

  public static void method1();
    Code:
       0: invokestatic  #21                 // Method method2:()V
       3: return

  public static void method2();
    Code:
       0: return
}


Result

Generated bytecode is the same for static methods

Comments

1

There shouldn't be any performance difference. Now if method2() will always be executed after method1(), it would be better to use Second Case.

1 Comment

Even more if methods are related to each other
1

In this case, your best option would simply be to refactor this by placing the body of "method2" at the end of "method1" and remove all the calls to "method2", and rename "method1" to something that reflects the fact that it's doing both operations now, along with renaming all the references to "method1".

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.