interface Verbose {
int SILENT = 0;
int TERSE = 1;
int NORMAL = 2;
int VERBOSE = 3;
void setVerbosity(int level);
int getVerbosity();
}
SILENT, TERSE, NORMAL, and VERBOSE can be passed to the setVerbosity method, giving names to
constant values that represent specific meanings. In this particular case, the verbosity levels could be more
effectively represented as the constants of an enumeration type nested within the Verbose interface.
Enumeration types are discussed in Chapter 6.
If you need shared, modifiable data in your interface you can achieve this by using a named constant that
refers to an object that holds the data. A nested class is good for defining that object, so we defer an example
until Chapter 5.
4.2.2. Interface Methods
The methods declared in an interface are implicitly abstract because no implementation is, or can be,
given for them. For this reason the method body is simply a semicolon after the method header. By
convention, the abstract modifier on the method declaration is omitted.
No other method modifiers are permitted on an interface method declaration, except for annotationssee
Chapter 15. They are implicitly public and so can have no other access modifier. They cannot have
modifiers that define implementation characteristicssuch as native, synchronized, or
strictfpbecause an interface does not dictate implementation, and they cannot be final because they
haven't been implemented yet. Of course, the implementation of these methods within a specific class can
have any of these modifiers. Interface methods can never be static because static methods can't be
abstract.
4.2.3. Interface Modifiers
An interface declaration can be preceded by interface modifiers:
- annotations Annotations and annotation types are discussed in Chapter 15.
public A public interface is publicly accessible. Without this modifier an interface is only
accessible within its own package.
•
abstract All interfaces are implicitly abstract because their methods are all abstractthey have no
implementation. Again, by convention, the abstract modifier is always omitted.
•
strict floating point An interface declared strictfp has all floating-point arithmetic, defined within
the interface, evaluated strictly. This applies to initialization expressions for constants and to all
nested types declared in the interface. In contrast to classes, this does not imply that each method in
the interface is implicitly strictfp because that is an implementation detail. See Section 9.1.3 on
page 203 for details.
•
When multiple modifiers are applied to the same interface declaration, we recommend using the order listed
above.