09 - Objects
Objects & Classes
What is it?
A class is a blueprint - a template that describes what something is (its properties) and what it can do (its methods). An object is a concrete instance created from that blueprint - the actual thing in memory with real values.
Think of it like this:
- Class = the cookie cutter (the shape/design)
- Object = the actual cookie (real, tangible, with actual dough)
Why does it exist?
As programs grow, you need to model real-world things (a Person, a BankAccount, a Car). Classes let you group related data and behavior together into a single unit, making code easier to understand and reuse.
Defining a Class
public class Person {
// Fields (properties) - what a Person HAS
String name;
int age;
// Constructor - how to CREATE a Person
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// Methods - what a Person can DO
public void greet() {
System.out.println("Hi, I'm " + name + " and I'm " + age + " years old.");
}
}
Creating Objects (Instantiation)
// Creating instances of Person
Person alice = new Person("Alice", 30);
Person bob = new Person("Bob", 25);
// Calling a method on an object
alice.greet(); // Hi, I'm Alice and I'm 30 years old.
bob.greet(); // Hi, I'm Bob and I'm 25 years old.
new Person(...)→ creates a newPersonobject and calls the constructoraliceandbobare reference variables - they point to objects in memory- Each object has its own copy of the fields (
name,age)
The Constructor
A constructor is a special method that runs when you create an object. It has:
- The same name as the class
- No return type (not even
void) - Used to set the initial values of fields
public Person(String name, int age) {
this.name = name; // this.name = the field, name = the parameter
this.age = age;
}
Note
thisrefers to the current object - the specific instance being constructed.this.nameis the field on the object;name(nothis) is the parameter passed in. Withoutthis, there’d be ambiguity when they share the same name.
Fields (Instance Variables)
Fields are variables that belong to an object. Every object gets its own copy:
alice.name // "Alice"
bob.name // "Bob" - completely separate from alice.name
Methods on Objects
Unlike static methods you saw in the basics, instance methods belong to a specific object and can access its fields:
public void birthday() {
age = age + 1; // modifies THIS object's age
}
alice.birthday();
System.out.println(alice.age); // 31
System.out.println(bob.age); // still 25 - bob is unaffected
null - When an Object Doesn’t Exist Yet
A reference variable that hasn’t been assigned an object holds null:
Person p = null; // p points to nothing
p.greet(); // NullPointerException! Can't call methods on null
Caution
NullPointerException(NPE) is one of the most common runtime errors in Java. Always ensure your reference is notnullbefore calling methods on it.
Putting it All Together - Full Class Example
public class BankAccount {
String owner;
double balance;
public BankAccount(String owner, double initialBalance) {
this.owner = owner;
this.balance = initialBalance;
}
public void deposit(double amount) {
if (amount > 0) {
balance += amount;
}
}
public void withdraw(double amount) {
if (amount > 0 && amount <= balance) {
balance -= amount;
} else {
System.out.println("Insufficient funds");
}
}
public void printBalance() {
System.out.println(owner + "'s balance: " + balance);
}
}
// Using it:
BankAccount account = new BankAccount("Alice", 1000.0);
account.deposit(500.0);
account.withdraw(200.0);
account.printBalance(); // Alice's balance: 1300.0
Gotchas
- A class file must be saved as
ClassName.java- exact match, case-sensitive - Forgetting
newwhen creating an object → compiler error - Using
==to compare objects compares memory addresses, not content - use.equals()or write your own comparison method - Calling a method on
null→NullPointerExceptionat runtime
Related: Methods, Lists, String, Comparison