Tanya Bear Tanya Bear - 1 month ago 20
Java Question

Constants in Interface

I know there were a lot of questions about putting constants into interfaces, and that is a bad practice.

But I have a question, what if I have a class, where I want to define some sets of constants, but I want to have them in groups, is it a bad practice to have interface/class inside a class and define constants in there, so my constants are grouped?

e.g.

public class MyClass{

public final class A {
public static final String A = "a" ;
}
public final class B {
public static final String B = "b";
}
....
}

Answer

Yes you can have nested class to group the constants. It's really not a bad practice.