1 / 16

Use interfaces to define constants?

Use interfaces to define constants? . public interface ScienceConstants { static final double PI = 3.14; static final double BOLTZMANN_CONSTANT = 1.3806503e-23; }. Alternative 1. Use Enums Enums in Java are full fledged classes (Java 1.5) More powerful than enums in C++

Download Presentation

Use interfaces to define constants?

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Use interfaces to define constants? public interface ScienceConstants { static final double PI = 3.14; static final double BOLTZMANN_CONSTANT = 1.3806503e-23; } Good Java Programming

  2. Alternative 1 • Use Enums • Enums in Java are full fledged classes (Java 1.5) • More powerful than enums in C++ • where they are just int values • By nature, Enums are immutable • All fields should be final • Make the fields private and provide accessors/mutators Good Java Programming

  3. Do NOT use interfaces to define constants • The use of constants in a class should be considered implementation detail • If a non-final class implements an interface, all its subclasses will have their namespaces polluted by the constants in the interface • Java uses constant interfaces in its libraries (example: java.io.ObjectStreamConstants) • treat these as anomalies Good Java Programming

  4. Alternative 2 • Use Constant Utility class public class PhsyicalConstants { private PhysicalConstants() { } //prevent instantiation static final double PI = 3.14; static final double BOLTZMANN_CONSTANT = 1.3806503e-23; } Usage: double circ = 2 * PhysicalConstants.PI * radius; Good Java Programming

  5. Alternative 2: continued • Use Constant Utility class • If it is heavily used, then you don’t need to qualify it each time • use static import facility, introduced in java 1.5 public class MyTest { import static PhysicalConstants.*; double myMethod () { double circ = 2 * PI* radius; … } } Good Java Programming

  6. More on Enums • Java Enums are classes that export one instance for each enumeration constant via a public static final field • Enum types are effectively final • no accessible constructor • clients can neither create instances or extend it • so, enum types are instance controlled • Enums are generalization of Singletons • Singleton is a single element enum Good Java Programming

  7. More on Enums (2) • Enums provide compile-time type safety • Enums with identically named constants can co-exist • as each type has its own namespace • Can augment Enums with methods • just like any Java class Good Java Programming

  8. More on Enums (3) • Has a static values(…) method that returns an array of its values in the order it was declared • example in next slides • Default toString() returns the declared name of each enum value • can be overridden Good Java Programming

  9. Enum Example public enum CourseAnalysis { cs240 (4, 81.5, 55), cs342 (4, 84.3, 35), cs654 (3, 79.8, 41); // semi colon here private final int numCredits; private final double avgScore; private final int numStudents; private static final double NO_OF_EXAMS = 3; // Constructor CourseAnalysis(int numCreditsIn, double avgScoreIn, int numStudentsIn) { numCredits = numCreditsIn; avgScore = avgScoreIn; numStudents = numStudentsIn; } Good Java Programming

  10. Enum Example (continued) public intgetNumCredits() { return numCredits; } public double getAvgScore() { return avgScore; } public intgetNumStudents() { return numStudents; } // dummy method, just an example // note: calculation is not intended to make sense … public double someWeightedAverage(double weight) { return weight * numCredits * avgScore / numStudents; } } Good Java Programming

  11. Enum: can’t instantiate like a Java object // Try calling the constructor CourseAnalysiscA = new CourseAnalysis(3, 3.14, 7); Driver.java:91: enum types may not be instantiated [javac] CourseAnalysiscA = new CourseAnalysis(3, 3.14, 7); [javac] ^ [javac] 1 error BUILD FAILED Good Java Programming

  12. Enum: cannot directly access private data members // try to access the private data member directly System.out.printf("Enum member: average score is %f ", CourseAnalysis.cs342.avgScore); Error: avgScore has private access in test.util.CourseAnalysis Good Java Programming

  13. Enum: default toString() System.out.printf("Enum member is %s %n", CourseAnalysis.cs342); Output: [java] Enum member is cs342 Good Java Programming

  14. Enum: accessing methods // access methods like a standard Java object System.out.printf("Enum member: average score is %f %n", CourseAnalysis.cs342.getAvgScore()); Output: [java] Enum member: average score is 84.300000 Good Java Programming

  15. Enum: foreach double weightValue = 0.77; for (CourseAnalysis c : CourseAnalysis.values()) { System.out.printf("WeightedAvg of %s is %f %n", c, c.someWeightedAverage(weightValue)); } [java] WeightedAvg of cs240 is 4.564000 [java] WeightedAvg of cs342 is 7.418400 [java] WeightedAvg of cs654 is 4.496049 Good Java Programming

  16. Design Guideline The constant interface pattern is a poor use of interfaces Good Java Programming

More Related