Introduction
In a Java application, Java classes are the fundamental building blocks of all implementation constructs, such as
JavaBeans, EJBs, Servlets, and Applets. All implementation logic is provided in Java classes.
Classes are templates from which objects are created. Classes encapsulate data structures with the algorithms for
manipulating the data. A class is usually a factory for the objects it defines.
These guidelines do not explain the concept of classes or object-oriented design in detail. The discussion that follows
assumes that the reader is familiar with object-oriented design. For a detailed discussion on object-oriented design,
please see the reference section.
Java Class Features
Java classes include:
-
Member functions
-
Member data
-
Static functions
-
Static data
-
Inheritance
-
Implementation of interfaces
Member functions implement some behavior relating to individual objects. Member functions are executed in the context
of an individual object.
Member data defines the data structures for objects, in the form of a set of variable declarations. The member data is
ideally only accessed through member functions.
Static functions are methods that are executed outside the context of the individual objects they define.
Static data belongs to the class defining it rather than the individual objects it defines. There is exactly one
instance of static data in a running application. The static data is typically only accessed through static functions,
although some designs call for instances directly accessing the static data.
Java classes can be related through inheritance, expressed using the Java keyword "extends". Inheritance provides a
sophisticated technique for code reuse, allowing designers to extract common data structures and behavior into
superclasses, leaving specific behavior in subclasses.
An interface declares a set of methods whose implementation is provided in classes. A class can implement several
interfaces and interfaces can be implemented by many classes. Separation of interface from implementation increases
decoupling of classes. Accessing objects through interfaces provides a clean way to use polymorphism.
Identifying Java Classes
In a J2EE application, Java classes can be identified to in order to support the design of J2EE elements. For more
information on identifying these elements, see the following guidelines:
The guidelines for identifying additional Java classes are no different than the guidelines for identifying any other
kind of classes. For more information, see Task: Identify Design Elements.
Modeling Java Classes
Java classes are modeled as UML classes.
In the diagram below, we show the UML representation of a Java class and the corresponding Java source code.
|
class MyClass {
private int myMemberData;
private String myOtherMemberData;
private static int myStaticData;
public int getMyMemberData() {
return this.myOtherMemberData;
}
public static int getMyStaticData() {
return MyClass.myStaticData;
}
}
|
|