Java Reference
In-Depth Information
EXAMPLE
public
int
getDay()
{
return
day;
}
void
Method Definition
SYNTAX
public void
Method_Name
(
Parameter_List
)
{
<
List of statements
>
}
If there are no
Parameters,
then the parentheses are empty.
EXAMPLE
public
void
writeOutput()
{
System.out.println(month + " " + day + ", " + year);
}
All method definitions are inside of some class definition. See Display 4.2 to see these
example method definitions in the context of a class.
When an instance variable name is used in a method definition, it refers to an instance
variable of the calling object.
return
Statements
The definition of a method that returns a value must have one or more
return
state-
ments. A
return
statement specifies the value returned by the method and ends the
method invocation.
SYNTAX
return
Expression
;
EXAMPLE
public
int
getYear()
{
return
year;
}
A
void
method definition need not have a
return
statement. However, a
return
state-
ment can be used in a
void
method to cause the method to immediately end. The form for
a
return
statement in a
void
method is
return
;