Data Fields (Also Known As Properties) With Their
Data Fields (Also Known As Properties) With Their
Objects
Class Name: Circle
A class template
Data Fields:
radius is _______
Methods:
getArea
Circle Object 1
Circle Object 2
Circle Object 3
Data Fields:
radius is 10
Data Fields:
radius is 25
Data Fields:
radius is 125
Three objects of
the Circle class
Classes
Classes are constructs that define objects of the
same type. A Java class uses variables to define
data fields and methods to define behaviors.
Additionally, a class provides a special type of
methods, known as constructors, which are
invoked to construct objects from the class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Classes
class Circle {
/** The radius of this circle */
double radius = 1.0;
/** Construct a circle object */
Circle() {
}
Data field
Constructors
Method
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Class name
radius: double
Data fields
Circle()
Constructors and
Methods
Circle(newRadius: double)
getArea(): double
circle1: Circle
radius: 10
circle2: Circle
radius: 25
circle3: Circle
UML notation
for objects
radius: 125
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Constructors
Circle() {
}
Circle(double newRadius) {
radius = newRadius;
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Constructors, cont.
A constructor with no parameters is referred to as a
no-arg constructor.
Example:
new Circle();
new Circle(5.0);
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Default Constructor
A class may be declared without constructors. In
this case, a no-arg constructor with an empty body
is implicitly declared in the class. This constructor,
called a default constructor, is provided
automatically only if no constructors are explicitly
declared in the class.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Example:
Circle myCircle;
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
10
Declaring/Creating Objects
in a Single Step
ClassName objectRefVar = new ClassName();
Example:
Create an object
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
11
Accessing Objects
e.g., myCircle.radius
e.g., myCircle.getArea()
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
12
TestCircle1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
13
animation
Trace Code
Declare myCircle
myCircle
no value
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
14
animation
myCircle
no value
: Circle
yourCircle.radius = 100;
radius: 5.0
Create a circle
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
15
animation
Assign object
reference to myCircle
: Circle
radius: 5.0
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
16
animation
: Circle
radius: 5.0
yourCircle
no value
Declare yourCircle
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
17
animation
: Circle
yourCircle.radius = 100;
radius: 5.0
no value
yourCircle
: Circle
Create a new
Circle object
radius: 0.0
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
18
animation
: Circle
yourCircle.radius = 100;
radius: 5.0
: Circle
radius: 1.0
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
19
animation
: Circle
yourCircle.radius = 100;
radius: 5.0
yourCircle reference value
: Circle
Change radius in
yourCircle
radius: 100.0
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
20
Caution
Recall that you use
Math.methodName(arguments) (e.g., Math.pow(3, 2.5))
to invoke a method in the Math class. Can you invoke getArea() using
Circle1.getArea()? The answer is no. All the methods used before this
chapter are static methods, which are defined using the static keyword.
However, getArea() is non-static. It must be invoked from an object
using
objectRefVar.methodName(arguments) (e.g., myCircle.getArea()).
21
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
22
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
23
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
24
Example
Java assigns no default value to a local variable inside a method.
25
Primitive type
int i = 1
Object type
Circle c
reference
c: Circle
radius = 1
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
26
After:
2
Object type assignment c1 = c2
Before:
After:
c1
c1
c2
c2
c1: Circle
C2: Circle
c1: Circle
C2: Circle
radius = 5
radius = 9
radius = 5
radius = 9
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
27
Garbage Collection
As shown in the previous figure, after the
assignment statement c1 = c2, c1 points to
the same object referenced by c2. The
object previously referenced by c1 is no
longer referenced. This object is known as
garbage. Garbage is automatically
collected by JVM.
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
28
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
29
java.util.Date
+Date()
+Date(elapseTime: long)
+toString(): String
+getTime(): long
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
30
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
31
+Random(seed: long)
+nextInt(): int
+nextLong(): long
+nextDouble(): double
+nextFloat(): float
+nextBoolean(): boolean
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
32
From random1: 734 660 210 581 128 202 549 564 459 961
From random2: 734 660 210 581 128 202 549 564 459 961
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
33
Instance
Variables, and Methods
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
34
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
35
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
36
circle1
radius = 1
numberOfObjects = 2
Circle
radius: double
numberOfObjects: int
getNumberOfObjects(): int
+getArea(): double
UML Notation:
underline: static variables or methods
instantiate
Memory
1
radius
numberOfObjects
radius
circle2
radius = 5
numberOfObjects = 2
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
37
Example of
Using Instance and Class Variables
and Method
Objective: Demonstrate the roles of
instance and class variables and their
uses. This example adds a class variable
numberOfObjects to track the number of
Circle objects created.
Circle2
TestCircle2
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
38
public
The class, data, or method is visible to any class in any
package.
private
39
package p1;
package p2;
public class C1 {
public int x;
int y;
private int z;
public class C2 {
void aMethod() {
C1 o = new C1();
can access o.x;
can access o.y;
cannot access o.z;
package p1;
class C1 {
...
}
public class C3 {
void aMethod() {
C1 o = new C1();
can access o.x;
cannot access o.y;
cannot access o.z;
can invoke o.m1();
cannot invoke o.m2();
cannot invoke o.m3();
}
package p2;
public class C2 {
can access C1
}
public class C3 {
cannot access C1;
can access C2;
}
40
NOTE
An object cannot access its private members, as shown in (b).
It is OK, however, if the object is declared in its own class, as
shown in (a).
public class Foo {
private boolean x;
public static void main(String[] args) {
Foo foo = new Foo();
System.out.println(foo.x);
System.out.println(foo.convert());
}
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
41
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
42
Example of
Data Field Encapsulation
The - sign indicates
private modifier
Circle
-radius: double
-numberOfObjects: int
+Circle()
+Circle(radius: double)
+getRadius(): double
+getNumberOfObject(): int
+getArea(): double
Circle3
TestCircle3
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
43
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
44
Example
public class Student {
private int id;
private BirthDate birthDate;
45
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
46
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
47
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
Heap
A circle
object
48
Scope of Variables
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
49
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
50
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
51
public Circle() {
this(1.0);
}
52
Array of Objects
Circle[] circleArray = new Circle[10];
53
circleArray
reference
circleArray[0]
circleArray[1]
Circle object 0
Circle object 1
circleArray[9]
Circle object 9
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
54
TotalArea
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
55
Class
Class Contract
(Signatures of
public methods and
public constants)
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rig
hts reserved. 0-13-222158-6
56