In the past, I often used the "import static" construct in Java classes. I recently realized that instead
import static my.pack.A.MY_CONSTANT;
you can use
import my.pack.A; public class B { private static final MY_CONSTANT = A.MY_CONSTANT; }
The most obvious benefits of this approach are:
- You can use refactoring in Eclipse to easily separate all long constant expressions, such as
ABCCONSTANT.ANOTHER_ONE.TOO_LONG from your code, without loading static imports (which were not so quickly mastered in Eclipse). - You can give any name to any expression that may be more meaningful in the current context.
For instance:
private static final PAYMENT_TYPE = PaymentType.CASH; ... calculateSomething(amount, PAYMENT_TYPE);
instead
import static my.pack.PaymentType.CASH ... calculateSomething(amount, CASH);
and it's also easier to refactor if the PaymentType defaults to CREDIT_CARD.
My question is: are there any drawbacks to this approach compared to static imports, or can it be used everywhere?
My only problem now is the resulting compiled .class file, which is probably different for the two approaches described. Thus, performance and memory usage could theoretically be affected.
source share