In any nontrivial software project, bugs are simply a fact of life. Careful planning, programming, and testing can help reduce their pervasiveness, but somehow, somewhere, they’ll always find a way to creep into your code. This becomes especially apparent as new features are introduced and your code base grows in size and complexity.
Fortunately, some bugs are easier to detect than others. Compile-time bugs, for example, tell you immediately that something is wrong; you can use the compiler’s error messages to figure out what the problem is and fix it, right then and there. Runtime bugs, however, can be much more problematic; they don’t always surface immediately, and when they do, it may be at a point in time that’s far removed from the actual cause of the problem.
Generics add stability to your code by making more of your bugs detectable at compile time. Some programmers choose to learn generics by studying the Java Collections Framework; after all, generics are heavily used by those classes. However, since we haven’t yet covered collections, this chapter will focus primarily on simple “collections-like” examples that we’ll design from scratch. This hands-on approach will teach you the necessary syntax and terminology while demonstrating the various kinds of problems that generics were designed to solve.
A Simple Box Class
Let’s begin by designing a nongenericBoxclass that operates on objects of any type. It need only provide two methods:add, which adds an object to the box, andget, which retrieves it:public class Box {Since its methods accept or returnprivate Object object;
public void add(Object object) {
this.object = object;
}public Object get() {
return object;
}
}
Object, you’re free to pass in whatever you want, provided that it’s not one of the primitive types. However, should you need to restrict the contained type to something specific (likeInteger), your only option would be to specify the requirement in documentation (or in this case, a comment), which of course the compiler knows nothing about:
Thepublic class BoxDemo1 {public static void main(String[] args) {
// ONLY place Integer objects into this box!
Box integerBox = new Box();integerBox.add(new Integer(10));
Integer someInteger = (Integer)integerBox.get();
System.out.println(someInteger);
}
}
BoxDemo1program creates anIntegerobject, passes it toadd, then assigns that same object tosomeIntegerby the return value ofget. It then prints the object’s value (10) to standard output. We know that the cast fromObjecttoIntegeris correct because we’ve honored the “contract” specified in the comment. But remember, the compiler knows nothing about this — it just trusts that our cast is correct. Furthermore, it will do nothing to prevent a careless programmer from passing in an object of the wrong type, such asString:
Inpublic class BoxDemo2 {public static void main(String[] args) {
// ONLY place Integer objects into this box!
Box integerBox = new Box();// Imagine this is one part of a large application
// modified by one programmer.
integerBox.add(“10”); // note how the type is now String// … and this is another, perhaps written
// by a different programmer
Integer someInteger = (Integer)integerBox.get();
System.out.println(someInteger);
}
}
BoxDemo2we’ve stored the number 10 as aString, which could be the case when, say, a GUI collects input from the user. However, the existing cast fromObjecttoIntegerhas mistakenly been overlooked. This is clearly a bug, but because the code still compiles, you wouldn’t know anything is wrong until runtime, when the application crashes with aClassCastException:Exception in thread “main”
java.lang.ClassCastException:
java.lang.String cannot be cast to java.lang.Integer
at BoxDemo2.main(BoxDemo2.java:6)
If the
Boxclass had been designed with generics in mind, this mistake would have been caught by the compiler, instead of crashing the application at runtime.
Month: October 2007
Numbers and Strings
Numbers
This section begins with a discussion of theNumberclass (in thejava.langpackage) and its subclasses. In particular, this section talks about the situations where you would use instantiations of these classes rather than the primitive data types. Additionally, this section talks about other classes you might need to work with numbers, such as formatting or using mathematical functions to complement the operators built into the language.
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects. This section describes using theStringclass to create and manipulate strings. It also compares theStringandStringBuilderclasses.
Strings
Strings, which are widely used in Java programming, are a sequence of characters. In the Java programming language, strings are objects.
The Java platform provides the
Stringclass to create and manipulate strings.
Creating Strings
The most direct way to create a string is to write:
In this case, “Hello world!” is a string literal—a series of characters in your code that is enclosed in double quotes. Whenever it encounters a string literal in your code, the compiler creates aString greeting = “Hello world!”;
Stringobject with its value—in this case,Hello world!.
As with any other object, you can create
Stringobjects by using thenewkeyword and a constructor. TheStringclass has 11 constructors that allow you to provide the initial value of the string using different sources, such as an array of characters:
The last line of this code snippet displayschar[] helloArray = { ‘h’, ‘e’, ‘l’, ‘l’, ‘o’, ‘.’};
String helloString = new String(helloArray);
System.out.println(helloString);
hello.
Note: TheStringclass is immutable, so that once it is created aStringobject cannot be changed. TheStringclass has a number of methods, some of which will be discussed below, that appear to modify strings. Since strings are immutable, what these methods really do is create and return a new string that contains the result of the operation.
String Length
Methods used to obtain information about an object are known as accessor methods. One accessor method that you can use with strings is thelength()method, which returns the number of characters contained in the string object. After the following two lines of code have been executed,lenequals 17:
A palindrome is a word or sentence that is symmetric—it is spelled the same forward and backward, ignoring case and punctuation. Here is a short and inefficient program to reverse a palindrome string. It invokes theString palindrome = “Dot saw I was Tod”;
int len = palindrome.length();
StringmethodcharAt(i), which returns the ith character in the string, counting from 0.
Running the program produces this output:public class StringDemo {
public static void main(String[] args) {
String palindrome = “Dot saw I was Tod”;
int len = palindrome.length();
char[] tempCharArray = new char[len];
char[] charArray = new char[len];// put original string in an array of chars
for (int i = 0; i < len; i++) {
tempCharArray[i] = palindrome.charAt(i);
}// reverse array of chars
for (int j = 0; j < len; j++) {
charArray[j] = tempCharArray[len – 1 – j];
}String reversePalindrome = new String(charArray);
System.out.println(reversePalindrome);
}
}
To accomplish the string reversal, the program had to convert the string to an array of characters (firstdoT saw I was toD
forloop), reverse the array into a second array (secondforloop), and then convert back to a string. TheStringclass includes a method,getChars(), to convert a string, or a portion of a string, into an array of characters so we could replace the firstforloop in the program above with
palindrome.getChars(0, len – 1, tempCharArray, 0);
Concatenating Strings
TheStringclass includes a method for concatenating two strings:
This returns a new string that is string1 with string2 added to it at the end.string1.concat(string2);
You can also use the
concat()method with string literals, as in:
Strings are more commonly concatenated with the“My name is “.concat(“Rumplestiltskin”);
+operator, as in
which results in“Hello,” + ” world” + “!”
The“Hello, world!”
+operator is widely used in
which printsString string1 = “saw I was “;
System.out.println(“Dot ” + string1 + “Tod”);
Such a concatenation can be a mixture of any objects. For each object that is not aDot saw I was Tod
String, itstoString()method is called to convert it to aString.
Note: The Java programming language does not permit literal strings to span lines in source files, so you must use the+concatenation operator at the end of each line in a multi-line string. For example,
Breaking strings between lines using theString quote = “Now is the time for all good ” +
“men to come to the aid of their country.”;
+concatenation operator is, once again, very common in
Creating Format Strings
You have seen the use of theprintf()andformat()methods to print output with formatted numbers. TheStringclass has an equivalent class method,format(), that returns aStringobject rather than aPrintStreamobject.
Using
String’sstaticformat()method allows you to create a formatted string that you can reuse, as opposed to a one-time print statement. For example, instead of
you can writeSystem.out.printf(“The value of the float variable is %f, while the value of the ” +
“integer variable is %d, and the string is %s”, floatVar, intVar, stringVar);
String fs;
fs = String.format(“The value of the float variable is %f, while the value of the ” +
“integer variable is %d, and the string is %s”, floatVar, intVar, stringVar);
System.out.println(fs);
Numbers
This section begins with a discussion of theNumberclass in thejava.langpackage, its subclasses, and the situations where you would use instantiations of these classes rather than the primitive number types.
This section also presents the
PrintStreamandDecimalFormatclasses, which provide methods for writing formatted numerical output.
Finally, the
Mathclass injava.langis discussed. It contains mathematical functions to complement the operators built into the language. This class has methods for the trigonometric functions, exponential functions, and so forth.
Interfaces and Inheritance
Interfaces
You saw an example of implementing an interface in the previous lesson. You can read more about interfaces here—what they are for, why you might want to write one, and how to write one.
Inheritance
This section describes the way in which you can derive one class from another. That is, how a subclass can inherit fields and methods from a superclass. You will learn that all classes are derived from theObjectclass, and how to modify the methods that a subclass inherits from superclasses. This section also covers interface-like abstract classes.
Inheritance
In the preceding lessons, you have seen inheritance mentioned several times. In the Java language, classes can be derived from other classes, thereby inheriting fields and methods from those classes.
The idea of inheritance is simple but powerful: When you want to create a new class and there is already a class that includes some of the code that you want, you can derive your new class from the existing class. In doing this, you can reuse the fields and methods of the existing class without having to write (and debug!) them yourself.
Definitions:
A class that is derived from another class is called a subclass (also a derived class, extended class, or child class). The class from which the subclass is derived is called a superclass (also a base class or a parent class).
Excepting
Object, which has no superclass, every class has one and only one direct superclass (single inheritance). In the absence of any other explicit superclass, every class is implicitly a subclass ofObject.
Classes can be derived from classes that are derived from classes that are derived from classes, and so on, and ultimately derived from the topmost class,
Object. Such a class is said to be descended from all the classes in the inheritance chain stretching back toObject.
A subclass inherits all the members (fields, methods, and nested classes) from its superclass. Constructors are not members, so they are not inherited by subclasses, but the constructor of the superclass can be invoked from the subclass.
The Java Platform Class Hierarchy
TheObjectclass, defined in thejava.langpackage, defines and implements behavior common to all classes—including the ones that you write. In the Java platform, many classes derive directly fromObject, other classes derive from some of those classes, and so on, forming a hierarchy of classes.
At the top of the hierarchy,
All Classes in the Java Platform are Descendants of Object
Objectis the most general of all classes. Classes near the bottom of the hierarchy provide more specialized behavior.
An Example of Inheritance
Here is the sample code for a possible implementation of aBicycleclass that was presented in the Classes and Objects lesson:
public class Bicycle {// the Bicycle class has three fields
public int cadence;
public int gear;
public int speed;// the Bicycle class has one constructor
public Bicycle(int startCadence, int startSpeed, int startGear) {
gear = startGear;
cadence = startCadence;
speed = startSpeed;
}// the Bicycle class has four methods
public void setCadence(int newValue) {
cadence = newValue;
}public void setGear(int newValue) {
gear = newValue;
}public void applyBrake(int decrement) {
speed -= decrement;
}public void speedUp(int increment) {
speed += increment;
}}
A class declaration for a
MountainBikeclass that is a subclass ofBicyclemight look like this:
public class MountainBike extends Bicycle {// the MountainBike subclass adds one field
public int seatHeight;// the MountainBike subclass has one constructor
public MountainBike(int startHeight, int startCadence, int startSpeed, int startGear) {
super(startCadence, startSpeed, startGear);
seatHeight = startHeight;
}// the MountainBike subclass adds one method
public void setHeight(int newValue) {
seatHeight = newValue;
}}
MountainBikeinherits all the fields and methods ofBicycleand adds the fieldseatHeightand a method to set it. Except for the constructor, it is as if you had written a newMountainBikeclass entirely from scratch, with four fields and five methods. However, you didn’t have to do all the work. This would be especially valuable if the methods in theBicycleclass were complex and had taken substantial time to debug.
What You Can Do in a Subclass
A subclass inherits all of the public and protected members of its parent, no matter what package the subclass is in. If the subclass is in the same package as its parent, it also inherits the package-private members of the parent. You can use the inherited members as is, replace them, hide them, or supplement them with new members:
The following sections in this lesson will expand on these topics.
- The inherited fields can be used directly, just like any other fields.
- You can declare a field in the subclass with the same name as the one in the superclass, thus hiding it (not recommended).
- You can declare new fields in the subclass that are not in the superclass.
- The inherited methods can be used directly as they are.
- You can write a new instance method in the subclass that has the same signature as the one in the superclass, thus overriding it.
- You can write a new static method in the subclass that has the same signature as the one in the superclass, thus hiding it.
- You can declare new methods in the subclass that are not in the superclass.
- You can write a subclass constructor that invokes the constructor of the superclass, either implicitly or by using the keyword
super.
Private Members in a Superclass
A subclass does not inherit theprivatemembers of its parent class. However, if the superclass has public or protected methods for accessing its private fields, these can also be used by the subclass.
A nested class has access to all the private members of its enclosing class—both fields and methods. Therefore, a public or protected nested class inherited by a subclass has indirect access to all of the private members of the superclass.
Casting Objects
We have seen that an object is of the data type of the class from which it was instantiated. For example, if we write
thenpublic MountainBike myBike = new MountainBike();
myBikeis of typeMountainBike.
MountainBikeis descended fromBicycleandObject. Therefore, aMountainBikeis aBicycleand is also anObject, and it can be used whereverBicycleorObjectobjects are called for.
The reverse is not necessarily true: a
Bicyclemay be aMountainBike, but it isn’t necessarily. Similarly, anObjectmay be aBicycleor aMountainBike, but it isn’t necessarily.
Casting shows the use of an object of one type in place of another type, among the objects permitted by inheritance and implementations. For example, if we write
thenObject obj = new MountainBike();
objis both anObjectand aMountainbike(until such time asobjis assigned another object that is not aMountainbike). This is called implicit casting.
If, on the other hand, we write
we would get a compile-time error becauseMountainBike myBike = obj;
objis not known to the compiler to be aMountainBike. However, we can tell the compiler that we promise to assign aMountainBiketoobjby explicit casting:
This cast inserts a runtime check thatMountainBike myBike = (MountainBike)obj;
objis assigned aMountainBikeso that the compiler can safely assume thatobjis aMountainBike. Ifobjis not aMountainbikeat runtime, an exception will be thrown.
Note: You can make a logical test as to the type of a particular object using theinstanceofoperator. This can save you from a runtime error owing to an improper cast. For example:
Here theif (obj instanceof MountainBike) {
MountainBike myBike = (MountainBike)obj;
}
instanceofoperator verifies thatobjrefers to aMountainBikeso that we can make the cast with knowledge that there will be no runtime exception thrown.
Interfaces
There are a number of situations in software engineering when it is important for disparate groups of programmers to agree to a “contract” that spells out how their software interacts. Each group should be able to write their code without any knowledge of how the other group’s code is written. Generally speaking, interfaces are such contracts.
For example, imagine a futuristic society where computer-controlled robotic cars transport passengers through city streets without a human operator. Automobile manufacturers write software (Java, of course) that operates the automobile—stop, start, accelerate, turn left, and so forth. Another industrial group, electronic guidance instrument manufacturers, make computer systems that receive GPS (Global Positioning Satellite) position data and wireless transmission of traffic conditions and use that information to drive the car.
The auto manufacturers must publish an industry-standard interface that spells out in detail what methods can be invoked to make the car move (any car, from any manufacturer). The guidance manufacturers can then write software that invokes the methods described in the interface to command the car. Neither industrial group needs to know how the other group’s software is implemented. In fact, each group considers its software highly proprietary and reserves the right to modify it at any time, as long as it continues to adhere to the published interface.
Interfaces in Java
In the Java programming language, an interface is a reference type, similar to a class, that can contain only constants, method signatures, and nested types. There are no method bodies. Interfaces cannot be instantiated—they can only be implemented by classes or extended by other interfaces. Extension is discussed later in this lesson.
Defining an interface is similar to creating a new class:
Note that the method signatures have no braces and are terminated with a semicolon.public interface OperateCar {// constant declarations, if any
// method signatures
int turn(Direction direction, // An enum with values RIGHT, LEFT
double radius, double startSpeed, double endSpeed);
int changeLanes(Direction direction, double startSpeed, double endSpeed);
int signalTurn(Direction direction, boolean signalOn);
int getRadarFront(double distanceToCar, double speedOfCar);
int getRadarRear(double distanceToCar, double speedOfCar);
……
// more method signatures
}
To use an interface, you write a class that implements the interface. When an instantiable class implements an interface, it provides a method body for each of the methods declared in the interface. For example,
In the robotic car example above, it is the automobile manufacturers who will implement the interface. Chevrolet’s implementation will be substantially different from that of Toyota, of course, but both manufacturers will adhere to the same interface. The guidance manufacturers, who are the clients of the interface, will build systems that use GPS data on a car’s location, digital street maps, and traffic data to drive the car. In so doing, the guidance systems will invoke the interface methods: turn, change lanes, brake, accelerate, and so forth.public class OperateBMW760i implements OperateCar {// the OperateCar method signatures, with implementation —
// for example:
int signalTurn(Direction direction, boolean signalOn) {
//code to turn BMW’s LEFT turn indicator lights on
//code to turn BMW’s LEFT turn indicator lights off
//code to turn BMW’s RIGHT turn indicator lights on
//code to turn BMW’s RIGHT turn indicator lights off
}// other members, as needed — for example, helper classes
// not visible to clients of the interface}
Interfaces as APIs
The robotic car example shows an interface being used as an industry standard Application Programming Interface (API). APIs are also common in commercial software products. Typically, a company sells a software package that contains complex methods that another company wants to use in its own software product. An example would be a package of digital image processing methods that are sold to companies making end-user graphics programs. The image processing company writes its classes to implement an interface, which it makes public to its customers. The graphics company then invokes the image processing methods using the signatures and return types defined in the interface. While the image processing company’s API is made public (to its customers), its implementation of the API is kept as a closely guarded secret—in fact, it may revise the implementation at a later date as long as it continues to implement the original interface that its customers have relied on.
Interfaces and Multiple Inheritance
Interfaces have another very important role in the Java programming language. Interfaces are not part of the class hierarchy, although they work in combination with classes. The Java programming language does not permit multiple inheritance (inheritance is discussed later in this lesson), but interfaces provide an alternative.
In Java, a class can inherit from only one class but it can implement more than one interface. Therefore, objects can have multiple types: the type of their own class and the types of all the interfaces that they implement. This means that if a variable is declared to be the type of an interface, its value can reference any object that is instantiated from any class that implements the interface. This is discussed later in this lesson, in the section titled “Using an Interface as a Type.”
Classes and Objects
With the knowledge you now have of the basics of the Java programming language, you can learn to write your own classes. In this lesson, you will find information about defining your own classes, including declaring member variables, methods, and constructors.
You will learn to use your classes to create objects, and how to use the objects you create.
This lesson also covers nesting classes within other classes, enumerations, and annotations.
Classes
This section shows you the anatomy of a class, and how to declare fields, methods, and constructors.
Objects
This section covers creating and using objects. You will learn how to instantiate an object, and, once instantiated, how to use thedotoperator to access the object’s instance variables and methods.
More on Classes
This section covers more aspects of classes that depend on using object references and thedotoperator that you learned about in the preceding section: returning values from methods, thethiskeyword, class vs. instance members, and access control.
Nested Classes
Static nested classes, inner classes, anonymous inner classes, and local classes are covered.
Enum Types
This section covers enumerations, specialized classes that allow you to define and use sets of constants.
Annotations
Annotations allow you to add information to your program that is not actually part of the program. This section describes three built-in annotations that you should know about.
Annotations
Annotations provide data about a program that is not part of the program itself. They have no direct effect on the operation of the code they annotate.
Annotations have a number of uses, among them:
Annotations can be applied to a program’s declarations of classes, fields, methods, and other program elements.
- Information for the compiler — Annotations can be used by the compiler to detect errors or suppress warnings.
- Compiler-time and deployment-time processing — Software tools can process annotation information to generate code, XML files, and so forth.
- Runtime processing — Some annotations are available to be examined at runtime.
The annotation appears first, often (by convention) on its own line, and may include elements with named or unnamed values:
or@Author(
name = “Benjamin Franklin”,
date = “3/27/2003”
)
class MyClass() { }
If there is just one element named “value,” then the name may be omitted, as in:@SuppressWarnings(value = “unchecked”)
void myMethod() { }
Also, if an annotation has no elements, the parentheses may be omitted, as in:@SuppressWarnings(“unchecked”)
void myMethod() { }
@Override
void mySuperMethod() { }
Documentation
Many annotations replace what would otherwise have been comments in code.
Suppose that a software group has traditionally begun the body of every class with comments providing important information:
To add this same metadata with an annotation, you must first define the annotation type. The syntax for doing this is:public class Generation3List extends Generation2List {// Author: John Doe
// Date: 3/17/2002
// Current revision: 6
// Last modified: 4/12/2004
// By: Jane Doe
// Reviewers: Alice, Bill, Cindy// class code goes here
}
The annotation type definition looks somewhat like an interface definition where the keyword@interface ClassPreamble {
String author();
String date();
int currentRevision() default 1;
String lastModified() default “N/A”;
String lastModifiedBy() default “N/A”;
String[] reviewers(); // Note use of array
}
interfaceis preceded by the @ character (@ = “AT” as in Annotation Type). Annotation types are, in fact, a form of interface, which will be covered in a later lesson. For the moment, you do not need to understand interfaces.
The body of the annotation definition above contains annotation type element declarations, which look a lot like methods. Note that they may define optional default values.
Once the annotation type has been defined, you can use annotations of that type, with the values filled in, like this:
@ClassPreamble (
author = “John Doe”,
date = “3/17/2002”,
currentRevision = 6,
lastModified = “4/12/2004”,
lastModifiedBy = “Jane Doe”
reviewers = {“Alice”, “Bob”, “Cindy”} // Note array notation
)
public class Generation3List extends Generation2List {// class code goes here
}
Note: To make the information in@ClassPreambleappear in Javadoc-generated documentation, you must annotate the@ClassPreambledefinition itself with the@Documentedannotation:
import java.lang.annotation.*; // import this to use@Documented@Documented
@interface ClassPreamble {// Annotation element definitions
}
Annotations Used by the Compiler
There are three annotation types that are predefined by the language specification itself:@Deprecated,@Override, and@SuppressWarnings.
@Deprecated—the
@Deprecatedannotation indicates that the marked element is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field with the@Deprecatedannotation. When an element is deprecated, it should also be documented using the Javadoc@deprecatedtag, as shown in the following example. The use of the “@” symbol in both Javadoc comments and in annotations is not coincidental—they are related conceptually. Also, note that the Javadoc tag starts with a lowercase “d” and the annotation starts with an uppercase “D”.
// Javadoc comment follows
/**
* @deprecated
* explanation of why it was deprecated
*/
@Deprecated
static void deprecatedMethod() { }
}
@Override—the
@Overrideannotation informs the compiler that the element is meant to override an element declared in a superclass (overriding methods will be discussed in the the lesson titled “Interfaces and Inheritance”).
While it’s not required to use this annotation when overriding a method, it helps to prevent errors. If a method marked with// mark method as a superclass method
// that has been overridden
@Override
int overriddenMethod() { }
@Overridefails to correctly override a method in one of its superclasses, the compiler generates an error.
@SuppressWarnings—the
@SuppressWarningsannotation tells the compiler to suppress specific warnings that it would otherwise generate. In the example below, a deprecated method is used and the compiler would normally generate a warning. In this case, however, the annotation causes the warning to be suppressed.
// use a deprecated method and tell
// compiler not to generate a warning
@SuppressWarnings(“deprecation”)
void useDeprecatedMethod() {
objectOne.deprecatedMethod(); //deprecation warning – suppressed
}
Every compiler warning belongs to a category. The Java Language Specification lists two categories: “deprecation” and “unchecked.” The “unchecked” warning can occur when interfacing with legacy code written before the advent of generics (discussed in the lesson titled “Generics”). To suppress more than one category of warnings, use the following syntax:
@SuppressWarnings({“unchecked”, “deprecation”})
Annotation Processing
The more advanced uses of annotations include writing an annotation processor that can read a Java program and take actions based on its annotations. It might, for example, generate auxiliary source code, relieving the programmer of having to create boilerplate code that always follows predictable patterns. To facilitate this task, release 5.0 of the JDK includes an annotation processing tool, calledapt. In release 6 of the JDK, the functionality ofaptis a standard part of the Java compiler.
To make annotation information available at runtime, the annotation type itself must be annotated with
@Retention(RetentionPolicy.RUNTIME), as follows:
import java.lang.annotation.*;@Retention(RetentionPolicy.RUNTIME)
@interface AnnotationForRuntime {// Elements that give information
// for runtime processing}
Enum Types
An enum type is a type whose fields consist of a fixed set of constants. Common examples include compass directions (values of NORTH, SOUTH, EAST, and WEST) and the days of the week.
Because they are constants, the names of an enum type’s fields are in uppercase letters.
In the Java programming language, you define an enum type by using the
enumkeyword. For example, you would specify a days-of-the-week enum type as:
You should use enum types any time you need to represent a fixed set of constants. That includes natural enum types such as the planets in our solar system and data sets where you know all possible values at compile time—for example, the choices on a menu, command line flags, and so on.public enum Day {
SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY
}
Here is some code that shows you how to use the
Dayenum defined above:
The output is:public class EnumTest {
Day day;public EnumTest(Day day) {
this.day = day;
}public void tellItLikeItIs() {
switch (day) {
case MONDAY: System.out.println(“Mondays are bad.”);
break;case FRIDAY: System.out.println(“Fridays are better.”);
break;case SATURDAY:
case SUNDAY: System.out.println(“Weekends are best.”);
break;default: System.out.println(“Midweek days are so-so.”);
break;
}
}public static void main(String[] args) {
EnumTest firstDay = new EnumTest(Day.MONDAY);
firstDay.tellItLikeItIs();
EnumTest thirdDay = new EnumTest(Day.WEDNESDAY);
thirdDay.tellItLikeItIs();
EnumTest fifthDay = new EnumTest(Day.FRIDAY);
fifthDay.tellItLikeItIs();
EnumTest sixthDay = new EnumTest(Day.SATURDAY);
sixthDay.tellItLikeItIs();
EnumTest seventhDay = new EnumTest(Day.SUNDAY);
seventhDay.tellItLikeItIs();}
}
Mondays are bad.
Midweek days are so-so.
Fridays are better.
Weekends are best.
Weekends are best.
Java programming language enum types are much more powerful than their counterparts in other languages. The
enumdeclaration defines a class (called an enum type). The enum class body can include methods and other fields. The compiler automatically adds some special methods when it creates an enum. For example, they have a staticvaluesmethod that returns an array containing all of the values of the enum in the order they are declared. This method is commonly used in combination with the for-each construct to iterate over the values of an enum type. For example, this code from thePlanetclass example below iterates over all the planets in the solar system.
for (Planet p : Planet.values()) {
System.out.printf(“Your weight on %s is %f%n”,
p, p.surfaceWeight(mass));
}
In the following example,
Note: All enums implicitly extendjava.lang.Enum. Since Java does not support multiple inheritance, an enum cannot extend anything else.
Planetis an enum type that represents the planets in the solar system. They are defined with constant mass and radius properties.
Each enum constant is declared with values for the mass and radius parameters. These values are passed to the constructor when the constant is created. Java requires that the constants be defined first, prior to any fields or methods. Also, when there are fields and methods, the list of enum constants must end with a semicolon.
In addition to its properties and constructor,
Note: The constructor for an enum type must be package-private or private access. It automatically creates the constants that are defined at the beginning of the enum body. You cannot invoke an enum constructor yourself.
Planethas methods that allow you to retrieve the surface gravity and weight of an object on each planet. Here is a sample program that takes your weight on earth (in any unit) and calculates and prints your weight on all of the planets (in the same unit):
If you runpublic enum Planet {
MERCURY (3.303e+23, 2.4397e6),
VENUS (4.869e+24, 6.0518e6),
EARTH (5.976e+24, 6.37814e6),
MARS (6.421e+23, 3.3972e6),
JUPITER (1.9e+27, 7.1492e7),
SATURN (5.688e+26, 6.0268e7),
URANUS (8.686e+25, 2.5559e7),
NEPTUNE (1.024e+26, 2.4746e7);private final double mass; // in kilograms
private final double radius; // in meters
Planet(double mass, double radius) {
this.mass = mass;
this.radius = radius;
}
private double mass() { return mass; }
private double radius() { return radius; }// universal gravitational constant (m3 kg-1 s-2)
public static final double G = 6.67300E-11;double surfaceGravity() {
return G * mass / (radius * radius);
}
double surfaceWeight(double otherMass) {
return otherMass * surfaceGravity();
}
public static void main(String[] args) {
double earthWeight = Double.parseDouble(args[0]);
double mass = earthWeight/EARTH.surfaceGravity();
for (Planet p : Planet.values())
System.out.printf(“Your weight on %s is %f%n”,
p, p.surfaceWeight(mass));
}
}
Planet.classfrom the command line with an argument of 175, you get this output:
$ java Planet 175
Your weight on MERCURY is 66.107583
Your weight on VENUS is 158.374842
Your weight on EARTH is 175.000000
Your weight on MARS is 66.279007
Your weight on JUPITER is 442.847567
Your weight on SATURN is 186.552719
Your weight on URANUS is 158.397260
Your weight on NEPTUNE is 199.207413
