Skip to content

Java Programming

What is Java Object Initialization?

The concept of objects in a broad sense can be described as a collection of data along with the methods to manipulate the corresponding data. The Objects are be loaded into the memory before processing. In main memory the object has a state determined by its instance variables and their values, which can be processed and evolved throughout its lifetime. The newly-created object should have a newly-allocated memory (heap) to accommodate the object’s instance variables, this is done by Java virtual machine (JVM).

Object initialization is very important in programming, as it is a common source of bugs. The Java Programming language provides different built-in mechanisms to ensure proper initialization of the memory occupied by a newly-created object, there by helping the objects to get a valid initial state.

The Java Programming language offers three mechanisms to ensure proper initialization of newly created objects.

  • Constructors.
  • Instance variable initialization.
  • Instance initialization.

When we are creating an object using new operator or the new Instance() method of class, JVM allocates memory for it, then Java virtual machine will run the initialization code before we use the newly-allocated memory.


Constructors are methods, with a set of parameters and a body of code and has the same name as the class but has no return type. The constructors can be overloaded by varying the number, types, and order of parameters. When we are creating new instance using new keyword we often provide a constructor that initialize the Object. If we are using class that has no constructor defined, the compiler will automatically generate a default constructor for the class. This constructor has no parameter and has empty body, this ensures that each class has at least one constructor.

class Demo


int a;

public Demo()


// the initialization code for instance variables




Instance variable initialization

In an instance variable initialization method java uses an equals sign and expression technique to initialize the instance variable.

class Demo


private int a = 1; //this evaluate a with a value of 1


Instance initialization

Instance initialization is also known as instance initialization block, which is one of the alternatives to Instance variable initialization. It is very useful in following situations

  • Initializer code need to catch exceptions.
  • Initializer code need to do calculations that can’t be expressed with an instance variable initializer.
  • If class has multiple constructors – no need to repeat the same initializing code in all constructors.
  • When we use anonymous inner classes, which normally do not have constructors.

class Demo


private int a;






One of the main properties of initializers is they cant make forward reference, as the initializers are executed in textual order.You have to make sure that the referring of instance variable should be only after it is initialized.

Default initial values

If we are not providing any explicit initialization method to variables including instance and class variables, they will be initialized according to their type. Each data type has its own default initial values. The local variable must be initialized explicitly before use.

e.g boolean-false

int – 0

Java’s initialization mechanisms help the programmer to ensure that objects we design have a valid and predictable state.

Julian is a professional content writer on Java Development and now working at Telious Technologies.

Be Sociable, Share!
    The following two tabs change content below.

    Latest posts by Ray (see all)

    Add Your Comment (Get a Gravatar)