0

I have stumbled upon some existing code which is using static import for constants.

import static com.zee.SelfServiceConstants.ATTR_SEV;
import static com.zee.SelfServiceConstants.ATTR_SEV_CRITICAL;

and it is used in the same class as:

propertyMap.put(ATTR_SEV, ATTR_SEV_CRITICAL);

Is this a good practice?
Or the traditional way of referring to constants better?

propertyMap.put(SelfServiceConstants.ATTR_SEV, SelfServiceConstants.ATTR_SEV_CRITICAL);
Zeeshan
  • 11,851
  • 21
  • 73
  • 98

2 Answers2

2

There is no difference. But there may be some readability.

From Java doc.

Used appropriately, static import can make your program more readable, by removing the boilerplate of repetition of class names.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
  • 1
    Agreed. But also from java doc: _If you overuse the static import feature, it can make your program unreadable and unmaintainable, polluting its namespace with all the static members you import._ My `class` namespace is polluted with so many of these static members import. :( – Zeeshan Oct 09 '14 at 11:08
0

I guess if you are using them much more than just once it's more convenient. I don't think that it either good or bad practice.

Lucas
  • 3,181
  • 4
  • 26
  • 45