Use interfaces to define constants public interface Science

Use interfaces to define constants? public interface Science. Constants { static final double PI = 3. 14; static final double BOLTZMANN_CONSTANT = 1. 3806503 e-23; } 9/24/2021 Good Java Programming 1

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

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

Alternative 2 n Use Constant Utility class public class Phsyical. Constants { private Physical. Constants() { } //prevent instantiation static final double PI = 3. 14; static final double BOLTZMANN_CONSTANT = 1. 3806503 e-23; } Usage: double circ = 2 * Physical. Constants. PI * radius; 9/24/2021 Good Java Programming 4

Alternative 2: continued n Use Constant Utility class n If it is heavily used, then you don’t need to qualify it each time u use static import facility, introduced in java 1. 5 public class My. Test { import static Physical. Constants. *; double my. Method () { double circ = 2 * PI * radius; … } } 9/24/2021 Good Java Programming 5

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

More on Enums (2) n Enums provide compile-time type safety n Enums with identically named constants can coexist u as each type has its own namespace n Can augment Enums with methods u just like any Java class 9/24/2021 Good Java Programming 7

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

Enum Example public enum Course. Analysis { cs 240 (4, 81. 5, 55), cs 342 (4, 84. 3, 35), cs 654 (3, 79. 8, 41); // semi colon here private final int num. Credits; private final double avg. Score; private final int num. Students; private static final double NO_OF_EXAMS = 3; // Constructor Course. Analysis(int num. Credits. In, double avg. Score. In, int num. Students. In) { num. Credits = num. Credits. In; avg. Score = avg. Score. In; num. Students = num. Students. In; } 9/24/2021 Good Java Programming 9

Enum Example (continued) public int get. Num. Credits() { return num. Credits; } public double get. Avg. Score() { return avg. Score; } public int get. Num. Students() { return num. Students; } // dummy method, just an example // note: calculation is not intended to make sense … public double some. Weighted. Average(double weight) { return weight * num. Credits * avg. Score / num. Students; } } 9/24/2021 Good Java Programming 10

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

Enum: cannot directly access private data members // try to access the private data member directly System. out. printf("Enum member: average score is %f ", Course. Analysis. cs 342. avg. Score); Error: avg. Score has private access in test. util. Course. Analysis 9/24/2021 Good Java Programming 12

Enum: default to. String() System. out. printf("Enum member is %s %n", Course. Analysis. cs 342); Output: [java] Enum member is cs 342 9/24/2021 Good Java Programming 13

Enum: accessing methods // access methods like a standard Java object System. out. printf("Enum member: average score is %f %n", Course. Analysis. cs 342. get. Avg. Score()); Output: [java] Enum member: average score is 84. 300000 9/24/2021 Good Java Programming 14

Enum: foreach double weight. Value = 0. 77; for (Course. Analysis c : Course. Analysis. values()) { System. out. printf("Weighted. Avg of %s is %f %n", c, c. some. Weighted. Average(weight. Value)); } [java] Weighted. Avg of cs 240 is 4. 564000 [java] Weighted. Avg of cs 342 is 7. 418400 [java] Weighted. Avg of cs 654 is 4. 496049 9/24/2021 Good Java Programming 15

Design Guideline The constant interface pattern is a poor use of interfaces 9/24/2021 Good Java Programming 16
- Slides: 16