Java Reference
In-Depth Information
Section 3.2.4
Account
UML Class Diagram with an Instance Variable and
set
and
get
Methods
• In the UML, each class is modeled in a class diagram (p. 77) as a rectangle with three compart-
ments. The top one contains the class's name centered horizontally in boldface. The middle one
contains the class's attributes, which correspond to instance variables in Java. The bottom one
contains the class's operations (p. 78), which correspond to methods and constructors in Java.
• The UML represents instance variables as an attribute name, followed by a colon and the type.
• Private attributes are preceded by a minus sign (
-
) in the UML.
• The UML models operations by listing the operation name followed by a set of parentheses. A
plus sign (
+
) in front of the operation name indicates that the operation is a public one in the
UML (i.e., a
public
method in Java).
• The UML models a parameter of an operation by listing the parameter name, followed by a colon
and the parameter type between the parentheses after the operation name.
• The UML indicates an operation's return type by placing a colon and the return type after the
parentheses following the operation name.
• UML class diagrams do not specify return types for operations that do not return values.
• Declaring instance variables
private
is known as data hiding or information hiding.
Section 3.2.5 Additional Notes on Class
AccountTest
• You must call most methods other than
main
explicitly to tell them to perform their tasks.
• A key part of enabling the JVM to locate and call method
main
to begin the app's execution is
the
static
keyword, which indicates that
main
is a
static
method that can be called without first
creating an object of the class in which the method is declared.
• Most classes you'll use in Java programs must be imported explicitly. There's a special relation-
ship between classes that are compiled in the same directory. By default, such classes are consid-
ered to be in the same package—known as the default package. Classes in the same package are
implicitly imported into the source-code files of other classes in that package. An
import
decla-
ration is not required when one class in a package uses another in the same package.
•An
import
declaration is not required if you always refer to a class with its fully qualified class
name, which includes its package name and class name.
Section 3.2.6 Software Engineering with
private
Instance Variables and
public
set
and
get
Methods
• Declaring instance variables
private
is known as data hiding or information hiding.
Section 3.3 Primitive Types vs. Reference Types
• Types in Java are divided into two categories—primitive types and reference types. The primitive
types are
boolean
,
byte
,
char
,
short
,
int
,
long
,
float
and
double
. All other types are reference
types, so classes, which specify the types of objects, are reference types.
• A primitive-type variable can store exactly one value of its declared type at a time.
• Primitive-type instance variables are initialized by default. Variables of types
byt
e,
char
,
short
,
int
,
long
,
float
and
double
are initialized to
0
. Variables of type
boolean
are initialized to
false
.
• Reference-type variables (called references; p. 81) store the location of an object in the comput-
er's memory. Such variables refer to objects in the program. The object that's referenced may
contain many instance variables and methods.
• Reference-type instance variables are initialized by default to the value
null
.