Java Reference
In-Depth Information
3.1
Introduction
3.2
Instance Variables,
set
Methods and
get
Methods
3.2.1
Account
Class with an Instance
Variable, a
set
Method and a
get
Method
3.2.2
AccountTest
Class That Creates
and Uses an Object of Class
Account
3.2.3 Compiling and Executing an App with
Multiple Classes
3.2.4
Account
UML Class Diagram with an
Instance Variable and
set
and
get
Methods
3.2.5 Additional Notes on Class
AccountTest
3.2.6 Software Engineering with
private
Instance Variables and
public
set
and
get
Methods
3.3
Primitive Types vs. Reference Types
3.4
Account
Class: Initializing Objects
with Constructors
3.4.1 Declaring an
Account
Constructor
for Custom Object Initialization
3.4.2 Class
AccountTest
: Initializing
Account
Objects When They're
Created
3.5
Account
Class with a Balance;
Floating-Point Numbers
3.5.1
Account
Class with a
balance
Instance Variable of Type
double
3.5.2
AccountTest
Class to Use Class
Account
3.6
(Optional) GUI and Graphics Case
Study: Using Dialog Boxes
3.7
Wrap-Up
Summary | Self-Review Exercises | Answers to Self-Review Exercises | Exercises | Making a Difference
[
Note:
This chapter depends on the terminology and concepts of object-oriented program-
ming introduced in Section 1.5, Introduction to Object Technology.]
In Chapter 2, you worked with
existing
classes, objects and methods. You used the
pre-
defined
standard output object
System.out
,
invoking
its methods
print
,
println
and
printf
to display information on the screen. You used the
existing
Scanner
class to create
an object that reads into memory integer data typed by the user at the keyboard. Through-
out the topic, you'll use many more
preexisting
classes and objects—this is one of the great
strengths of Java as an object-oriented programming language.
In this chapter, you'll learn how to create your own classes and methods. Each new
class
you create becomes a new
type
that can be used to declare variables and create objects.
You can declare new classes as needed; this is one reason why Java is known as an
extensible
language.
We present a case study on creating and using a simple, real-world bank account
class—
Account
. Such a class should maintain as
instance variables
attributes such as its
name
and
balance
, and provide
methods
for tasks such as querying the balance (
getBalance
),
making deposits that increase the balance (
deposit
) and making withdrawals that decrease
the balance (
withdraw
). We'll build the
getBalance
and
deposit
methods into the class in
the chapter's examples and you'll add the
withdraw
method in the exercises.
In Chapter 2 we used the data type
int
to represent integers. In this chapter, we intro-
duce data type
double
to represent an account balance as a number that can contain a
dec-
imal point
—such numbers are called
floating-point numbers
. [In Chapter 8, when we get a
bit deeper into object technology, we'll begin representing monetary amounts precisely with
class
BigDecimal
(package
java.math
) as you should do when writing industrial-strength
monetary applications.]