You are on page 1of 5

Chapter 8

Objects
Every program that you write or at least one thing that is being created
or manipulated by the program.
This thing is the object.
An object is characterized by its STATE and its BEHAVIOR
For example, a book has a state described by its title, author, whether
it's on the shelf or not, and so on.
Java has a way to represent an object as a variable in a program. This variable
is called an OBJECT REFERENCE.
Class
A class is a software blueprint for implementing objects of a given type
. An object is a single INSTANCE of
the class. In a program there will often be several different INSTANCES
of a given class type.
The current STATE of a given object is maintained in its DATA FIELDS (va
riables that make up the object) or
INSTANCE VARIABLES provided by the class.
The methods of the class provides both the behavior exhibted by the obje
ct and the operations that manipulate
the object.
Combining an objects data and methods into a single unit called a class is known
as ENCAPSULATION.
Public, private, static
Public
A public class name signals that the class is usable by all clie
nt programs.
A public method is accessible to all client programs. Clients, h
owever, are not privy to the class implementation
and may not access the private instance variables (i.e. the obje
cts data) and the private methods of the class.
Restriction of access is known as information hiding
Private
A private method or variable in a class can be accessed only by
the methods of that class.
Static
A static variable (i.e. class variable) contains a value that is
shared by all instances of the class.
Exp)
public class Employee{
private String name;
private static int employeeCount = 0;
public Employee(<parameter list>){
<initializing of private instanc

e variables>
}
.
.
.
other methods
}
A static final variables are constants
Types of Methods
Constructor Method
A constructor creates an object of the class. It always has the
same name as the class. Also, a constructor has no return
type.
Having several constructors provides different ways of initializ
ing class objects. For exaple, there are two constructors
in the BankAccount class.
Remember that having multiple constructors is an example of meth
od overloading.
Accessor Methods
An accessor method accesses a class object without altering the
object. An accessor method returns some information
about the object (A lot of the times the method name will start
with the word get).
Mutator Method
A mutator method changes the state of an object by modifying at
least one of its instance variables. (A lot of times--but
not always--the method name will start with the word set ie setN
ame, setPassword, setColor, etc.)
To access a mutator method through a client program isdone the s
ame way as an accessor method: using an object variable
with the dot operator.
A client program may use the methods as follows:
b1.withdraw("Mattw", 200.0);
b2.deposit("DannyB", 35.60);
b1 and b2 are the IMPLICIT parameters. Checked at Run Time.
Within the ( ) are the EXPLICIT parameters. Checked at Compile time.
static methods versus instance methods
The methods discussedd in the preceding sections (constructors, accessor
s and mutators) all operate on individual objects. They are called
instance methods.
A method that performs an operation for the entire class, not its indivi
dual objects, is called a static method (sometimes called
class methods). A static method uses the keyword static. If the code in
a static method tries to call an instance method or invoke a private
instance variable, a syntax error will occur. A static method can use a
static variable.

Exp)
public static int getEmployeeCount(){
return employeeCount;
}
Recall that an instance method is invoked in a client program by using a
n object variable followed by the dot operator followed by the method name.
Exp)
BankAccount b = new BankAccount();
b.deposit(password, amount);
//This invokes the deposit method for BankAccount object b
A static method by contrast, is invoked by using the class name with the
dot operator.
Exp)
double interestRate = BankAccount.getInterestRate();
Remember that you don't necessarily need the class name before the dot.
But it is a good idea for readability.
Static Methods in a Driver Class
Often a class that contains the main() method is used as a driver progra
m(i.e. client program) to test other classes.
Usually such a class creates no objects of its own class. So all the met
hod in the client program must be static.
Scope
The instance variables, static variable, and methods of a class belong t
o that class' scope, which extends from the opening brace to the
closing brace of the class.
Within that class, all instance variables and methods are accessible and
can be referred to simply by name (no dot operator required).
In contrast, a local variable's scope extends from teh point where it wa
s declared to the end of the block in which its declaration occurs.
this keyword
An instance method is always called for a particular object. This object
is an implicit paramter for the method and is referred to with the keyword
this.
In the implementation of instance mthods, all instance variables can be
written with the prefix this followed by the dot operator.
References
References vs Primitive Types
All objects are reference data types. The difference between pri
mitive types and reference types lies in the way that they are stored
Primitive Types Example
int num1 = 3;
int num2 = num1;
num1

num2

[__3__] [__3__]
Reference Type Examples
Date d = new Date(2, 17, 1948);
d-->

|Date
|myMonth [__2__]
|myDay [__17__]
|myYear [__1948__]

Date birthday = d;
This statement creates the reference variable birthday, which co
ntains the same address as d.
d-->
birthday--->

|Date
|myMonth [__2__]
|myDay [__17__]
|myYear [__1948__]

Having two or more references for the same object is known as al


iasing. Aliasing can cause unintended problems for the programmer.
The statement:
d.changeDate();
will automatically change the object referred to by birthday as
well. Any change to one object will change the other.
What the programmer probably intended was to create a second obj
ect called birthday whose attributes exactly matched those of the d object refer
ence
For example,
Date birthday = new Date(d.getMonth(), d.getDay(), d.getYear());
The statement d.changeDate() will now leave the birthday object
unchanged.
The null reference
The declaraton:
BankAccount b;
defines a reference b that is uninitialized. (To construct the object th
at b refers to, requires the new operator and a BankAccount constructor).
An uninitialized object variable like b is called a null reference or nu
ll pointer.
You can test whether a variable like b refers to an object or is uniniti
alized byusing the keyword null.
Exp) if(b == null)
An attempt to invoke an instance method with a null reference will cause
your program to terminate.
For example,
public class Personal Finances{
BankAccount b; //b is a null reference
b.withdraw(password, amount);

}
This will throw an exception because b has not been constructed with new
keyword.
If you fail to initialize a local variable in a method before you use it
, you'll get a compile-time error.
With uninitialized instance variables, the compiler provides reasonable
default values for primitive types and null for reference types.
Method Parameters
Primitive Types
Primitive types are passed by value. This means that during the
execution of the method, the parameters are local to that method. This means tha
t
any changes made to the parameters will not affect the values of
the arguments in the calling program.
Exp) public class ParmTest{
public static void main(String[] args){
int a = 7;
int b = 6;
foo(a,b);
print(a + " " + b); //output is 7 6
}
public static void foo(int x, int y){
x = 3;
y = 4;
}
}
Passing objects as Parameters
In Java object references are also passed by value. What this me
ans is that it is not possible for a method to replace an object with another on
e.
You can't change the reference that was passed. It is, however,
possible to change the state of an object to which the parameter refers through
methods that act on the object.

You might also like