What Are Classes?
Imagine you have a blueprint for a garage. This blueprint shows the design, structure, and key features of the garage. But each garage built from that blueprint can have unique additions—like different paint colors or extra storage space.
In Java, a class is like that blueprint, and the actual garage you build is called an object. For example, if you have a class called Garage
, you can create objects like “Garage1” or “Garage2” based on it. These objects are called “instances of the class Garage
.”
There are two main types of classes:
- Custom Classes: The ones you create yourself.
- Predefined Classes: These are already built by other programmers and included in Java’s libraries. You can use these classes in your project by importing them (Lesson 3: What Does “Import” Mean in Java?).
Example: A Class and an Object
Here’s an
example
from a lesson called
Lesson 2: The Power of Pseudocode:
Scanner scanner = new Scanner(System.in);
Let’s break it down:
Scanner
is a predefined class in the Java library.scanner
is an object created from that class. It’s a specific instance that we can use in our code.
Think of Scanner
as a universal tool designed by Java developers to make it easier to handle input (like typing on the keyboard). Meanwhile, the object scanner
is your personal tool, created for this specific task in your program.
To use a predefined class like Scanner
, you need to tell your program where to find it by writing an import statement at the top of your code:
import java.util.Scanner;
What Are Objects?
Objects are created from a class to represent individual items or concepts in your program. Think of objects as real-world things, like students. Every student has certain properties (like a name and a grade level) and can perform actions (like studying or graduating).
Let’s say we define a class Student
with the following structure:
class Student {
String name;
int gradeLevel;
}
Here, the blueprint specifies that every student will have:
- A
name
(e.g., “Anna” or “Liam”). - A
gradeLevel
(e.g., 10th grade or 12th grade).
Now we can create objects (students) based on this class:
Student student1 = new Student(); student1.name = "Anna"; student1.gradeLevel = 10; Student student2 = new Student(); student2.name = "Liam"; student2.gradeLevel = 12;
Why Use Classes and Objects?
At this point, you might ask: “Why go through all this trouble? Isn’t this more work?”
For example, this code:
Student student1 = new Student();
student1.name = "Anna";
student1.gradeLevel = 10;
is longer than:
String student1Name = "Anna";
int student1GradeLevel = 10;
You’re right! For a single example, it might seem unnecessary. But the real power of classes and objects shines in larger programs. Imagine you have 500 students. Would you want to write:
String student1Name = "Anna";
int student1GradeLevel = 10;
String student2Name = "Liam";
int student2GradeLevel = 12;
// And so on...
That’s a lot of repetitive work! Instead, with classes, you can create all your students using just a few lines of code:
Student[] students = new Student[500];
for (int i = 0; i < students.length; i++) {
students[i] = new Student();
students[i].name = "Student" + (i + 1);
students[i].gradeLevel = 9; // Set a default grade level
}
Now you have 500 students, and you can easily:
- Print all their names.
- Change their grade levels.
- Find a specific student by name.
Understanding the Term “Object”
A quick note: The term “object” can be a bit confusing for beginners because it’s used in two ways:
- Object as a programming concept: An instance created from a class.
- Object in a general sense: A thing or item in the real world or in your program. For example, you might call a block of code an “object” informally, but it’s not the same as a class-based object.
To clarify:
- In the real world: An object is a “thing” like a car, a chair, or a phone. Each has properties (like color or size) and behaviors (like driving, reclining, or ringing).
- In programming: An object is a digital representation of a real-world thing, created from a blueprint (class). For example:
- A class is the blueprint of a car.
- An object is a specific car, built using that blueprint.
Back to the Example
In this code:
Scanner scanner = new Scanner(System.in);
scanner
is the object. You can name it anything you like, just like naming a variable. For instance:
Scanner keyboardInput = new Scanner(System.in);
This name makes it clear that the object is used for keyboard input. Similarly, you can create another object for reading from a file:
Scanner fileInput = new Scanner(file);
Both objects (keyboardInput
and fileInput
) use the same Scanner
class, saving you from rewriting code for each new task.
Why Are We Learning This?
At this stage, it might feel like classes and objects add unnecessary complexity. But in real-world programming, they save you time and effort by:
- Making your code reusable and efficient.
- Allowing you to manage hundreds (or thousands) of items with ease.
- Helping you write cleaner and more organized programs.
Even if this feels a bit abstract now, understanding the class-object relationship is a fundamental concept that will help you as you build more advanced programs.
I hope this explanation makes things a bit clearer! Programming is like learning to ride a bike—it feels tricky at first, but soon, it’ll feel natural. Keep practicing, and you’ll get there!