|
|
|
Sponsored Link •
|
|
Advertisement
|
Instance initialization methods
When you compile a class, the Java compiler creates an instance
initialization method for each constructor you declare in the
source code of the class. Although the constructor is not a method, the
instance initialization method is. It has a name, <init>, a return type,
void, and a set of parameters that match the parameters of
the constructor from which it was generated. For example, given the following
two constructors in the source file for class CoffeeCup:
// In source packet in file init/ex8/CoffeeCup.java
class CoffeeCup {
public CoffeeCup() {
//...
}
public CoffeeCup(int amount) {
//...
}
// ...
}
the compiler would generate the following two instance initialization
methods in the class file for class CoffeeCup, one for
each constructor in the source file:
// In binary form in file init/ex8/CoffeeCup.class: public void(CoffeeCup this) {...} public void (CoffeeCup this, int amount) {...}
Note that <init> is not a valid
Java method name, so you could not define a method in your source file
that accidentally conflicted with an instance initialization method.
(<init> is not a method in the Java
language sense of the term, because it has an illegal name. In the
compiled, binary Java class file, however, it is a legal method.)
Also, the this reference passed as the first parameter to
<init> is inserted by the Java compiler into the
parameter list of every instance method. For example, the method
void add(int amount) in the source file for
class CoffeeCup would become the void add(CoffeeCup
this, int amount) method in the class file. The hidden
this reference is the way in which instance methods,
including instance initialization methods, are able to access instance
data.
If you don't explicitly declare a constructor in a class, the Java compiler will create a default constructor on the fly, then translate that default constructor into a corresponding instance initialization method. Thus, every class will have at least one instance initialization method.
When the compiler generates an instance initialization method, it bases
it on a constructor. It gives the method the same parameter list as the
constructor, and it puts the code contained in the constructor's body
into the method's body. But the instance initialization method does not
necessarily represent a mere compilation of the constructor with the
name changed to <init> and a return value of
void added. Often, the code of an instance initialization
method does more than the code defined in the body of its corresponding
constructor. The compiler also potentially adds code for any
initializers and an invocation of the superclass's constructor.
The <init> method is not actually part of the Java
language. Rather, it is something the Java virtual machine expects to
see in a Java class file. This distinction is significant because the
Java language does not depend on the class file. Java source can be
compiled into other binary formats, including native executables. A
Java compiler that translates Java language source into some other
binary format need not generate a method named
<init>, so long as objects are initialized in the
proper way at the proper time. The Java Language Specification (JLS) details
the order of initialization and when it occurs, but doesn't say how it
is actually accomplished. (See the Resources
section for information on the Java Language Specification.) Still,
understanding how initialization works inside class files can help you
understand the order of initialization in the language.
|
Sponsored Links
|