You are on page 1of 1

Initialization blocks When a block without name appears outside all methods, but inside the class, it is called

an initialization block. Instance variable Initialization blocks When a block without name appears outside all methods, but inside the class, it is called an initialization block. Initialization blocks can be used for initial ization code similar to constructors and are executed every time an object is cr eated similar to constructors. All init blocks will be executed in the order the y appear, but before the constructor. Example for a normal initialization block: class MyClass { /*some code*/ { /*some code*/ } /*some code*/ } static initialization blocks There can be also static initialization blocks, which has a static keyword befor e the block. Static Initialization blocks run only once when the class is first loaded. All static init blocks will be executed in the order they appear. Example for a static initialization block: class MyClass { /*some code*/ static { /*some code*/ } /*some code*/ }

You might also like