Java constant examples (Create a java file having only constants)

前端 未结 5 661
别那么骄傲
别那么骄傲 2021-01-31 03:14

What is the best practice to declare a java file having only constant?

public interface DeclareConstants
{
    String constant = \"Test\";
}

OR

相关标签:
5条回答
  • 2021-01-31 03:27

    Neither one. Use final class for Constants declare them as public static final and static import all constants wherever necessary.

    public final class Constants {
    
        private Constants() {
                // restrict instantiation
        }
    
        public static final double PI = 3.14159;
        public static final double PLANCK_CONSTANT = 6.62606896e-34;
    }
    

    Usage :

    import static Constants.PLANCK_CONSTANT;
    import static Constants.PI;//import static Constants.*;
    
    public class Calculations {
    
            public double getReducedPlanckConstant() {
                    return PLANCK_CONSTANT / (2 * PI);
            }
    }
    

    See wiki link : http://en.wikipedia.org/wiki/Constant_interface

    0 讨论(0)
  • 2021-01-31 03:34

    - Create a Class with public static final fields.

    - And then you can access these fields from any class using the Class_Name.Field_Name.

    - You can declare the class as final, so that the class can't be extended(Inherited) and modify....

    0 讨论(0)
  • 2021-01-31 03:39

    You can also use the Properties class

    Here's the constants file called

    # this will hold all of the constants
    frameWidth = 1600
    frameHeight = 900
    

    Here is the code that uses the constants

    public class SimpleGuiAnimation {
    
        int frameWidth;
        int frameHeight;
    
    
        public SimpleGuiAnimation() {
            Properties properties = new Properties();
    
            try {
                File file = new File("src/main/resources/dataDirectory/gui_constants.properties");
                FileInputStream fileInputStream = new FileInputStream(file);
                properties.load(fileInputStream);
            }
            catch (FileNotFoundException fileNotFoundException) {
                System.out.println("Could not find the properties file" + fileNotFoundException);
            }
            catch (Exception exception) {
                System.out.println("Could not load properties file" + exception.toString());
            }
    
    
            this.frameWidth = Integer.parseInt(properties.getProperty("frameWidth"));
            this.frameHeight = Integer.parseInt(properties.getProperty("frameHeight"));
        }
    
    0 讨论(0)
  • 2021-01-31 03:40

    Both are valid but I normally choose interfaces. A class (abstract or not) is not needed if there is no implementations.

    As an advise, try to choose the location of your constants wisely, they are part of your external contract. Do not put every single constant in one file.

    For example, if a group of constants is only used in one class or one method put them in that class, the extended class or the implemented interfaces. If you do not take care you could end up with a big dependency mess.

    Sometimes an enumeration is a good alternative to constants (Java 5), take look at: http://docs.oracle.com/javase/1.5.0/docs/guide/language/enums.html

    0 讨论(0)
  • 2021-01-31 03:41

    This question is old. But I would like to mention an other approach. Using Enums for declaring constant values. Based on the answer of Nandkumar Tekale, the Enum can be used as below:

    Enum:

    public enum Planck {
        REDUCED();
        public static final double PLANCK_CONSTANT = 6.62606896e-34;
        public static final double PI = 3.14159;
        public final double REDUCED_PLANCK_CONSTANT;
    
        Planck() {
            this.REDUCED_PLANCK_CONSTANT = PLANCK_CONSTANT / (2 * PI);
        }
    
        public double getValue() {
            return REDUCED_PLANCK_CONSTANT;
        }
    }
    

    Client class:

    public class PlanckClient {
    
        public static void main(String[] args) {
            System.out.println(getReducedPlanckConstant());
            // or using Enum itself as below:
            System.out.println(Planck.REDUCED.getValue());
        }
    
        public static double getReducedPlanckConstant() {
            return Planck.PLANCK_CONSTANT / (2 * Planck.PI);
        }
    
    }
    

    Reference : The usage of Enums for declaring constant fields is suggested by Joshua Bloch in his Effective Java book.

    0 讨论(0)
提交回复
热议问题