0% found this document useful (0 votes)
10 views

9.core Java Blocks

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

9.core Java Blocks

Uploaded by

ms.madhu27
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Leela Soft Core Java (J2SE) Madhusudhan

Static Blocks or Static Initialization Blocks


A static initialization block is a normal block of code enclosed in braces, { }, and preceded by the
static keyword. Here is an example:

static {
// whatever code is needed for initialization goes here
}

A class can have any number of static initialization blocks, and they can appear anywhere in
the class body. The runtime system guarantees that static initialization blocks are called in the
order that they appear in the source code.

Example:
class Test {
static { // static block
System.out.println("Static Block 4");
}

static { // static block


System.out.println("Static Block 2");
}

public static void main(String[] args) {


System.out.println("Main Method");
}

static { // static block


System.out.println("Static Block 1");
}

static { // static block


System.out.println("Static Block 3");
}
}

Example:
class Test {
static { // static block
System.out.println("Static Block 1");
}

{ // instance block
System.out.println("Instance Block 1");
}

Test() {
System.out.println("0-arg Const");

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t1 = new Test();
Test t2 = new Test();
}

static { // static block


System.out.println("Static Block 2");
}
}

Example:
class Test {
static int x;
static { // static block
x = 10;
Test.m1();
System.out.println("Static Block 1");
}

static void m1() {


System.out.println("m1 method :"+x);
}

public static void main(String[] args) {


System.out.println("Main Method");
}
}

There is an alternative to static blocks — we can write a private static method:

class Whatever {
public static varType myVar = initializeClassVariable();

private static varType initializeClassVariable(){


// initialization code goes here
}
}

The advantage of private static methods is that they can be reused later if we need to reinitialize
the class variable.

Instance Block or initializer blocks:

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
Normally, we would put code to initialize an instance variable in a constructor. There are two
alternatives to using a constructor to initialize instance variables: initializer blocks and final
methods.

Initializer blocks for instance variables look just like static initializer blocks, but without the
static keyword:

{
// whatever code is needed for initialization goes here
}

The runtime system guarantees that instance initialization blocks are called in the order that they
appear in the source code.

The Java compiler copies initializer blocks into every constructor. Therefore, this approach can
be used to share a block of code between multiple constructors.

Example:
class Test {
int x;

{ // instance block
x = 10;
System.out.println("Instance Block");
}

Test() {
System.out.println("0-arg Const :" + x);
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
}
}

Example:
class Test {
{ // instance block
System.out.println("Instance Block 1");
}

{ // instance block
System.out.println("Instance Block 2");
}

Test() {

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
System.out.println("0-arg Const");
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t = new Test();
}
}

Example:
class Test {
{ // instance block
System.out.println("Instance Block 1");
}

{ // instance block
System.out.println("Instance Block 2");
}

Test() {
System.out.println("0-arg Const");
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t1 = new Test();
Test t2 = new Test();
}
}

Example:
class Test {
{ // instance block
System.out.println("Instance Block 1");
}

Test() {
System.out.println("0-arg Const");
}

Test(int x) {
System.out.println("1-arg Const");
}

public static void main(String[] args) {


System.out.println("Main Method");
Test t1 = new Test();
Test t2 = new Test(10);

[email protected] Cell: 78 42 66 47 66
Leela Soft Core Java (J2SE) Madhusudhan
}
}

[email protected] Cell: 78 42 66 47 66

You might also like