Creating Objects
& Initialization
Learn how to create objects using constructors and initialize their state to build powerful Java programs.
What Is an Object?
In Java, an object is a specific instance of a class. A class defines the blueprint: what data (fields) and behaviors (methods) an object will have. An object is the actual thing built from that blueprint, living in memory at runtime.
What Is a Constructor?
A constructor is a special method that runs automatically when you create a new object. It has the same name as the class, has no return type (not even void), and its job is to initialize the object's state.
The new Keyword
To create an object, you use the new keyword followed by a constructor call. This allocates memory on the heap and runs the constructor to initialize the object.
Object Initialization: The Full Picture
The Default Constructor
If you don't write any constructor, Java provides a no-argument default constructor automatically. It initializes all fields to their default values. But the moment you write any constructor, the default one disappears.
Class vs Object: Key Distinction
| Class | Object | |
|---|---|---|
| What it is | A blueprint / template | An instance built from the blueprint |
| Memory | No memory per instance | Allocated on the heap |
| Created with | class keyword |
new keyword |
| How many? | One per type definition | Many from the same class |
| Holds data? | Defines fields (no values) | Stores actual values |
Constructor Overloading
A class can have multiple constructors with different parameter lists. Java picks the right one based on the arguments you pass. This is called constructor overloading.
The this Keyword
When a constructor parameter has the same name as a field, use this to distinguish them. this.name refers to the field; name alone refers to the parameter.
How to Create an Object: Step by Step
this.new keyword: ClassName obj = new ClassName(args);Example 1: Student Class
Example 2: Rectangle with Overloading
Example 3: BankAccount
References & null
A variable of a class type holds a reference (memory address), not the object itself. If you declare a variable without using new, it is null: it points to nothing.
Common Mistakes
void Dog() makes it a regular method, not a constructor. Constructors have no return type.Dog d = Dog("Rex", 2); without new is a compile error. The new keyword is required.name = name; assigns the parameter to itself. Use this.name.Summary: Constructor Concepts
| Concept | Syntax / Rule | Key Point |
|---|---|---|
| Constructor | Same name as class, no return type | Initializes object state |
| new keyword | new ClassName(args) |
Allocates memory + calls constructor |
| Default constructor | Auto-provided if none written | Disappears when you write one |
| Overloading | Multiple constructors, different params | Matched by argument count + type |
| this keyword | this.field = param |
Disambiguates field from parameter |
| null reference | Declared but not initialized | Causes NullPointerException |
Question 1 of 5
Which of the following is a valid constructor for a class named Car?
Question 2 of 5
What happens if you write a class with no constructors at all?
Question 3 of 5
Consider this code:
public class Book {
String title;
int pages;
public Book(String title, int pages) {
title = title;
pages = pages;
}
}
Book b = new Book("Java", 400);
After creating b, what are the values of b.title and b.pages?
title = title and pages = pages just assign the parameters to themselves. The fields keep their default values: null and 0.Question 4 of 5
A class Pen has two constructors: Pen(String color) and Pen(String color, double thickness). What is this called?
Question 5 of 5
Consider this code:
public class Lamp {
boolean isOn;
public Lamp(boolean isOn) {
this.isOn = isOn;
}
}
Which statement will cause a compile error?
Lamp(boolean) was defined, the default no-argument constructor no longer exists. Calling new Lamp() with no arguments causes a compile error.