Java Reference
In-Depth Information
SR6 . The general inside-out rule in programming languages says that a subpart
of a construct can reference anything that is declared in or is accessible in the
construct, unless the subpart redeclared it. A method body can reference names
declared in the class in which the method is declared, unless the name is rede-
clared as a parameter of the method.
SR7 .
Retrieve a value from the instance.
SR8 .
Initialize fields of a newly created instance.
SR9 . Make this (7, 60); the first statement of the constructor.
SR10 . Yield a description of the fields of the object in which toString appears.
SR11 . We do not answer this one here.
SR12 . Class Coordinate will have two private fields, the x and y coordinates of
a point. There will be a constructor with two parameters to initialize the fields,
as well as getter and setter methods for them. Finally, there will be a toString
function, which will produce a String that looks like this (depending on the
coordinates): "(5,2)" .
Using classes
The class as a type
In the next subsection 3.2.2, we show how a program can create new folders (or
instances) of class Employee and other classes. Generally, when a folder is cre-
ated, its name (the label on the folder) is placed in a variable, just as an integer
is placed in an int variable. To declare the variable that can contain the name of
a folder, we use the name of the class as a type.
For example, the first declaration below declares x to be a variable that can
contain an int , while the second declaration declares v to be a variable that can
contain the name of an instance of class Employee (or, to abbreviate, the name
of an Employee ):
int x;
Employee v;
Suppose we assigned 4 to x and the name of the object in Fig. 3.2 to v (we
show how to do this in the next subsection). Then, the variables look like this:
v 0
It is important to realize that the name of the instance, and not the instance
itself, is placed in v. The consequences of this are discussed in Sec. 3.2.4.
From the above, we see that class Employee can be viewed as a type. Since
it is also a class, we sometimes talk of Employee as a class-type . And just as we
say that x is of type int , we say that v is of type or class-type Employee .
Search WWH ::

Custom Search