The order of initialization is statics first, if they haven’t already been initialized by a previous object creation, and then the non-static objects...

Linki


» Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.
»
Creating stubs and skeletons If you compile and run PerfectTime...
»
mad? Run her to lunacy? Please say I haven’t...
»
But Che was already reconsidering...
»
order to fall in with):--meeting...
»
33: */34: public void initialize(Subject subject, CallbackHandler callbackHandler, 35: Map sharedState, Map options)36: {37: super...
»
„SAMB”, Computer system of static analysis of shear wall structures in tall buildings „SAMB” - Computer system of static analysis of...
»
dojdziemy od razu do czwartego i ostatniego objawu, którynależałoby nam omówić w związku z patologią ducha wieku,mianowicie do fanatyzmu...
»
- Generał?- Szef Wydziału Bezpieczeństwa, generał Balchen...
»
robotnych
»
XZD*DQ ]D F]\QQRß SRGNRURZ :LHOH ZVND]XMH QD WR *H QDWXUD EXGRZDåD DSDUDW UDFMRQDOQRFL QLH SRQDG DSDUDWHP ELRORJLF]QHM UHJXODFML OHF] UD]HP ] QLP´ >...

Dzieci to nie książeczki do kolorowania. Nie da się wypełnić ich naszymi ulubionymi kolorami.

You can see the evidence of this in the output.
It’s helpful to summarize the process of creating an object. Consider a class called Dog: 1. The first time an object of type Dog is created, or the first time a static method or static field of class Dog is accessed, the Java interpreter must locate Dog.class, which it does by searching through the classpath.
2. As Dog.class is loaded (which creates a Class object, which you’ll learn about later), all of its static initializers are run. Thus, static initialization takes place only once, as the Class object is loaded for the first time.
3. When you create a new Dog( ), the construction process for a Dog object first allocates enough storage for a Dog object on the heap.
4. This storage is wiped to zero, automatically setting all the primitives in Dog to their default values (zero for numbers and the equivalent for boolean and char).
5. Any initializations that occur at the point of field definition are executed.
6. Constructors are executed. As you shall see in Chapter 6, this might actually involve a fair amount of activity, especially when inheritance is involved.
Explicit static initialization
Java allows you to group other static initializations inside a special “static construction clause” (sometimes called a static block) in a class. It looks like this: class Spoon {
static int i;
static {
i = 47;
}
// . . .
Chapter 4: Initialization & Cleanup
155
So it looks like a method, but it’s just the static keyword followed by a method body. This code, like the other static initialization, is executed only once, the first time you make an object of that class or you access a static member of that class (even if you never make an object of that class). For example:
//: ExplicitStatic.java
// Explicit static initialization
// with the "static" clause.
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Cups {
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.c1.f(99); // (1)
}
static Cups x = new Cups(); // (2)
static Cups y = new Cups(); // (2)
} ///:~
The static initializers for Cups will be run when either the access of the static object c1
occurs on the line marked (1), or if line (1) is commented out and the lines marked (2) are uncommented. If both (1) and (2) are commented out, the static initialization for Cups never occurs.
Non-static instance initialization
Java 1.1 provides a similar syntax for initializing non-static variables for each object. Here’s an example:
//: Mugs.java
// Java 1.1 "Instance Initialization"
class Mug {
Mug(int marker) {
156
Thinking in Java
www.BruceEckel.com
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
public class Mugs {
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
}
} ///:~
You can see that the instance initialization clause:
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 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 Chapter 7).
Array initialization
Initializing arrays in C is error-prone and tedious. C++ uses aggregate initialization to make it much safer.6 Java has no “aggregates” like C++, since everything is an object in Java. It does have arrays, and these are supported with array initialization.
An array is simply a sequence of either objects or primitives, all the same type and packaged together under one identifier name. Arrays are defined and used with the square-brackets indexing operator [ ]. To define an array you simply follow your type name with empty square brackets:
int[] a1;
You can also put the square brackets after the identifier to produce exactly the same meaning:

6 See Thinking in C++ for a complete description of aggregate initialization.
Chapter 4: Initialization & Cleanup
157
int a1[];
This conforms to expectations from C and C++ programmers. The former style, however, is probably a more sensible syntax, since it says that the type is “an int array.” That style will be used in this book.

Powered by MyScript