Is this a good idea, to group constants in classes inside Constants
class container like
public final class Constants {
public final class File {
public static final int MIN_ROWS = 1;
public static final int MAX_ROWS = 1000;
private File() {}
}
public final class DB {
public static final String name = "oups";
public final class Connection() {
public static final String URL = "jdbc:tra-ta-ta";
public static final String USER = "testUser";
public static final String PASSWORD = "testPassword";
private Connection() {}
}
private DB() {}
}
private Constants() {}
}
It allows to use Constants.DB.Connection.URL
instead of DbConnectionConstants.URL
.
I generally prefer to put constants in the class where they belong. For example, the file constants could be in FileManager
(or something like that), where they are used. The connection constants could be in your DBUtil
class, where they are used.
Think about the JDK. Does it have a gigantic Constants class? No. The constants used by (and with) BorderLayout
are in the class BorderLayout
. The constants used by (and with) JOptionPane
are in JOptionPane
.
Advantages:
Disadvantages:
In conclusion, it's something that might be handy when you write just some little application for your own self. Generally, it is not a good practice.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With