Programacion

Solo disponible en BuenasTareas
  • Páginas : 2 (495 palabras )
  • Descarga(s) : 0
  • Publicado : 1 de marzo de 2012
Leer documento completo
Vista previa del texto
Module 7
Advanced Class Features
Objectives
• 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

Relevance
• 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
• 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
Class attributes are shared among all instances of a class:

Static Initializers
• 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.

The finalKeyword
• 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
constructor.
• 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

Static Imports
•Astatic import imports the static members from a class:
import static <pkg_list>.<class_name>.<member_name>;
OR
import static <pkg_list>.<class_name>.*;
• A static...
tracking img