Java Reference
In-Depth Information
2.2.1 Access to Class and Instance Variables
In addition to static , a field can be declared using the optional access modifiers private , public , or protected . In
the Book class, we declared all our instance variables using private . The keyword private indicates that the variable
is “known” only inside the class and can be manipulated directly only by methods within the class. In other words, no
method from outside the class has direct access to a private variable. However, as we will see shortly, we can provide
public methods that other classes can use to set and access the values of private variables. This way, we ensure that
class data can be changed only by methods within the class.
Declaring a variable public means that it can be accessed directly from outside the class. Hence, other classes
can “do as they please” with a public variable. For example, if Discount is declared as public , then any other class
can access it using Book.Discount and change it in any way it pleases. This is not normally encouraged since a class
then loses control over its data.
For the most part, we will declare a class's fields using private . Doing so is the first step in implementing the
concept of information hiding , which is part of the philosophy of object-oriented programming. The idea is that users
of an object must not be able to deal directly with the object's data; they should do so via the object's methods.
Declaring a variable protected means that it can be accessed directly from the class and any of its subclasses, as
well as other classes in the same package. We will not use protected variables in this introduction.
If no access modifier is specified, then the variable can be accessed directly by other classes in the same
package only.
A method within a class can refer to any variable ( static or non- static , public or private ) in the class simply
by using its name. (An exception is that a static method cannot access non-static variables.) If a static variable is
known outside the class (that is, not private ), it is referenced by qualifying the variable with the class name, as in
Book.Discount and Book.MinBooks .
From outside the class, a nonprivate instance variable can be referenced only via the object to which it belongs;
this is illustrated in the next Section. However, as indicated, good programming practice dictates that, most of the
time, our variables will be declared private , so the notion of direct access from outside the class does not arise.
2.2.2 Initializing Class and Instance Variables
When the Book class is loaded, storage is immediately allocated to the class variables Discount and MinBooks ; they
are then assigned initial values of 0.25 and 5, respectively. The meaning behind these variables is that if five or more
copies of a book are sold, then a 25 percent discount is given. Since these values apply to all topics, it would be a waste
of storage to store them with each book's data, hence their declaration as static variables. All book objects will have
access to the single copy of these variables. (Note, however, that if we wanted to vary these values from book to book,
then they become attributes of a specific book and would have to be declared non-static.)
When the class is first loaded, no storage is allocated to the instance (non-static) variables. At this time, we have
only a specification of the instance variables, but none actually exists as yet. They will come into existence when an
object is created from the class. The data for an object is determined by the instance variables. When an object is
“created,” storage is allocated for all the instance variables defined in the class; each object created has its own copy of
the instance variables. To create an object, we use the keyword new as in the following:
Book b;
b = new Book();
The first statement declares b as a variable of type Book . From this, we see that a class name is considered to be a
type (similar to int or char ) and can be used to declare variables. We say that b is an object variable of type Book .
The declaration of b does not create an object; it simply creates a variable whose value will eventually be a pointer
to an object. When declared as shown, its value is undefined.
The second statement finds some available memory where a Book object can be stored, creates the object, and
stores the address of the object in b . (Think of the address as the first memory location occupied by the object. If the
object occupies locations 2575 to 2599 , its address is 2575 .) We say that b contains a reference or pointer to the object.
Thus, the value of an object variable is a memory address , not an object. This is illustrated as shown in Figure 2-1 .
 
Search WWH ::




Custom Search