Submitted by: Octavia Anghel(octaviaanghel@gmail.com)

Wednesday May 16th 2018 by Octavia Anghel

Language: Java, Expertise: Beginner - Learn why, with very few exceptions, you should never place constants across component boundaries.

Using one class or interface that contains all kinds of constants used during the application may not be the best solution. These constants are unrelated with each other, the only thing that they have in common is the interface or the class and the reference to this class will contaminate many other unrelated components of the application.

What if you want to share some classes between a server and a remote client? Or later extract a component and use it in another application? This class has created a dependency between unrelated components and this limits reuse and loose coupling. You should never place constants across component boundaries, this is an exception only if the component is a library, and an explicit dependency is wanted.

public interface Constants {
   String version = "1.0";
   String dateFormat = "dd.MM.yyyy";
   String configurationFile = ".apprc"; 
   int maxNameLength = 32;
   String someQuery = "SELECT * FROM ...";
Mobile Site | Full Site