Java Reference
In-Depth Information
thebytecodeequivalentof int counter = 1; ,anditwouldfinallycontainthebyte-
code equivalent of System.out.println("instance initializer 2");
System.out.println("counter = "+counter); .
When new C() executes, <init>() executesimmediatelyandgeneratesthefol-
lowing output:
instance initializer 1
instance initializer 2
counter = 1
Note Youshouldrarelyneedtousetheinstanceinitializer,whichisnotcommonly
used in industry.
Initialization Order
to instance field initializers, although I am guilty of not doing so consistently, and re-
strict your use of instance initializers to anonymous classes.) Furthermore, class fields
and instance fields initialize to default values. Understanding the order in which all of
thisinitializationoccursisnecessarytopreventingconfusion,socheckout Listing2-20 .
Listing 2-20. A complete initialization demo
class InitDemo
static double double1;
double double2;
static int int1;
int int2;
static String string1;
String string2;
System.out.println("[class] double1 = "+double1);
System.out.println("[class] int1 = "+int1);
System.out.println("[class] string1 = "+string1);
Search WWH ::

Custom Search