Search code examples
javainterfacefinal

Interface Vs Final class to store the final variables


I have a class which contains only final variables. E.g:

public class Constants
{
    public static final String PREFIX = "some prefix";
    public static final String SUFFIX = "some suffix";
    //and so on...
}

Which type is recommended here - an interface or a final class?


Solution

  • Interfaces are used to define a contract. In the code you copy paste, these constants should be defined in a final class. Check What is the best way to implement constants in Java?