HOWTO · Java
What Does Instantiate Mean in Java
Learn what it means to instantiate a class in Java, how new creates an object reference, and how constructors and instance fields establish object state.
On this page
In Java, instantiating a class means creating one of its objects; new creates it and returns a reference you can store and use.
What Does It Mean to Instantiate a Class in Java?
To instantiate a class in Java is to create an object that is an instance of that class. A class defines the fields and methods its objects can have; an object is a particular instance with its own field values. A variable such as car holds a reference to that object, so you can use the reference to access its fields and methods.
How new Creates an Object and a Constructor Initializes It
This example creates a Car object, stores its reference in car, and calls a method on it:
public class Car {
private final String model;
private final String color;
Car(String model, String color) {
this.model = model;
this.color = color;
}
void display() {
System.out.println("Car model: " + model + ", Color: " + color);
}
public static void main(String[] args) {
Car car = new Car("Toyota", "Red");
car.display();
}
}
The expression new Car("Toyota", "Red") creates a Car instance and invokes its constructor with two arguments. The expression produces a reference, which the assignment stores in car. A constructor has the class name and no return type; it can use its parameters to set up the new object’s fields. The program prints the model and color through the reference.
A class does not need an explicitly written constructor. Java supplies a no-argument default constructor only when the class declares no constructors of its own. Once you declare a constructor with parameters, Java does not also add that default constructor.
An abstract class cannot be instantiated directly because it may leave methods for subclasses to implement. You can instantiate a concrete subclass that provides those implementations.
How Objects Hold State and Use Instance Methods
Each object has its own instance-field values. For example, two Car objects can hold different models and colors, even though both use the same display() method defined by Car. Instance methods operate on the particular object referenced when you call them; in car.display(), the method reads that car’s fields. Creating another object gives you another set of instance state, rather than changing the first object’s values.
Instantiation vs. Initialization in Java
Instantiation means creating an object. Initialization means establishing its starting state. These are related steps, but they are not synonyms: Java initializes instance fields to their default values as part of object creation, then field initializers and the constructor can provide more useful values. Later assignments can change the object’s state too.
In the example, new Car(...) is the instantiation expression, while the Car constructor assigns the starting model and color values. A constructor is one way to initialize an object; it does not make the terms “instantiation” and “initialization” interchangeable.