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.
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.
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()
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.
Student s1 = new Student();
This creates a new Student object.
The variable s1 refers to that 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 |
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:
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
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();
}
}
2018 Red Mazda
2024 Blue Toyota
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.
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.
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.
A constructor is a special method that runs when an object is created.
Constructors are often used to give objects starting values.
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);
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();
}
}
2018 Red Mazda
2024 Blue Toyota
| 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 |
Thinking the class and object are the same thing.
The class is the plan. The object is the actual thing created from the plan.
Example:
Car car1 = new Car("Red", "Mazda", 2018);
Car is the class.car1 is the object variable.new Car(...) creates the object.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!");
}
}
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();
}
}
Buddy says woof!
Max says woof!
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:
StudentTeacherCarBankAccountGameCharacterDogAssignmentThis makes programs easier to understand, build, test, and fix.
Student s1 = new Student();
Student s1 = new Student();
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();
}
}
Alex is in grade 10
Maria is in grade 11
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.