Java Class Notes: Classes vs. Objects

Learning Goal

By the end of these notes, students should be able to explain the difference between a class and an object in Java and write simple Java code that creates and uses objects.

1. What Is a Class?

A class is a blueprint or plan for creating objects.

A class describes:

Think of a class like a blueprint for a house. The blueprint is not the actual house. It only describes how the house should be built.

In Java, a class defines the structure for objects.

Example Class

public class Student {
    String name;
    int gradeLevel;

    public void introduce() {
        System.out.println("Hello, my name is " + name);
    }
}

This class is named Student.

It describes a student object that has:

String name;
int gradeLevel;

It also has a method:

introduce()

2. What Is an Object?

An object is an actual item created from a class.

If the class is the blueprint, the object is the real thing built from the blueprint.

A class is general. An object is specific.

Example Object

Student s1 = new Student();

This creates a new Student object. The variable s1 refers to that object.

3. Class vs. Object

Class Object
A blueprint A real example created from the blueprint
Describes what something should have and do Actually stores data and performs actions
Does not represent one specific thing Represents one specific thing
Example: Student Example: s1, s2, studentA

4. Real-World Example

Imagine a class called Car.

The class describes what all cars might have:

The class also describes what cars can do:

The class is not one real car.

An object would be a specific car, such as:

5. Java Example: Car Class

public class Car {
    String color;
    String make;
    int year;

    public void displayInfo() {
        System.out.println(year + " " + color + " " + make);
    }
}

This class defines a Car.

Each Car object can have its own:

color
make
year

6. Creating Car Objects

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car();
        car1.color = "Red";
        car1.make = "Mazda";
        car1.year = 2018;

        Car car2 = new Car();
        car2.color = "Blue";
        car2.make = "Toyota";
        car2.year = 2024;

        car1.displayInfo();
        car2.displayInfo();
    }
}

Output

2018 Red Mazda
2024 Blue Toyota

7. Key Idea

Both car1 and car2 are created from the same Car class. However, they are different objects.

Each object has its own data.

car1.color = "Red";
car2.color = "Blue";

Changing one object does not automatically change the other object.

8. Instance Variables

An instance variable is a variable that belongs to an object.

Example:

String color;
String make;
int year;

Each object gets its own copy of these variables.

For example:

car1.color = "Red";
car2.color = "Blue";

The same class can create many objects, and each object can store different values.

9. Methods

A method is an action that an object can perform.

Example:

public void displayInfo() {
    System.out.println(year + " " + color + " " + make);
}

This method prints information about the car object.

When you call:

car1.displayInfo();

Java uses the data stored inside car1.

When you call:

car2.displayInfo();

Java uses the data stored inside car2.

10. Constructor

A constructor is a special method that runs when an object is created.

Constructors are often used to give objects starting values.

Constructor Example

public class Car {
    String color;
    String make;
    int year;

    public Car(String c, String m, int y) {
        color = c;
        make = m;
        year = y;
    }

    public void displayInfo() {
        System.out.println(year + " " + color + " " + make);
    }
}

Now we can create objects more easily:

Car car1 = new Car("Red", "Mazda", 2018);
Car car2 = new Car("Blue", "Toyota", 2024);

11. Full Constructor Example

public class Main {
    public static void main(String[] args) {
        Car car1 = new Car("Red", "Mazda", 2018);
        Car car2 = new Car("Blue", "Toyota", 2024);

        car1.displayInfo();
        car2.displayInfo();
    }
}

Output

2018 Red Mazda
2024 Blue Toyota

12. Important Vocabulary

Term Meaning
Class A blueprint for creating objects
Object A specific item created from a class
Instance Another word for an object
Instance variable A variable that belongs to an object
Method An action or behavior an object can perform
Constructor A special method used to create and initialize an object
new Keyword used to create an object

13. Common Mistake

Mistake

Thinking the class and object are the same thing.

Correction

The class is the plan. The object is the actual thing created from the plan.

Example:

Car car1 = new Car("Red", "Mazda", 2018);

14. Another Example: Dog Class

public class Dog {
    String name;
    int age;

    public Dog(String n, int a) {
        name = n;
        age = a;
    }

    public void bark() {
        System.out.println(name + " says woof!");
    }
}

Creating Dog Objects

public class Main {
    public static void main(String[] args) {
        Dog dog1 = new Dog("Buddy", 4);
        Dog dog2 = new Dog("Max", 2);

        dog1.bark();
        dog2.bark();
    }
}

Output

Buddy says woof!
Max says woof!

15. Why Classes and Objects Matter

Classes and objects are important because they allow programmers to organize code around real-world things.

Instead of writing one huge program, programmers can create classes for different parts of the program.

Examples:

This makes programs easier to understand, build, test, and fix.

16. Quick Review

17. Practice Questions

  1. What is the difference between a class and an object?
  2. In the following line, what is the class name?
Student s1 = new Student();
  1. In the following line, what is the object variable name?
Student s1 = new Student();
  1. What keyword is used to create a new object?
  2. What is a constructor used for?
  3. Can two objects from the same class store different values?
  4. What is an instance variable?
  5. What is a method?

18. Simple Student Practice Program

public class Student {
    String name;
    int gradeLevel;

    public Student(String n, int g) {
        name = n;
        gradeLevel = g;
    }

    public void printInfo() {
        System.out.println(name + " is in grade " + gradeLevel);
    }
}
public class Main {
    public static void main(String[] args) {
        Student student1 = new Student("Alex", 10);
        Student student2 = new Student("Maria", 11);

        student1.printInfo();
        student2.printInfo();
    }
}

Output

Alex is in grade 10
Maria is in grade 11

19. Summary

In Java, a class defines what an object should have and what it should do.

An object is a real example created from the class.

Classes help programmers organize code, avoid repetition, and model real-world things in a program.