Skip to content

Java OOP

0/36 lessons · 0% complete

— XP

Java OOP · Module 2

Creating objects with new

What really happens on the right-hand side

16m45 XP
→→→

What you'll be able to do

  • List the four things `new` does, in order
  • Create objects and call methods that change their state over time
  • Explain why a declared-but-not-created variable throws NullPointerException
  • Track an object's state through a sequence of method calls
1

Creating Objects (new)

The new keyword, step by step

`new Student()` looks like one action. It is actually four.

What Java does when it reads `Student s = new Student();`

  1. Allocates memory on the heap big enough for all of Student's fields.
  2. Fills every field with its type default: 0, 0.0, false, null.
  3. Runs the constructor (Module 3). Right now that is the invisible default constructor, which does nothing extra.
  4. Returns the reference — the object's address — which is stored in the variable s.

Reading the declaration properly

Main.java
1Student s = new Student();
2s.name = "Sara";
3s.introduce();
Student s = new Student();
Left of =: the declared type and the variable. Right of =: the object being built. The variable is not the object — it points at it.
s.name = "Sara";
The dot means 'go to the object this reference points at, and use its member'. Here we write to a field.
s.introduce();
Same dot, but now we call behaviour. Inside introduce, name means this object's name — "Sara".

Declaration is not creation

Student s; creates a variable holding null. The first s.name or s.introduce() then throws NullPointerException at runtime. If you see that exception, look for a missing new.

Prove it — coding challenges

2

Object State

State evolves through method calls

An object is not a snapshot; it has a lifetime. Every call can move it to a new state, and the next call sees the result of the previous one. Reading code therefore means tracking state, not just reading lines. Keep a small table in your head (or on paper): after each call, what does every field hold?

Track the counter

Before running: what will `show()` print each time? Then add one more `increment()` and re-check.

Output
Press Run to execute this example.

Heap after three increments

Stack
c→ref → #1
Heap
#1: Counter
value3

The variable never changed — only the object it points at did.

Common mistakes

WrongCounter c; c.increment();
Why`c` is null — no object was created.
FixCounter c = new Counter(); c.increment();
Wrongnew Counter(); c.increment();
WhyThe object was created but its reference was never stored, so it is unreachable immediately.
FixAlways assign the result: `Counter c = new Counter();`
WrongExpecting `new` to copy an existing object.
Why`new` always builds a fresh object with default field values.
FixTo copy, create a new object and assign each field (or use a constructor — Module 3).

Lesson playground

Free practice

A lamp that toggles. Add a `brightness` field and a method that raises it, then print both fields.

Output
Press Run to execute this example.

Check understanding

Quiz

Question 1 of 4

Track the boolean field through three prints.

Main.java
1class Lamp {
2 boolean on;
3
4 void toggle() {
5 on = !on;
6 }
7}
8
9public class Main {
10 public static void main(String[] args) {
11 Lamp l = new Lamp();
12 System.out.println(l.on);
13 l.toggle();
14 System.out.println(l.on);
15 l.toggle();
16 System.out.println(l.on);
17 }
18}

Summary

  • `new` allocates, defaults every field, runs the constructor, and returns a reference.
  • The variable holds a reference; the object lives on the heap.
  • Missing `new` → NullPointerException as soon as you use the dot on a method.
  • Reading object code means tracking state call by call.