thinking in Java 笔记 5

来源:互联网 发布:音序器软件 编辑:程序博客网 时间:2024/05/16 04:33
Non-static instance initialization
Java provides a similar syntax, called instance initialization, for initializing non-static
variables for each object. Here’s an example:
//: initialization/Mugs.java
// Java "Instance Initialization."
import static net.mindview.util.Print.*;
class Mug {
Mug(int marker) {
print("Mug(" + marker + ")");
}
void f(int marker) {
print("f(" + marker + ")");
}
}
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
Mugs() {
print("Mugs()");
}
Mugs(int i) {
print("Mugs(int)");
}
public static void main(String[] args) {
print("Inside main()");
new Mugs();
print("new Mugs() completed");
new Mugs(1);
print("new Mugs(1) completed");
}
} /* Output:
Inside main()
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs()
new Mugs() completed
Mug(1)
Mug(2)
mug1 & mug2 initialized
Mugs(int)
new Mugs(1) completed
*///:~
You can see that the instance initialization clause:
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
looks exactly like the static initialization clause except for the missing static keyword. This
syntax is necessary to support the initialization of anonymous inner classes (see the Inner
Classes chapter), but it also allows you to guarantee that certain operations occur regardless
of which explicit constructor is called. From the output, you can see that the instance

initialization clause is executed before either one of the constructors.


p.166

Composition syntax
Composition has been used quite frequently up to this point in the book. You simply place
object references inside new classes. For example, suppose you’d like an object that holds
several String objects, a couple of primitives, and an object of another class. For the non-
primitive objects, you put references inside your new class, but you define the primitives
directly:
//: reusing/SprinklerSystem.java
// Composition for code reuse.
class WaterSource {
private String s;
WaterSource() {
System.out.println("WaterSource()");
s = "Constructed";
}
public String toString() { return s; }

public class SprinklerSystem {
private String valve1, valve2, valve3, valve4;
private WaterSource source = new WaterSource();
private int i;
private float f;
public String toString() {
return
"valve1 = " + valve1 + " " +
"valve2 = " + valve2 + " " +
"valve3 = " + valve3 + " " +
"valve4 = " + valve4 + "\n" +
"i = " + i + " " + "f = " + f + " " +
"source = " + source;
}
public static void main(String[] args) {
SprinklerSystem sprinklers = new SprinklerSystem();
System.out.println(sprinklers);
}
} /* Output:
WaterSource()
valve1 = null valve2 = null valve3 = null valve4 = null
i = 0 f = 0.0 source = Constructed
*///:

One of the methods defined in both classes is special: toString( ). Every non-primitive
object has a toString( ) method, and it’s called in special situations when the compiler
wants a String but it has an object. So in the expression in SprinklerSystem.toString( ):
"source = " + source;
the compiler sees you trying to add a String object ("source = ") to a WaterSource.
Because you can only “add” a String to another String, it says “I’ll turn source into a
String by calling toString( )!” After doing this it can combine the two Strings and pass the
resulting String to System.out.println( ) (or equivalently, this book’s print() and
printnb( ) static methods). Any time you want to allow this behavior with a class you
create, you need only write a toString( ) method.
Primitives that are fields in a class are automatically initialized to zero, as noted in the
Everything Is an Object chapter. But the object references are initialized to null, and if you
try to call methods for any of them, you’ll get an exception-a runtime error. Conveniently,
you can still print a null reference without throwing an exception.
It makes sense that the compiler doesn’t just create a default object for every reference,
because that would incur unnecessary overhead in many cases. If you want the references
initialized, you can do it:
1.  At the point the objects are defined. This means that they’ll always be initialized
before the constructor is called.
2.  In the constructor for that class.
3.  Right before you actually need to use the object. This is often called lazy
initialization. It can reduce overhead in situations where object creation is expensive
and the object doesn’t need to be created every time.
4.  Using instance initialization.
All four approaches are shown here:
//: reusing/Bath.java
// Constructor initialization with composition.
import static net.mindview.util.Print.*;
class Soap {
private String s;
Soap() {
print("Soap()");
s = "Constructed";
}
public String toString() { return s; }

public class Bath {
private String // Initializing at point of definition:
s1 = "Happy",
s2 = "Happy",
s3, s4;
private Soap castille;
private int i;
private float toy;
public Bath() {
print("Inside Bath()");
s3 = "Joy";
toy = 3.14f;
castille = new Soap();
}
// Instance initialization:
{ i = 47; }
public String toString() {
if(s4 == null) // Delayed initialization:
s4 = "Joy";
return
"s1 = " + s1 + "\n" +
"s2 = " + s2 + "\n" +
"s3 = " + s3 + "\n" +
"s4 = " + s4 + "\n" +
"i = " + i + "\n" +
"toy = " + toy + "\n" +
"castille = " + castille;
}
public static void main(String[] args) {
Bath b = new Bath();
print(b);
}
} /* Output:
Inside Bath()
Soap()
s1 = Happy
s2 = Happy
s3 = Joy
s4 = Joy
i = 47
toy = 3.14
castille = Constructed
*///:~
Note that in the Bath constructor, a statement is executed before any of the initializations
take place. When you don’t initialize at the point of definition, there’s still no guarantee that
you’ll perform any initialization before you send a message to an object reference—except for
the inevitable run-time exception.
When toString( ) is called it fills in s4 so that all the fields are properly initialized by the
time they are used.

0 0