01 / 21

Creating Objects
& Initialization

Learn how to create objects using constructors and initialize their state to build powerful Java programs.

Java AP Computer Science A

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.

Class: Dog name: String age: int bark() fetch() new Object: myDog name = "Buddy" age = 3 Object: yourDog name = "Max" age = 5

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.

public class Dog { String name; int age; // Constructor public Dog(String n, int a) { name = n; age = a; } }

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.

Dog myDog = new Dog("Buddy", 3);
Dog Type myDog Reference = new Allocate Dog("Buddy", 3) Constructor Call Anatomy of Object Creation

Object Initialization: The Full Picture

1. Memory Allocated (Heap) 2. Fields Set to Defaults 3. Constructor Body Runs 4. Reference Returned to Variable

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.

No Constructor Written
Java provides Dog() automatically. Fields get defaults: null, 0, false.
Custom Constructor Written
Default constructor disappears. You must explicitly write Dog() if you still need it.
// This class has NO explicit constructor public class Cat { String name; } // Java auto-provides Cat() so this works: Cat c = new Cat(); // name is null

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.

public class Dog { String name; int age; // Constructor 1: both parameters public Dog(String n, int a) { name = n; age = a; } // Constructor 2: name only, default age public Dog(String n) { name = n; age = 1; } }

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.

public class Dog { String name; int age; public Dog(String name, int age) { this.name = name; // field = parameter this.age = age; // field = parameter } }

How to Create an Object: Step by Step

1
Define the class with fields (instance variables) that hold the object's state.
2
Write constructor(s) that accept parameters and assign them to the fields using this.
3
Instantiate with the new keyword: ClassName obj = new ClassName(args);
4
Use the object: call methods and access fields through the reference variable.

Example 1: Student Class

public class Student { String name; int grade; public Student(String name, int grade) { this.name = name; this.grade = grade; } } // In main method: Student s1 = new Student("Alice", 10); Student s2 = new Student("Bob", 11);

Example 2: Rectangle with Overloading

public class Rectangle { double width; double height; // Full constructor public Rectangle(double w, double h) { width = w; height = h; } // Square shortcut public Rectangle(double side) { width = side; height = side; } public double area() { return width * height; } }
Rectangle r1 = new Rectangle(5.0, 3.0); // 5x3 Rectangle r2 = new Rectangle(4.0); // 4x4 square System.out.println(r1.area()); // 15.0 System.out.println(r2.area()); // 16.0

Example 3: BankAccount

public class BankAccount { String owner; double balance; public BankAccount(String owner, double initialDeposit) { this.owner = owner; this.balance = initialDeposit; } public BankAccount(String owner) { this.owner = owner; this.balance = 0.0; } public void deposit(double amount) { balance += amount; } }

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.

myDog Stack (Reference) Dog Object name = "Buddy" age = 3 Heap (Object Data) Dog x; // x is null No object created yet

Common Mistakes

✕ Adding a Return Type
Writing void Dog() makes it a regular method, not a constructor. Constructors have no return type.
✕ Forgetting new
Writing Dog d = Dog("Rex", 2); without new is a compile error. The new keyword is required.
✕ Skipping this
When parameter and field names match, 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

After creating b, what are the values of b.title and b.pages?

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

Which statement will cause a compile error?