Professional Documents
Culture Documents
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
1
Reference Data Fields
The data fields can be of reference types. For example,
the following Student class contains a data field name of
the String type.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
2
The null Value
If a data field of a reference type does not
reference any object, the data field holds a
special literal value, null.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
3
Default Value for a Data Field
The default value of a data field is null for a
reference type, 0 for a numeric type, false for a
boolean type, and '\u0000' for a char type.
However, Java assigns no default value to a local
variable inside a method.
public class Test {
public static void main(String[] args) {
Student student = new Student();
System.out.println("name? " + student.name);
System.out.println("age? " + student.age);
System.out.println("isScienceMajor? " + student.isScienceMajor);
System.out.println("gender? " + student.gender);
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
4
Example
Java assigns no default value to a local variable inside a method.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
6
Static Variables, Constants,
and Methods
Static variables are shared by all the instances of the
class.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
7
Static Variables, Constants,
and Methods, cont.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
8
Static Variables, Constants,
and Methods, cont.
instantiate
circle1 Memory
radius = 5 5 radius
UML Notation: numberOfObjects = 2
+: public variables or methods
underline: static variables or methods
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
9
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.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
10
public class CircleStaticExample {
Example
double radius;
static int numberOfObjects = 0;
CircleStaticExample() {
radius = 1.0;
numberOfObjects++;
}
CircleStaticExample(double newRadius) {
radius = newRadius;
numberOfObjects++;
}
static int getNumberOfObjects() {
return numberOfObjects;
}
double getArea() {
return radius * radius * Math.PI;
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
11
TestCricleStaticExample
public class TestCricleStaticExample {
public static void main(String args[]){
CircleStaticExample c1=new CircleStaticExample();
System.out.println (CircleStaticExample.getNumberOfObjects());
System.out.println(c1.getNumberOfObjects());
CircleStaticExample c2=new CircleStaticExample();
CircleStaticExample c3=new CircleStaticExample(5);
CircleStaticExample c4=new CircleStaticExample();
System.out.println(c1.getNumberOfObjects());
System.out.println (CircleStaticExample.getNumberOfObjects());
}
}
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
12
Visibility Modifiers and
Accessor/Mutator Methods
By default, the class, variable, or method can be
accessed by any class in the same package.
public
The class, data, or method is visible to any class in any package.
private
The data or methods can be accessed only by the declaring class.
The get and set methods are used to read and modify private
properties. getter methods are also called as accessor methods
where as setter methods are called as mutator.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
13
package p1; package p2;
public class C1 { public class C2 { public class C3 {
public int x; void aMethod() { void aMethod() {
int y; C1 o = new C1(); C1 o = new C1();
private int z; can access o.x; can access o.x;
can access o.y; cannot access o.y;
public void m1() { cannot access o.z; cannot access o.z;
}
void m2() { can invoke o.m1(); can invoke o.m1();
} can invoke o.m2(); cannot invoke o.m2();
private void m3() { cannot invoke o.m3(); cannot invoke o.m3();
} } }
} } }
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
15
Why Data Fields Should Be
private?
To protect data.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
16
Example of
Data Field Encapsulation
Circle
The - sign indicates
private modifier -radius: double The radius of this circle (default: 1.0).
-numberOfObjects: int The number of circle objects created.
Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All
rights reserved. 0132130807
17