Accessing static field: caching handles locally
I have a class structure such as following for storing static final constants:
public final 开发者_StackOverflow中文版class A{
//..list of constants
public final class B{
//..list of constants
public final class C{
// ..list of constants
}
}
}
If I need to access static constants of class C(say about 10 to them) in some thread, which of the two approaches is better(faster)? Does it even make a difference? Is this some optimization that is VM specific? I am running this code on Android platform:
void doIt(){
A.B.C temp;
S.O.P(temp.FIELD1);
S.O.P(temp.FIELD2);
...
S.O.P(temp.FIELD10);
}
//OR
void doIt(){
S.O.P(A.B.C.FIELD1);
S.O.P(A.B.C.FIELD2);
...
S.O.P(A.B.C.FIELD10);
}
The first example won't compile, since you're not initializing the variable 'temp'. And since you said you're only accessing static members, instantiating it would be completely pointless.
A static final primitive will normally get inlined by the compiler, so as far as performance, it makes absolutely no difference where you get it from.
精彩评论