Professional Documents
Culture Documents
Class Contrcutor Practice
Class Contrcutor Practice
INITIALIZING FIELDS
There are three ways in Java to give a field an initial value:
Direct Assignment
Instance Initialization Block
Constructors
1.DIRECT ASSIGNMENT
}
2.INSTANCE INITIALIZATION BLOCK
Instance initialization blocks are indicated by blocks of code inside the class, but outside any
method.
Whenever an object is created from a class the code in each instance initialization block is
executed.
If there is more than one instance initialization block they are executed in order, from top to
bottom of the class.
Use initialization blocks when the initialization cannot be done in a simple assignment and
needs no extra input parameters.
Direct assignment and constructors are used far more often than initialization blocks.
2.INSTANCE INITIALIZATION BLOCK