Initialize variable
Initialize Member Variables(Class variable, Instance variable)
Initializing order
cv
iv
automatic : 0
=
{} : class variable(static) => static {...}, instance variable => constructor
Initialize local variable before use!(ESSENTIAL)
member variable(instance variable, class(static) variable)) is initialized automatically.
Initialize with =
class Car {
int door = 4;
//Engine e;
Engine e = new Engine();//create object and put into e
2. initialize with {..}
initialize instance : {...}
initialize cv(classs variable(static)) : static {...}
class StaticBlockTest {
static int[] arr = new int[10];
//initialize static variable(class variable)
static {
for(int i = 0; i < arr.length;i++) {
arr[i] = (int)(Math.random()*10)+1;
}
}
}
3. initialize (instance) with constructor
class Car {
Car(String color, String gearType, int door) {
this.color = color;
this.gearType = gearType;
this.door = door;
}
...
}
Last updated
Was this helpful?