Search code examples
javaclasschaining

Java constant classes chaining (dot notation)


If you have only ONE class with constants and then uses that class to get the constants, it is considered a bad idea. I read that usually constant classes are created relative to the closest class that are using these constants.

Then I will need to check for what I called the class before calling a constant, but can I have a syntax that calls different classes constants like this:

Data.MainMenu.STR_PLAY
Data.Shop.INT_PRICE_APPLE;

These classes will only hold values of primitive data types.

Where Data is the main class refering to Shop and MainMenu in some way? This is the syntax I want to be able to do.


Solution

  • Where Data is the main class refering to Shop and MainMenu in some way

    That'd be an inner class

    public class Data {
        public static class MainMenu {
            public static String STR_PLAY;
        } 
        // Same for Shop 
    }