|
Member initialization
Java try to ensure that: all variables before use can be properly initialized.
Local variables: the uninitialized compilation errors.
void f () {
int i;
// System.out.println (i); // compile error
}
Data members of the class (fields) are basic types: to ensure there will be an initial value.
public class InitialValues {
boolean bl;
char c;
byte bt;
short s;
int i;
long l;
float f;
double d;
InitialValues reference;
void printValues () {
System.out.printf (
"\ Nboolean:" + bl
+ "\ Nchar:" + c
+ "\ Nbyte:" + bt
+ "\ Nshort:" + s
+ "\ Nint:" + i
+ "\ Nlong:" + l
+ "\ Nfloat:" + f
+ "\ Ndouble:" + d
+ "\ Nreference:" + reference
);
}
public static void main (String [] args) {
new InitialValues () printValues ().;
}
}
Results output:
boolean: false
char:
byte: 0
int: 0
long: 0
float: 0.0
double: 0.0
reference: null
Specifies initialization
In the definition of the class member variables place for variable assignment
public class InitialValues {
boolean bl = true;
char c = 'a';
byte bt = 22;
short s = 0xff;
int i = 1202;
long l = 1;
float f = 3.14f;
double d = 3.14159;
}
Initialize the object in the same way non-basic types
class Apple {}
public class InitialValues {
Apple apple = new Apple ();
}
Call the method to the initial value
int i1 = getIntValue ();
int i2 = getDoubleValue (2);
private int getDoubleValue (int i) {
return i * 2;
}
private int getIntValue () {
return 2;
}
Constructor to initialize
Were unable to prevent automatic initialization, it will occur before the constructor is called.
public class ConstructorInitial {
int i;
ConstructorInitial () {
System.out.println (i);
i = 1;
System.out.println (i);
}
public static void main (String [] args) {
new ConstructorInitial ();
}
}
result:
0
1
The initialization sequence
Within the class, order of variable definition determines the order of initialization.
Initialize static data
No matter how many objects are created, static data occupies only a storage area.
Static initialization only when necessary will be carried out.
Initialization sequence is: Static objects -> non-static objects.
The process of object creation
Suppose there is a class called Dog:
When you first create Dog object (constructor can be viewed as a static method) or the first visit to a static method or static field Dog class, Java interpreter must locate the class path to locate Dog.class file.
Then load Dog.class (This will create a Class object), all actions are performed on static initialization. Thus, static initialization only when the Class object is first loaded once.
When you create an object with new Dog (), the first allocated on the heap enough storage space for the Dog object.
This storage will be cleared, which automatically Dog object of all the basic types of data are set to default values (the number is 0, boolean type, similar to the character), and the reference is set to null.
Perform all appeared in the fields defined at initialization action.
Executive constructor.
Static initialization display
Java allows multiple static initialization action organized into a special "static block"
class Cup {
Cup (int marker) {
System.out.println ( "Cup (" + marker + ")");
}
void f (int marker) {
System.out.println ( "f (" + marker + ")");
}
}
class Cups {
// Static member variables
static Cup cup1;
static Cup cup2;
// Explicit static initialization
// Static block
static {
System.out.println ( "static initialization");
cup1 = new Cup (1);
cup2 = new Cup (2);
}
Cups () {
System.out.println ( "Cups ()");
}
}
public class Initialization {
public static void main (String [] args) {
Cups.cup1.f (100);
/ **
* Static initialization
Cup (1)
Cup (2)
f (100)
* /
}
// Static Cups cups = new Cups ();
/ **
* Static initialization
Cup (1)
Cup (2)
Cups ()
* /
}
Non-static instance initialization
Initializes an instance: non-static variables are initialized for each object.
class Cup {
Cup (int marker) {
System.out.println ( "Cup (" + marker + ")");
}
void f (int marker) {
System.out.println ( "f (" + marker + ")");
}
}
class Cups {
// Static member variables
static Cup cup1;
static Cup cup2;
Cup cup3, cup4;
// Explicit static initialization
// Static block
static {
System.out.println ( "static initialization");
cup1 = new Cup (1);
cup2 = new Cup (2);
}
// Non-static instance initialization
{
System.out.println ( "non-static initialization");
cup3 = new Cup (3);
cup4 = new Cup (4);
}
Cups () {
System.out.println ( "Cups ()");
}
}
public class Initialization {
public static void main (String [] args) {
new Cups ();
/ **
* Static initialization
Cup (1)
Cup (2)
non-static initialization
Cup (3)
Cup (4)
Cups ()
* /
}
} |
|
|
|