Java Reference
In-Depth Information
indicates that main is a static method. A static method is special, because you can call it
without first creating an object of the class in which the method is declared— in this case class
AccountTest . We discuss static methods in detail in Chapter 6.
Notes on import Declarations
Notice the import declaration in Fig. 3.2 (line 3), which indicates to the compiler that the
program uses class Scanner . As you learned in Chapter 2, classes System and String are
in package java.lang , which is implicitly imported into every Java program, so all pro-
grams can use that package's classes without explicitly importing them. Most other classes
you'll use in Java programs must be imported explicitly .
There's a special relationship between classes that are compiled in the same directory,
like classes Account and AccountTest . By default, such classes are considered to be in the
same package—known as the default package . Classes in the same package are implicitly
imported into the source-code files of other classes in that package. Thus, an import dec-
laration is not required when one class in a package uses another in the same package—
such as when class AccountTest uses class Account .
The import declaration in line 3 is not required if we refer to class Scanner
throughout this file as java.util.Scanner , which includes the full package name and class
name . This is known as the class's fully qualified class name . For example, line 10 of
Fig. 3.2 also could be written as
java.util.Scanner input = new java.util.Scanner(System.in);
Software Engineering Observation 3.1
The Java compiler does not require import declarations in a Java source-code file if the
fully qualified class name is specified every time a class name is used. Most Java
programmers prefer the more concise programming style enabled by import declarations.
3.2.6 Software Engineering with private Instance Variables and
public set and get Methods
As you'll see, through the use of set and get methods, you can validate attempted modifi-
cations to private data and control how that data is presented to the caller—these are
compelling software engineering benefits. We'll discuss this in more detail in Section 3.5.
If the instance variable were public , any client of the class—that is, any other class
that calls the class's methods—could see the data and do whatever it wanted with it,
including setting it to an invalid value.
You might think that even though a client of the class cannot directly access a private
instance variable, the client can do whatever it wants with the variable through public set
and get methods. You would think that you could peek at the private data any time with
the public get method and that you could modify the private data at will through the
public set method. But set methods can be programmed to validate their arguments and
reject any attempts to set the data to bad values, such as a negative body temperature, a day
in March out of the range 1 through 31, a product code not in the company's product
catalog, etc. And a get method can present the data in a different form. For example, a
Grade class might store a grade as an int between 0 and 100, but a getGrade method
might return a letter grade as a String , such as "A" for grades between 90 and 100, "B"
for grades between 80 and 89, etc. Tightly controlling the access to and presentation of
 
 
Search WWH ::




Custom Search