Chapter 5

advertisement
Chapter 5
5.1.2 A Student example
public class Student {
public String name; // Student’s name.
public double test1, test2, test3; //Grades on 3 tests.
public double getAverage() { // compute avg. test grade
return (test1 + test2 + test3) / 3;
}
} // end of class Student
Properties and Methods of Student
• Properties
•
•
•
•
name – String
test 1 – double
test 2 - double
test 2 – double
• Methods
• getAverage() (a method to get average test grade.)
Using the Student Class
public class StudentDemo {
public static void main(String[] args)
Student george = new Student();
Student fred = new Student();
george.test1 = 98.6; //’reaches in’
george.test2 = 88.9;
george.test3 = 94.0;
System.out.println("George's avg.: "
System.out.println("Fred's avg.: " +
}
}
{
and set a score
+ george.getAverage());
fred.getAverage());
What is the variable “fred”?
• In Java, no variable can ever hold an object.
• A variable can only hold a reference to an object.
• It is possible for a variable like fred, whose type is given by a class, to
refer to no object at all. We say in this case that fred holds a null
pointer or null reference.
Another Example
Student std, std1,
std2, std3;
std = new Student();
// Declare four variables of
// type Student.
// Create a new object belonging
// to the class Student, and
// store a reference to that
// object in the variable std.
std1 = new Student(); // Create a second Student object
// and store a reference to
// it in the variable std1.
std2 = std1;
// Copy the reference value in std1
// into the variable std2.
std3 = null;
// Store a null reference in the
// variable std3.
std.name = "John Smith"; // Set values of some instance variables.
std1.name = "Mary Jones";
// (Other instance variables have default
// initial values of zero.)
Copy objects? Compare Objects?
• When one object variable is assigned to another, only a reference is
copied.
• The object referred to is not copied.
• The expression greeting == "Hello" tests whether
greeting and "Hello" contain the same characters stored in the
same memory location.
• Bottom line: operations like copying and comparing usually require
using special methods written to what you really want to do.
Passing to methods
void dontChange(int z) {
z = 42;
}
void change(Student s) {
s.name = "Fred";
}
x = 17;
dontChange(x);
System.out.println(x);
stu.name = "Jane";
change(stu);
System.out.println(stu.name);
//prints 17
//prints Fred
5.1.3 Getters and Setters
• Access Control: who gets to see and change variables?
• Modern programming practice discourages allowing variables to be
“seen” outside a class.
• Statements like: std.name = "John Smith"; are
discouraged.
• It is considered better to “route” through getters and setters:
methods whose job it is to control access to variables.
• This keeps the method a “black box”
Getters or accessors
• The name usually starts with “get” (or “is” if the variable is a
boolean).
• The argument list is usually empty: getName()
• A getter’s type is the type of the variable.
• A getter can do more than just return the value, if you want. This is
up to the programmer.
• A getter can hide whether a value is just returned from a stored copy,
or calculated “on the fly”.
Setters or mutators
• These are usually of type void: their job is to do something, not return
something.
• There is usually one argument, of the type of the variable in question.
• Once again, the method can do more than just set the value (It might
want to track how often a variable is being changed, for example.)
• Example: setName("George Washington");
• There doesn’t even have to be a variable of that name, although
there usually is. That’s internal business of the method.
5.2.2 Constructors
• This is a special type of method (or is it really a method?):
•
•
•
•
Unlike all other methods, it does not have a type.
They are neither instance methods nor static methods.
The name of the constructor method is identical to the name of the class.
It is invoked with the operator new.
• There is a default, no-arg constructor that “goes away” as soon as you
write any constructor.
• Constructors are frequently overloaded.
5.2.3 Garbage Collection
Student std = new Student("John Smith");
std = null;
Java uses a procedure called garbage collection to reclaim memory
occupied by objects that are no longer accessible to a program. It is the
responsibility of the system, not the programmer, to keep track of
which objects are “garbage.” In the above example, it was very easy to
see that the Student object had become garbage. Usually, it’s much
harder. If an object has been used for a while, there might be several
references to the object stored in several variables. The object doesn’t
become garbage until all those references have been dropped.
5.3.1 Some Built-in Classes
• String
• StringBuilder with .append() and .toString() methods
• java.util.Date
• java.util.Random with .nextInt()
• java.awt.Color
• Strings and Colors are immutable.
5.3.2 The class “Object”
• Subclass and inheritance: Classes can be made from other classes. In
fact, every class is a subclass of Object.
• several instance methods that are inherited by every other class
• toString() The built-in toString() usually isn’t very useful, just a name and a
hash code. You usually write your own to replace the inherited version.
• equals() compares to another object, but usually will be replaced.
• clone() makes a copy
• getClass() gets the class
• finalize(), notify(), notifyAll(), wait() these are a bit technical and beyond this
course.
5.5 Inheritance, Polymorphism, and Abstract Classes
Download