Java Reference
In-Depth Information
What would you expect the following to return? Think carefully about this before you try it, and
be sure to use the
t2
variable this time.
t2.getBalance()
Did you get the answer you expected? Can you find a connection between the variables
t1
and
t2
that would explain what is happening?
2.23
Summary
In this chapter, we have covered the basics of how to create a class definition. Classes contain
fields, constructors, and methods that define the state and behavior of objects. Within the body of
a constructor or method, a sequence of statements implements that part of its behavior. Local vari-
ables can be used as temporary data storage to assist with that. We have covered assignment state-
ments and conditional statements and will be adding further types of statements in later chapters.
Terms introduced in this chapter
field, instance variable, constructor, method, method header, method body,
actual parameter, formal parameter, accessor, mutator, declaration, initializa-
tion, block, statement, assignment statement, conditional statement, return
statement, return type, comment, expression, operator, variable, local variable,
scope, lifetime
Concept summary
■
object creation
Some objects cannot be constructed unless extra information is provided.
■
field
Fields store data for an object to use. Fields are also known as instance variables.
■
comment
Comments are inserted into the source code of a class to provide explanations to
human readers. They have no effect on the functionality of the class.
■
constructor
Constructors allow each object to be set up properly when it is first created.
■
scope
The scope of a variable defines the section of source code from which the variable can
be accessed.
■
lifetime
The lifetime of a variable describes how long the variable continues to exist before it
is destroyed.
■
assignment
Assignment statements store the value represented by the right-hand side of the
statement in the variable named on the left.
■
accessor method
Accessor methods return information about the state of an object.
■
mutator method
Mutator methods change the state of an object.
■
println
The method
System.out.println
prints its parameter to the text terminal.
Search WWH ::
Custom Search