Java Reference
In-Depth Information
class-type String instead of keyword void . This type defines the type of the
variable that the function returns.
Functions are different from procedures in that they return a value. To state
what a function returns, use a return statement , of the form
return expression ;
Execution of the return statement evaluates the expression , stops execution
of the function body, and yields the value of the expression as the result of the
function call. In getPreviousTitle , the expression is this .previousTitle ,
and its value is the value in that field.
Drawing a folder that contains a field
Figure 1.11 contains an instance of the revised class OurFrame . We have
placed field previousTitle in it, as well as getter method getPreviousTitle .
Field previousTitle is grayed out to indicate that it is private , so it cannot be
referenced outside the class.
Self-review exercises
Hopefully, you have had your IDE open while you read Sec. 1.4 and have tried
making JFrame s. These exercises will give you more practice with JFrame s and
with writing customized window classes.
SR1. Type into Java this import statement:
import javax.swing.*;
import javax.swing.*;
public class OurFrame extends JFrame {
/** Class invariant: previousTitle contains the previous title (initially "" ) */
private String previousTitle= "";
/** Set the title of this instance to contain the origin of the window */
public void setTitleToOrigin() {
this .previousTitle= this .getTitle();
this .setTitle("(" + this .getX() + ", " + this .getY() + ")");
/**= the previous title (empty "" if none) */
public String getPreviousTitle() {
return this .previousTitle;
Figure 1.10:
Class OurFrame , revised to maintain the previous title
Search WWH ::

Custom Search