The concept of an object in a broad sense can be described as a collection of data along with the methods to manipulate the corresponding data. The Objects are being loaded into the memory before processing. In the 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, thereby helping the objects to get a valid initial state.
The Java Programming language offers three mechanisms to ensure proper initialization of newly created objects.
- 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 have the same name as the class but have no return type. The constructors can be overloaded by varying the number, types, and order of parameters. When we are creating a new instance using a new keyword we often provide a constructor that initializes the Object. If we are using the class that has no constructor defined, the compiler will automatically generate a default constructor for the class. This constructor has no parameter and has an empty body, this ensures that each class has at least one constructor.
// 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.
private int a = 1; //this evaluate a with a value of 1
Instance initialization is also known as instance initialization block, which is one of the alternatives to Instance variable initialization. It is very useful in the 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 a 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.
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.
int – 0
Java’s initialization mechanisms help the programmer to ensure that the objects we design have a valid and predictable state.