Java Reference
In-Depth Information
3.6
Key concepts
•Class
. A
class
is a file drawer that contains two kinds of items: manila folders
of the class and components that were declared with modifier
static
.
• Instances and objects.
The terms
folder
,
instance
, and
object
are used inter-
changeably.
• Components: fields and methods
. The components of an
object
of a class are
instance variables
(or
fields
) and
instance methods
—variables and methods that
are defined in the class without modifier
static
. Thus, the class definition is a
template
for the folders of the class.
• Procedures, functions, constructors.
There are three kinds of method. A call
to a
function
produces a value. A call to a
procedure
performs some task but does
not produce a value. A call to a
constructor
initializes the fields of a newly cre-
ated object. A method call can have
arguments
, which are expressions.
• New-expression.
Evaluation of a new-expression
new
class-name
(
arguments
)
creates a new object of class
class-name
, initializes its fields by executing the
call
class-name
(
arguments
), and yields the name of the new object as its value.
• Calling a constructor
. A constructor is called either in a new-expression or as
the first statement of another constructor of the class; in the latter case, keyword
this
is used instead of the class name.
• Class as a type.
A class name may be used as a type, and a variable declared
with that type can contain the name of a folder of that type.
• Null.
The value
null
represents the absence of the name of an instance. If a
variable
u
contains
null
, attempting to access a component using
u.
component-
name
is an error.
• Information hiding.
The principle of information hiding says to hide informa-
tion from those who do not need it. We usually hide fields of an object by declar-
ing them to be
private
, although in rare cases, making them
public
is pre-
ferred.
• Inside-out rule.
The inside-out rule, used in most programming languages,
says that a program part can reference items declared or referenceable in sur-
rounding constructs, unless a redeclaration hides them.
• toString.
Put a function
toString
in (almost) every class to provide a descrip-
tion of the object in which the function appears.
• Equality testing.
A relation
c==b
tests whether the names (of objects) that are
in
c
and
b
are the same, not whether the contents of the objects are the same.
Write a function
equals
to test whether the contents of the objects are the same.
• Static items.
Make a variable static if it is a constant or if only one copy is
Search WWH ::
Custom Search