Java Reference
In-Depth Information
Java syntax: instance-function call
variable . name
(
arguments
)
Java syntax: instance-procedure call
variable . name
(
arguments
);
Example
:
w.getTitle()
Example
:
w.setTitle("Peace");
Purpose
: To call the function
name
that occurs
in the folder whose name is in variable
vari-
able
. A function call is an expression and does
not end in a semicolon.
Purpose
: To call the procedure
name
that occurs in the
folder whose name is in variable
variable
. A procedure
call ends in a semicolon.
a reference to a component of the folder follows; the phrase “
show
” is a reference
to procedure
show
within that folder; and the parentheses indicate the (empty) list
of arguments. In general, in order to access a component (variable or method) of
a folder, use the form
<
variable that contains the name of the folder
>
.
<
component-name
>
We summarize how you can create and show on your monitor a window that
is associated with a JFrame. Just type the following three lines into the
Interactions pane of DrJava, and the window will appear in the upper left corner:
import
javax.swing.*;
JFrame window;
window=
new
JFrame("first Title");
window.show();
The first line says that the classes in package
javax.swing
may be used.
The second line declares variable
window
. The fourth line creates a manila fold-
er of class
JFrame
and stores its name in
window
. The fourth line shows the win-
dow on the monitor.
Getter and setter methods
A folder of class
JFrame
contains lots of information about the window with
which it is associated. One can generally get this information using calls on cer-
tain functions that appear in each folder of the class. In Java parlance, these are
called
getter
methods because they
get
a value from the folder. For example,
below, we show a few function calls and the results of their evaluation:
function call
value
title of the window
window.getTitle()
height of the window, in “pixels”
window.getHeight()
width of the window, in “pixels”
window.getWidth()
Thus, a getter method is a function that retrieves a value from an object.
A
setter method
is a procedure that sets (or changes) a value in an object. For
example, you can change the title of the window to “another title” by executing
the procedure call:
Search WWH ::
Custom Search