0

by an high-performance point of view, calling an outer class static method is slower than calling a static method in the same class?

2 Answers 2

2

No, never. Leaving method visibility apart, static method are just global function in the C sense. So it can't matter, what class they belong to, not even on Android.

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

Comments

0

It depends. Consider the following:

public class Outer {
    /*private*/ static void outerMethod() {
        // do stuff
    }

    static class Inner {
        public void doStuff() {
            outerMethod();
        }
    }
}

The doStuff() method's bytecode looks like this:

     0: invokestatic  #2                  // Method Outer.outerMethod:()V
     3: return        

But if we make outerMethod() private, the code will look like this instead:

     0: invokestatic  #2                  // Method Outer.access$000:()V
     3: return        

The problem is that the inner class can't directly call the outer class method, because it's private. The compiler works around this by creating a synthetic access$000() method, which does nothing but call outerMethod(). Calling through the accessor method is slower unless the AOT or JIT compiler recognizes the pattern and optimizes it out.

So, calling an outer-class static method could be slower if it's also declared private.

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.