views:

26

answers:

1

I have a class structure such as following for storing static final constants:

public final 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);
}
+2  A: 

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.

Mike Baranczak
thank you very much!
Samuh