Classes and Objects
Learn the foundations of OOP in Java - define classes as blueprints and create objects (instances) from them.
Overview
Key Points
- •A class defines the structure (fields) and behaviour (methods) of a type
- •An object is an instance of a class created with the new keyword
- •Fields (instance variables) hold the state of each object
- •Methods define what an object can do
- •Each object has its own independent copy of instance fields
- •Access with the dot operator: object.field or object.method()
- •Constructors initialise new objects - covered in depth in J035
Examples
Defining a class and creating objects
// Usually in its own file: Dog.java
class Dog {
// Fields (instance variables)
String name;
String breed;
int age;
// Method
void bark() {
System.out.println(name + " says: Woof!");
}
void info() {
System.out.println(name + " is a " + age + "-year-old " + breed);
}
}
public class Main {
public static void main(String[] args) {
// Create two Dog objects
Dog rex = new Dog();
rex.name = "Rex";
rex.breed = "German Shepherd";
rex.age = 3;
Dog bella = new Dog();
bella.name = "Bella";
bella.breed = "Labrador";
bella.age = 5;
rex.bark(); // Rex says: Woof!
bella.bark(); // Bella says: Woof!
rex.info(); // Rex is a 3-year-old German Shepherd
}
}rex and bella are separate objects - each has its own name, breed, and age.
A more complete class with methods that use fields
class BankAccount {
String owner;
double balance;
void deposit(double amount) {
balance += amount;
System.out.printf("Deposited $%.2f. Balance: $%.2f%n", amount, balance);
}
void withdraw(double amount) {
if (amount > balance) {
System.out.println("Insufficient funds!");
} else {
balance -= amount;
System.out.printf("Withdrew $%.2f. Balance: $%.2f%n", amount, balance);
}
}
void showBalance() {
System.out.printf("%s's balance: $%.2f%n", owner, balance);
}
}
public class Main {
public static void main(String[] args) {
BankAccount account = new BankAccount();
account.owner = "Alice";
account.balance = 1000.00;
account.deposit(250.0);
account.withdraw(100.0);
account.showBalance();
}
}Methods can read and modify the object's own fields directly without parameters.
null - an object reference with no object
public class Main {
public static void main(String[] args) {
Dog myDog = null; // reference, no object yet
myDog = new Dog(); // now it points to a Dog object
myDog.name = "Buddy";
myDog.bark();
// myDog = null; // if you uncomment and call myDog.bark(), NullPointerException!
}
}A reference variable set to null points to nothing. Calling methods on null throws NullPointerException.
Common Mistakes
- Calling methods or accessing fields on a null reference - always ensure the object is created (new) before use
- Thinking all objects share one set of fields - each object has its own independent field values
- Forgetting that class names should be PascalCase (Dog, BankAccount) while variable names use camelCase (myDog)
Note
Design classes around real-world concepts or logical entities in your domain. A good class name is a noun (Dog, Account, Product, Order). Methods are usually verbs (bark(), deposit(), calculate()).
Exercises
Write Java code and click Run to execute it instantly. When your output looks correct, mark as done.
Completed: 0 / 4 exercises
Knowledge Check
Question 1 of 4