When it comes to Java programming, you should know the rules of default initialization of instance variables as well as how initialization blocks work. And in this article, I’m happy to discuss with you about this topic in Java.

 

1. Default Initialization of Instance Variables in Java

When you declare a variable without assigning it an explicit value, the Java compiler will assign a default value. Let’s consider the following example:

public class DefaultVarInit {
	int number;			// number will have default value: 0
	float ratio;		// default value: 0.0
	boolean success;	// default value: false

	String name;		// default: null
	Object obj;			// default: null

	public void print() {
		System.out.println("number = " + number);
		System.out.println("ratio = " + ratio);
		System.out.println("success = " + success);
		System.out.println("name = " + name);
		System.out.println("obj = " + obj);
	}

	public static void main(String[] args) {
		new DefaultVarInit().print();
	}

}
 

Output:

number = 0
ratio = 0.0
success = false
name = null
obj = null
 

NOTE: This default initialization applies for instance variables, not for method variables. For variables in method, we have to initialize them explicitly.

So remember these rules:

  • Integer numbers have default value: 0
    • for int type: 0
    • for byte type: (byte) 0
    • for short type: (short) 0
    • for long type: 0L
  • Floating point numbers have default value: 0.0
    • for float type: 0.0f
    • for double type: 0.0d
  • Boolean variables have default value: false
  • Character variables have default value: ‘\u0000’
  • Object references have default value: null
 

2. Default Initialization of Arrays in Java



An array is an object, hence the default value of an uninitialized array (member variable) is null. For example:

String[] names;	// default is null
When the size of an array is initialized, then its components will have default values specified by the rules above. For example:

String[] names = new String[5];	// all 5 String elements are null by default
int[] numbers = new int[10];	// all 10 integer elements are zeroes by default
NOTE: The default initialization of array components is the same, regardless of the array itself is member variable or local variable.

 

3. Instance Initialization Blocks in Java

In Java, we can put a piece of code inside a block enclosed within { and }. Let’s look at the following example:

public class Dog {
	// begin instance initialization block
	{
		System.out.println("Dog's Instance Init Block");
	}
	// end instance initialization block


	public Dog() {
		System.out.println("Dog's constructor");
	}
}
As you can see, the code between the { and } is called instance initialization block, which is executed every time an instance of the class is created and after the call to super() in the constructor. For example, if we instantiate a new Dog object like this:

Dog dog = new Dog();
Then we will have the following output:

Dog's Instance Init Block
Dog's constructor
It’s because the Java compiler inserts a call to super() in the constructor like this:

public Dog() {
	super();
	System.out.println("Dog's constructor");
}
There can be multiple instance initialization blocks in a class, and they are executed in the order they appear. Consider the following example:

public class Cat {
	{
		System.out.println("Cat's Instance Init Block #1");
	}

	public Cat() {
		System.out.println("Cat's Constructor");
	}

	{
		System.out.println("Cat's Instance Init Block #2");
	}

	public void foo() {
		System.out.println("Cat's Foo");
	}
}
Test code:

Cat cat = new Cat();
And output:

Cat's Instance Init Block #1
Cat's Instance Init Block #2
Cat's Constructor
 

4. Static Initialization Blocks in Java

Here’s an example of a static initialization block:

public class Bird {
	// begin static initialization block
	static {
		System.out.println("Bird's Static Init Block");
	}
	// end static initialization block

	public Bird() {
		System.out.println("I'm a bird");
	}
}
Unlike instance initialization blocks, a static block is executed only once when the class is first loaded, whereas an instance initialization block is executed every time an instance is created.

There can be multiple static initialization blocks in a class, and they are executed in the order they appear.

Note that static initialization blocks are always executed before instance initialization blocks (but only once).

Let’s see the following example:

public class Bird {

	static {
		System.out.println("Bird's Static Init Block #1");
	}


	public Bird() {
		System.out.println("I'm a bird");
	}

	{
		System.out.println("Bird's Instance Init Block #1");
	}

	public void fly() {
		System.out.println("I'm flying");
	}

	{
		System.out.println("Bird's Instance Init Block #2");
	}

	static {
		System.out.println("Bird's Static Init Block #2");
	}

}
Test code:

Bird bird = new Bird();
 

and output:

Bird's Static Init Block #1
Bird's Static Init Block #2
Bird's Instance Init Block #1
Bird's Instance Init Block #2
I'm a bird
I hope you have learned something new about variable default initialization and instance and static initialization blocks in this article.

 

Other Recommended Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



Add comment

   


Comments 

#1Mohammed Asif2020-04-25 03:48
Who will assign the default values for global variables.?
According to my study constructor will assign the default values .?
But how it will assign is my doubt.?
Please can you clarify my doubt.
Quote