I need to have a global constants accessable from multiple classes within a (default) package. How can I declare them so all classes can see them ? I was told to create an interface but I am not quite sure how it will work.
Create a generic class such as GlobalConstants.class and just create static variables for the variables you want to have global access. If they don't change make them final. Becareful maybe to make them private and create getter and setter methods...i am showing the lazy way.
public class GlobalConstants{
public static String name = "Chilly Billy";
public static String address = "10 Chicken head Lane";
If you use a class, you can also include static methods that are used globally.
I tend to use an interface to hold static final variables, and a class to hold static global methods. As I get better, though, I use fewer and fewer global methods.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.