Java Reference
In-Depth Information
a compilation error will occur. Thus, public classes Account and AccountTest (Fig. 3.2)
must be declared in the separate files Account.java and AccountTest.java , respectively.
Every class declaration contains the keyword class followed immediately by the
class's nameā€”in this case, Account . Every class's body is enclosed in a pair of left and right
braces as in lines 6 and 20 of Fig. 3.1.
Identifiers and Camel Case Naming
Class names, method names and variable names are all identifiers and by convention all use
the same camel case naming scheme we discussed in Chapter 2. Also by convention, class
names begin with an initial uppercase letter, and method names and variable names begin
with an initial lowercase letter.
Instance Variable name
Recall from Section 1.5 that an object has attributes, implemented as instance variables
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 exe-
cution. Each object (instance) of the class has its own copy of the class's instance variables.
A class normally contains one or more methods that manipulate the instance variables be-
longing to particular objects of the class.
Instance variables are declared inside a class declaration but outside the bodies of the
class's methods. Line 7
private String name; // instance variable
declares instance variable name of type String outside the bodies of methods setName (lines
10-13) and getName (lines 16-19). String variables can hold character string values such
as "Jane Green" . If there are many Account objects, each has its own name . Because name
is an instance variable, it can be manipulated by each of the class's methods.
Good Programming Practice 3.1
We prefer to list a class's instance variables first in the class's body, so that you see the names
and types of the variables before they're used in the class's methods. You can list the class's
instance variables anywhere in the class outside its method declarations, but scattering the
instance variables can lead to hard-to-read code.
Access Modifiers public and private
Most instance-variable declarations are preceded with the keyword private (as in line 7).
Like public , private is an access modifier . Variables or methods declared with access mod-
ifier private are accessible only to methods of the class in which they're declared . So, the
variable name can be used only in each Account object's methods ( setName and getName in
this case). You'll soon see that this presents powerful software engineering opportunities.
setName Method of Class Account
Let's walk through the code of setName 's method declaration (lines 10-13):
public void setName(String name)
{
this .name = name; // store the name
}
This line is the method header
 
Search WWH ::




Custom Search