In a class, members can be initialized when they are declared, by assigning a
value to it. It looks like this:
class Dog {
String name="PP";
....
}
Here, the member "name" is initialized with "PP" at the place it is declared.
Moreover, you can assign a return value of a function to the variable. The
function can take no arguments, or several arguments, as long as the arguments is
initialized when the assignment occurs.
For example, you can write this,
class A {
int i = 20;
int g = f(i);
int f(int i) {
...
}
}
But, you will encounter compile error when it comes to like that:
class A {
int g = f(i);
int i;
int f(int i) {
...
}
}
Please note that, for the first example, if there is no assignment to i when it is
declared before the declaration of g, that is:
int i;
int g=f(i);
....
There is no compile error. In the case of no assignment, the variable is set
to the default value. For integer, the default value is 0, while for boolean
is false.
It is about the primitive above. As to the object, it is similar to the
primitive, with the default value "null".
Besides, variable can be initiated in the constructors, and a special block
closed by curly. So you can make initiation like this:
class Cat {
public Cat() {
name = "cat";
}
{
sex = "male";
}
String name;
String sex;
}
At some time, you may add the modifier "static" to a variable. In this case,
you can refer to the above as to its initialization. It can be initialized when
it is declare and in the special block closed by curly block following a modifier
"static". But it cannot be initialized in the constructor. Also, you must keep it
in mind that such variables is initialized only once.
There are several ways to initialize a variable. And what's the order of
initialization? I'll summarize first and then demonstrate it by example.
1. The static fields are initialized if they have not been initialized before.
They are initialized at the order of their definition.
2. The non-static fields are then initialized. They are initialized at the order
of their definition.
3. The statements is the curly-close-blocks are executed.
4. The constructor is executed to do its initialization.
Let's examine an example.
Class Ini {
public static void main(String[] args) {
Ini first = new Ini();
Ini second = new Ini();
}
int a1 = 2;
static int sa1 = 3;
public Ini() {
System.out.println("constructor is called. g=" + g);
}
int a2;
int g = f(a1);
static int sa2;
// the initialization in the curly-closed-block
{
a2 = 3;
System.out.println("Initialization in curly-closed-block. a1="
+ a1);
}
// for statics
static {
sa2 = 3;
System.out.println("Initialization in curly-closed-block. sa1="
+ sa1);
}
}
When this example is compile and run, we can get the output like following.
Initialization in curly-closed-block. sa1=3
Initialization in curly-closed-block. a1=2
constructor is called. g=3
Initialization in curly-closed-block. a1=2
constructor is called. g=3
Please note that the field sa1 only initialized once, when the first object of
Ini is defined. In the object "second", all the static fields do not bother to
be initialized again. From the output, we also can point out that the
execution of the curly-closed-block is after the execution of the definition
of all fields before it. There can't be any variables that comes after is.
To summarize, all the fields in a class are initialized as they are defined.
When there is only the declaration without the assignment, the field is set to
a default value. Then the curly-closed-block is executed before the constructor.
Also, the static fields will be initialized when the object is defined or the
fields are access without an object. The initialization of static field comes
before the non-static fields.