Instantiation in Java
Instantiation is an immense word to define a universal and straightforward concept in Java programming, creating new instances of objects to be used in a program. In this section, we will discuss what is instantiation in Java, how to instantiate a class, and what are the ways to create instances or objects of the class?
In Java, instantiation mean to call the constructor of a class that creates an instance or object of the type of that class. In other words, creating an object of the class is called instantiation. It occupies the initial memory for the object and returns a reference. An object instantiation in Java provides the blueprint for the class.
What is an object?
Syntax for Instantiation
Let's understand the above statements through an example.
There are two ways to create instances:
Using the new Keyword
Java provides the new keyword to instantiate a class.
Defining a Reference
We can also instantiate the above class as follows if we defining a reference variable.
We observe that when we use the new keyword followed by the class name, it creates an instance or object of that class. Creating a constructor of the class is also known as instantiation.
Note: If we want to invoke the methods of the local inner class, we must instantiate that class.
Using Static Factory Method
Another way to instantiate a class is by calling a static factory method. A class can provide a public static factory method that is nothing but a static method that returns an instance of the class. Always remember that it is not the same as the factory method pattern.
We can use it instead of the constructor. Providing a static factory method instead of a constructor has both pros and cons. The first advantage is that static factory methods also have names, unlike constructors. The second advantage is that unlike constructors they are not required to create a new object each time they are invoked. The third advantage is that unlike constructors they can return an object of any subtype of their return type. The fourth advantage is that they reduce the verbosity of creating parameterized type instances.
The disadvantage is that providing only static factory methods is that classes without public or protected constructors cannot be sub-classed. Another disadvantage is that they are not readily distinguishable from other static methods.
Let's see an example of the static factory method.
Let's see some important questions that may click in the mind.
Can we instantiate abstract class?
No, we cannot instantiate abstract classes. But they can be sub-classed. When an abstract class is sub-classed, it usually provides implementations for all of the abstract methods in its parent class.
What is the difference between instantiation and initialization?
Instantiation and initialization are completely different concepts in Java programming.
Initialization: Assigning a value to a variable is called initialization. For example, cost = 100. It sets the initial value of the variable cost to 100.
Instantiation: Creating an object by using the new keyword is called instantiation. For example, Car ca = new Car(). It creates an instance of the Car class.