Java Notes
Understand Java classes and objects — how to define a class, create objects with new, what happens in memory, the difference between reference and object, and how null works.
Every Java program is built around classes and objects. You cannot write real Java without understanding these two ideas, so let us build a solid picture before anything else.
What is a Class?
A class is a blueprint. It describes what an object will look like (its data) and what it can do (its behaviour). The class itself is not an object — it is just the definition written in code.
Think of a class like the design plan for a mobile phone. The plan says: every phone has a brand, a model, and a battery level. The plan also says: a phone can make a call and charge. But you cannot make a call on a plan — you need the actual phone.
What is an Object?
An object is a real instance built from the class blueprint. When you create an object, Java allocates memory for it and stores the actual values. One class can produce as many objects as needed, each with its own independent data.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
class Phone— defines the blueprint. Everything inside the{}belongs to this class.String brand; String model; int batteryLevel;— these are fields (also called instance variables). Every Phone object gets its own copy of these.void charge()— a method. This is the behaviour. It works on whichever object called it.Phone p1 = new Phone();— creates a new Phone object in memory and stores a reference to it inp1.p1.brand = "Samsung";— sets the value on p1's own copy ofbrand.p1.charge()— calls the method on p1. Insidecharge(),brandrefers to p1's brand.p2is completely separate — changingp2.batteryLevelnever affectsp1.
Run this. Notice each object has its own data and behaves independently.
What Actually Happens When You Write new Phone()?
Three things happen in order:
- Java allocates memory on the heap for the new Phone object.
- All fields are set to their default values (
nullfor String,0for int). - The reference to this object is returned and stored in your variable (
p1).
After this, p1 is not the object itself — it is a reference variable that holds the memory address of the object.
Reference Variable vs Object — A Critical Difference
This is the single most important thing beginners get wrong.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
Phone p2 = p1;— bothp1andp2now point to the same object in memory. No second Phone was created.p2.brand = "Updated Brand"— this modifies the one shared object.p1.brandis also changed because there is only one object.p2 = new Phone()— nowp2is redirected to a brand new object.p1still points to the original.
Memory picture:
What is null?
null means a reference variable exists but points to no object.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
NullPointerException is one of the most common Java errors. It always means you tried to use a reference that points to nothing. The fix is always the same — make sure the object is created with new before calling methods on it.
Instance Variables vs Local Variables
There are two kinds of variables you will see inside a class. Knowing the difference matters.
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
Line by line:
int count = 0;— instance variable. Each Counter object has its owncount.c1.countandc2.countare separate.int step = 1;— local variable insideincrement(). It is created each time the method runs and destroyed when the method returns.c1.increment()twice —c1.countbecomes 2.c2.increment()once —c2.countbecomes 1. It started at 0, not wherec1left off.
| Feature | Instance Variable | Local Variable |
|---|---|---|
| Declared | In class body | Inside a method |
| Default value | Yes (0, null, false) | No — must be set before use |
| Lifetime | As long as the object lives | Only during the method call |
| Shared? | Each object has its own copy | Only visible in its method |
Multiple Objects from One Class
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
One class, three completely independent objects. The printGrade() method uses whatever name and marks belong to the object that called it.
Common Mistake 1: Calling a Method Without Creating an Object
// WRONG
Student.printGrade(); // compile error — Student is a class, not an object
// CORRECT
Student s = new Student();
s.name = "Asha";
s.marks = 85;
s.printGrade();A method needs an object to work on. You must create one first.
Common Mistake 2: Thinking p2 = p1 Creates a Copy
// WRONG assumption
Phone p1 = new Phone();
p1.brand = "Samsung";
Phone p2 = p1; // This is NOT a copy — both point to the same object
p2.brand = "Apple";
System.out.println(p1.brand); // Apple — not Samsung!p2 = p1 copies the reference, not the object. If you want two separate objects, create two with new:
// CORRECT — two separate objects
Phone p1 = new Phone();
p1.brand = "Samsung";
Phone p2 = new Phone();
p2.brand = "Apple";
System.out.println(p1.brand); // Samsung — not affected by p2Common Mistake 3: Forgetting new
// WRONG — p is null, no object created
Phone p;
p.charge(); // NullPointerException
// CORRECT
Phone p = new Phone();
p.charge();Phone p; only declares a variable. The object does not exist until you write new Phone().
Practice Task
Task: Create a class Laptop with fields brand (String), ramGB (int), and isOn (boolean). Add a method boot() that prints "[brand] is starting up..." and sets isOn to true. Add a method shutdown() that prints "[brand] shutting down." and sets isOn to false. In main, create two Laptop objects, boot both, then shut down one. Print the isOn value of each at the end.
Hint: Follow the same pattern as Phone — declare fields, write methods that use this object's fields, create objects with new in main.
Solution:
Try it in WoHoCode
Edit the program, then run it here.
Run this example to see its output.
What to observe: l1.isOn is false after shutdown. l2.isOn is still true — completely separate object, completely separate state. Try writing Laptop l3 = l1; then calling l3.boot() — check if l1.isOn also changes.
Continue the lesson path