Shared constants can be defined as static variables but then the question to ask is which class should contain them? I've seen plenty of Contstants classes dedicated to this but these quickly degenerate into a jumbled mess. It's better to place them individually on the types which identify with them the most. For example, if you had a class called HttpRequest
, then your request method constants would fit well there as HttpRequest.GET
and HttpRequest.POST
. If you don't have any one class that identifies with the constants more than others, at least create a class to group like constants such as HttpRequestMethods.POST
and HttpRequestMethods.GET
.
If you have shared variables, be sure to encapsulate them with setProperty methods to make their modification thread safe since two or more collaborators could be modifying the variables at the same time. As for the class that contains them, instead of exposing the accessor methods statically, you may want to use the singleton pattern instead so that your shared variables are exposed on a shared object. That way you can use the benefit of OOP when the need arises. The singleton pattern can inhibit testability though so consider using an IOC container which will create one instance of this object an inject it into all collaborators that need access to these shared variables.