Creating Objects in Java — The new Keyword, Constructors & Object Lifecycle
Creating an object means allocating memory and initializing fields. In Java, you use the new keyword.
The new Keyword
Employee emp = new Employee("Alice", "Engineering", 95000);
What happens:
- Memory allocated on the heap for the object
- Fields initialized to default values (0, null, false)
- Constructor called to set initial values
- Reference returned and stored in
emp
Default Constructor
If you don’t write any constructor, Java provides a default constructor (no-arg):
class Dog {
String name;
int age;
// Java provides: Dog() { } implicitly
}
Dog d = new Dog(); // uses default constructor
// d.name = null, d.age = 0
⚠️ If you define any constructor, Java no longer provides the default one.
Custom Constructors
class Employee {
String name;
double salary;
// Custom constructor
Employee(String name, double salary) {
this.name = name;
this.salary = salary;
}
}
Employee e = new Employee("Alice", 95000);
// Employee e2 = new Employee(); // ERROR — no default constructor!
Constructor Overloading
Multiple constructors with different parameter lists:
class Employee {
String name;
String department;
double salary;
// Full constructor
Employee(String name, String department, double salary) {
this.name = name;
this.department = department;
this.salary = salary;
}
// Partial — default department
Employee(String name, double salary) {
this(name, "Unassigned", salary); // calls full constructor
}
// Minimal
Employee(String name) {
this(name, 0); // chains to 2-arg constructor
}
}
this(...) calls another constructor in the same class — must be the first statement.
Object Initialization Patterns
Pattern 1: Constructor
Employee e = new Employee("Alice", "Engineering", 95000);
Pattern 2: Setter Methods
Employee e = new Employee();
e.setName("Alice");
e.setDepartment("Engineering");
e.setSalary(95000);
Pattern 3: Builder Pattern (preview)
Employee e = Employee.builder()
.name("Alice")
.department("Engineering")
.salary(95000)
.build();
Object References
Variables hold references (pointers), not the object itself:
Employee a = new Employee("Alice", 95000);
Employee b = a; // b points to the SAME object
b.giveRaise(10);
System.out.println(a.getSalary()); // also raised! Same object.
Stack Heap
┌─────┐ ┌─────────────────┐
│ a ────→ │ name: "Alice" │
│ │ ↗ │ salary: 104500 │
│ b ─── └─────────────────┘
└─────┘
Object Comparison
Employee a = new Employee("Alice", 95000);
Employee b = new Employee("Alice", 95000);
System.out.println(a == b); // false — different objects
System.out.println(a.equals(b)); // false — unless equals() is overridden
Override equals() for meaningful comparison:
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Employee e)) return false;
return name.equals(e.name) && salary == e.salary;
}
Garbage Collection
When no references point to an object, it becomes eligible for garbage collection:
Employee e = new Employee("Alice", 95000);
e = null; // object is now unreachable — GC can reclaim it
Java’s garbage collector runs automatically — you don’t need to free memory manually.
Quick Exercises
- Create a
Carclass with overloaded constructors: full, brand-only, and default. - Demonstrate that two variables pointing to the same object share state changes.
- What happens if you call
new Employee()when only a parameterized constructor exists?
Summary
newallocates memory + calls constructor- Default constructor provided only if no constructors are defined
- Constructor overloading: multiple constructors with different parameters
this(...)chains constructors- Variables hold references, not objects
- Garbage collector reclaims unreachable objects
What’s Next?
Let’s explore the difference between Instance Variables and Methods vs class-level (static) members.