Java Reference
In-Depth Information
In this chapter, you learned how to create your own classes and methods, create objects of
those classes and call methods of those objects to perform useful actions. You declared in-
stance variables of a class to maintain data for each object of the class, and you declared
your own methods to operate on that data. You learned how to call a method to tell it to
perform its task and how to pass information to a method as arguments whose values are
assigned to the method's parameters. You learned the difference between a local variable
of a method and an instance variable of a class, and that only instance variables are initial-
ized automatically. You also learned how to use a class's constructor to specify the initial
values for an object's instance variables. You saw how to create UML class diagrams that
model the methods, attributes and constructors of classes. Finally, you learned about float-
ing-point numbers (numbers with decimal points)—how to store them with variables of
primitive type
double
, how to input them with a
Scanner
object and how to format them
with
printf
and format specifier
%f
for display purposes. [In Chapter 8, we'll begin rep-
resenting monetary amounts precisely with class
BigDecimal
.] You may have also begun
the optional GUI and Graphics case study, learning how to write your first GUI applica-
tions. In the next chapter we begin our introduction to control statements, which specify
the order in which a program's actions are performed. You'll use these in your methods to
specify how they should order their tasks.
Summary
Section 3.2 Instance Variables,
set
Methods and
get
Methods
• Each 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 Java is known as an extensible language.
Section 3.2.1
Account
Class with an Instance Variable, a
set
Method and a
get
Method
• Each class declaration that begins with the access modifier (p. 71)
public
must be stored in a file
that has the same name as the class and ends with the
.java
filename extension.
• Every class declaration contains keyword
class
followed immediately by the class's name.
• Class, method and variable names are identifiers. By convention all use camel case names. Class
names begin with an uppercase letter, and method and variable names begin with a lowercase
letter.
• An object has attributes that are implemented as instance variables (p. 72) and carried with it
throughout its lifetime.
• Instance variables exist before methods are called on an object, while the methods are executing
and after the methods complete execution.
• A class normally contains one or more methods that manipulate the instance variables that be-
long to particular objects of the class.
• Instance variables are declared inside a class declaration but outside the bodies of the class's meth-
od declarations.
• Each object (instance) of the class has its own copy of each of the class's instance variables.
• Most instance-variable declarations are preceded with the keyword
private
(p. 72), which is an
access modifier. Variables or methods declared with access modifier
private
are accessible only
to methods of the class in which they're declared
.