Notes / Java / Classes & Objects

Classes & Objects in Java — Revision Notes

A quick-reference cheat sheet: definition, syntax, example, common mistakes and an interview tip.

intermediate 📖 2 min revision

Definition

📌 Classes & Objects A class is a blueprint for creating objects — it defines the data (fields) and behavior (methods) that its objects will have.

Syntax

syntax
class ClassName {
    fields;
    ClassName(parameters) { ... }  // constructor
    returnType method() { ... }
}
ClassName obj = new ClassName(args);

Example

java
class Student {
    String name;
    int age;

    Student(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void introduce() {
        System.out.println("Hi, I'm " + name + ", age " + age);
    }
}

public class Main {
    public static void main(String[] args) {
        Student s1 = new Student("Aisha", 21);
        s1.introduce();
    }
}

Output

outputHi, I'm Aisha, age 21

Important Points

Common Mistakes

Interview Tip

🎯 Before your interview Be ready to define encapsulation in one sentence: bundling data and the methods that operate on it, while restricting direct access to that data.
Back to notes Read full lesson →