Curriculum / Java / Lesson 11

Classes & Objects in Java

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

11 of 11
intermediate 📖 6 min read 🎯 1 question

Classes & Objects

A class is a blueprint for creating objects — it defines the data (fields) and behavior (methods) that its objects will have. An object is a specific instance created from that blueprint using the new keyword.

Java is a fundamentally object-oriented language: essentially everything you write lives inside a class.

💡 Key insight A class is the recipe; an object is the actual dish made from it — you can make many objects from one class.

Syntax

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

Example

this.name = name; assigns the constructor's parameter to the object's own field, distinguishing them by name.

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();
    }
}
outputHi, I'm Aisha, age 21

Try it yourself

Copy this example into the Java compiler and run it — try changing a value and see what happens.

Quick Quiz

1. What keyword is used to create a new object from a class in Java?

Back to curriculum