Java Reference
In-Depth Information
Top compartment
Middle compartment
Account
- name : String
+ setName(name : String)
+ getName() : String
Bottom compartment
Fig. 3.3
|
UML class diagram for class
Account
of Fig. 3.1.
Middle Compartment
The
middle
compartment contains the
class's attribute
name
, which corresponds to the in-
stance variable of the same name in Java. Instance variable
name
is
private
in Java, so the
UML class diagram lists a
minus sign (
-
) access modifier
before the attribute name. Follow-
ing the attribute name are a
colon
and the
attribute type
, in this case
String
.
Bottom Compartment
The
bottom
compartment contains the class's
operations
,
setName
and
getName
, which
correspond to the methods of the same names in Java. The UML models operations by
listing the operation name preceded by an
access modifier
, in this case
+ getName
. This plus
sign (
+
) indicates that
getName
is a
public
operation in the UML (because it's a
public
method in Java). Operation
getName
does
not
have any parameters, so the parentheses fol-
lowing the operation name in the class diagram are
empty
, just as they are in the method's
declaration in line 16 of Fig. 3.1. Operation
setName
, also a public operation, has a
String
parameter called
name
.
Return Types
The UML indicates the
return type
of an operation by placing a colon and the return type
after
the parentheses following the operation name.
Account
method
getName
(Fig. 3.1)
has a
String
return type. Method
setName
does not
return a value (because it returns
void
in Java), so the UML class diagram
does not
specify a return type after the parentheses of
this operation.
Parameters
The UML models a parameter a bit differently from Java by listing the parameter name,
followed by a colon and the parameter type in the parentheses after the operation name.
The UML has its own data types similar to those of Java, but for simplicity, we'll use the
Java data types.
Account
method
setName
(Fig. 3.1) has a
String
parameter named
name
,
so Fig. 3.3
lists
name : String
between the parentheses following the method name.
static
Method
main
In Chapter 2, each class we declared had one method named
main
. Recall that
main
is a
special method that's
always
called automatically by the Java Virtual Machine (JVM) when
you execute an app. You must call most other methods
explicitly
to tell them to perform
their tasks.
Lines 7-27 of Fig. 3.2 declare method
main
. A key part of enabling the JVM to locate
and call method
main
to begin the app's execution is the
static
keyword (line 7), which