Java Homework Help | Help with Java Programming | Programming Homework Help | Java Programming Assignment Help | Help with Java Programming Assignment | Java Tutor | Programming Help | Java Online Help | Java Programming Online | Java Programming Projects

Initializer blocks is very important in Java Online Help . In any function or class block its essential to categorize the special functions and concepts in class. In java programming students need to learn block concpet to make good and robust code for real time problems. To help students and easily understand the concept below is example with description:-
Initializer blocks are used to initialize instance variables.

Syntax of Initializer Blocks
{
//initialization Blocks code goes here
}

Java compiler copies initializer blocks into every constructor.

Example
class Box{
int width,height;

{
width = 10;
height = 15;
}

public static void main(String args[]){
Box b1 = new Box();
System.out.println(b1.width +"\t"+b1.height);
}
}

Output
10 15

Some Points to Remember
1. A class can have more than one initializer block
class Box{
int width,height;
String name;

{
width = 10;
height = 15;
}

{
name = "My Box";
}

public static void main(String args[]){
Box b1 = new Box();
System.out.println(b1.width +"\t"+b1.height);
System.out.println(b1.name);
}
}

Output
10 15
My Box

2. The data in initializer blocks copied to every constructor
Example
class Box{
int width,height;
String name;

{
width = 10;
height = 15;
}

{
name = "My Box";
}

Box(){

}

Box(int w){
width = w;
}

public static void main(String args[]){
Box b1 = new Box();
Box b2 = new Box(100);

System.out.println(b1.width +"\t"+b1.height);
System.out.println(b1.name);
System.out.println(b2.width +"\t"+b2.height);
System.out.println(b2.name);
}
}

Output
10 15
My Box
100 15
My Box

3. Initializer block are called every time, when you created the object
Example
class Person{
{
System.out.println("I am first");
}

{
System.out.println("I am second");
}

public static void main(String args[]){
Person p1 = new Person();
Person p2 = new Person();
}
}

Output
I am first
I am second
I am first
I am second

4. Initializing static fields in the initialization block is valid, but don't use it, since the static variable is initialized every time when a new object created.
Example
class Person{
static int minSalary;

{
minSalary = 10000;
}

public static void main(String args[]){
Person p1 = new Person();
Person.minSalary = 50000;
System.out.println(Person.minSalary);

Person p2 = new Person();
System.out.println(Person.minSalary);
}
}

Output
50000
10000

 

Static Blocks
static blocks are used to initialize static variables. Static blocks are executed when the class gets loaded by a class loader.
Syntax
static{

}

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 Person{
static int minSalary;

static{
minSalary = 10000;
}

public static void main(String args[]){
System.out.println(Person.minSalary);
}
}

Output
10000

Some points to remember
1. A class can have any number of static blocks
Example
class Person{
static int minSalary;
static int maxSalary;
static int minAge;

static{
minSalary = 10000;
maxSalary = 100000;
}

static{
minAge = 21;
}

public static void main(String args[]){
System.out.println(Person.minSalary +"\t" + Person.maxSalary);
System.out.println(Person.minAge);
}
}

Output
10000 100000
21

2. Static blocks are executed in the order they defined
Example
class Person{
static int minSalary;
static int maxSalary;
static int minAge;

static{
System.out.println("I am the first static Block");
}

static{
System.out.println("I am the second static Block");
}

static{
System.out.println("I am the third static Block");
}

public static void main(String args[]){
System.out.println("I am in main method");
}
}

Output
I am the first static Block
I am the second static Block
I am the third static Block
I am in main method

3. You can't initialize a instance variable in the static block
Example
class Person{
String name;

static{
name = "Krishna";
}
}

When you try to compile the above code, compiler throws the below error
Person.java:4: error: non-static variable name cannot be referenced from a static context
name = "Krishna";
^
1 error

 

Keyword : this
“this” is a reference to the current object. i.e, by using “this” in any instance method or constructor, you can point out current object properties, behaviors and you can call current object constructors.

Example
class Person{
String firstName, lastName;

Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

public static void main(String args[]){
Person p1 = new Person("Gopi", "Battu");
System.out.println(p1.firstName +"\t"+p1.lastName);
}
}

Output
Gopi Battu

As you seen in the above example, constructor Person has parameter names as firstName, lastName which are same like instance properties of an object. So to differentiate parameters with instance properties we used the keyword “this”.

this can be used to call constructor
Example
class Person{
String firstName, lastName;

Person(){
this("noName", "noName"); // calls the constructor
}

Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

public static void main(String args[]){
Person p1 = new Person();
System.out.println(p1.firstName +"\t"+p1.lastName);
}
}

Output
noName noName

As you see in the above program, this is used to call the constructor.

Some Points to Remember
1. call to “this” must be first statement in constructor
Example
class Person{
String firstName, lastName;

Person(){
firstName = "HI";
this("noName", "noName");
}

Person(String firstName, String lastName){
this.firstName = firstName;
this.lastName = lastName;
}

public static void main(String args[]){
Person p1 = new Person();
System.out.println(p1.firstName +"\t"+p1.lastName);
}
}

When you try to compile the above program, compiler throws below error

Person.java:7: error: call to this must be first statement in constructor
this("noName", "noName");
^
1 error

2. Calling this from any methods other than constructor causes compiler error
class Person{
Person(){

}

void setName(){
this();
}
}

When you try to compile the above program, compiler throws below error
Person.java:7: error: call to this must be first statement in constructor
this();
^
1 error

Our Team

We have 2500+ expert pool from all over the world. They are highly qualified and well experienced. All experts have experinced in teaching and good command over programming topics. Some of our experts are as follows:


John Doe

JAVA EXPERT(10+ Yrs Exp.)

Larry Doe

C EXPERT(12+ Yrs Exp.)

Ranith Kays

C++ EXPERT(8+ Yrs Exp.)

Joan Ray

OPEN SOURCE LANGUAGE EXPERT(8+ Yrs Exp.)