Advanced Class Features
• Create static variables, methods, and initializers
• Create final classes, methods, and variables
• Create and use enumerated types
• Use thestatic import statement
• Create abstract classes and methods
• Create and use an interface
• How can you create a constant?
• Howcan you declare data that is shared by all instances of agiven class?
• How can you keep a class or method from being subclassed or overridden?
• The statickeyword is used as a modifier on variables, methods, and nested classes.
• Thestatic keyword declares the attribute or method is associated with the class as a whole rather than any particular instance of that class.
• Thus static members are often called class members, suchas class attributes or class methods.
Class attributes are shared among all instances of a class:
• A class can contain code in a static block that does notexist within a method body.
• Static block code executes once only, when the class is loaded.
• Usually, a static block is used to initialize static (class) attributes.
• Youcannot subclass a final class.
• You cannot override a final method.
• Afinal variable is a constant.
• You can set a final variable once only, but that
assignment can occur independently of thedeclaration;
this is called a blank final variable.
• A blank final instance attribute must be set in every
• A blank final method variable must be set in the
method body before beingused.
Old-Style Enumerated Type Idiom
This enumerated type idiom has several problems:
• Not type-safe
• No namespace
• Brittle character
• Uninformative printed values
•Astatic import imports the static members from a class:
import static <pkg_list>.<class_name>.<member_name>;
import static <pkg_list>.<class_name>.*;
• A static...