1. Introduction
Classes and objects are the building blocks of object-oriented programming (OOP) in Java. Whether you’re modeling a car, a user, or a game character, you’ll use classes and objects to define and interact with these real-world entities. This tutorial will teach you the main differences between a class and an object.
2. What is a Class?
A class is a blueprint or template that defines the structure and behavior of objects. It contains fields (attributes) and methods (behaviour) that describe what an object can hold and do.
In this example, Car is the class name. Its has brand
and speed
as fields; accelerate()
, and decelerate()
as methods.
public class Car {
String brand;
int speed;
void accelerate() {
speed += 10;
}
void decelerate() {
speed -= 10;
}
}
Storing a Java class on Disk
In Java, each class is stored in a file with .java extension. The previous Car class will therefore be stored in a file named Car.java on disk.
However, you can store many classes in a single .java file. The rule is to ensure only one of those classes is public, and the public class must match the filename.
//Filename : Student.java
public class Student {
String name;
int age;
List<Course> courses;
public Student(String name, int age) {
this.name = name;
this.age = age;
this.courses = new ArrayList<>();
}
void addCourse(String title) {
Course course = new Course(title);
courses.add(course);
}
}
class Course {
String title;
Course(String title) {
this.title = title;
}
}
As you can see, the file Student.java holds two classes: Student and Course. Student is the only public class in the file.
3. What is an Object?
An object is an instance of a class. When you create an object, Java allocates memory and sets it up based on the class definition.
In the following example, myCar
is an object created from the Car
class using the new
operator.
Car myCar = new Car();
myCar.brand = "Toyota";
myCar.speed = 50;
myCar.accelerate();
System.out.println(myCar.speed); // Output: 60
Each object can have different values, but they share the structure defined by the class.
Car firstCarObject = new Car();
firstCarObject.brand = "Toyota";
firstCarObject.speed = 50;
firstCarObject.accelerate();
System.out.println(firstCarObject.speed); // Output: 60
Car secondCarObject = new Car();
secondCarObject.brand = "Hyundai";
secondCarObject.speed = 50;
secondCarObject.decelerate();
System.out.println(secondCarObject.speed); // Output: 40
4. Constructors
A constructor is a special method that runs when an object is created. It can initialize fields with specific values.
public class Car {
String brand;
int speed;
// Constructor
public Car(String brand, int speed) {
this.brand = brand;
this.speed = speed;
}
}
To create a new object using the two-argument constructor above, simply pass the arguments to the method as follows:
Car car1 = new Car("Honda", 40);
The Default constructor
Java also provides a default constructor if you don’t write one. However, if you define a constructor with field parameters, you MUST explicitly define the no-argument constructor.
Based on the previous definition of the Car class, the following code will not compile:
Car bad = new Car(); //won't compile
You should explicitly provide the default constructor to fix this:
// Default Constructor
public Car(){
this("Toyota",10);
}
5. Encapsulation and Access Modifiers
Encapsulation is the practice of keeping fields private and providing access through public methods(accessors). This helps protect the internal state of an object.
public class Car {
private int speed;
public int getSpeed() {
return speed;
}
public void setSpeed(int speed) {
if (speed >= 0) {
this.speed = speed;
}
}
}
- Using
private
hides the field, whilepublic
methods expose safe access. The methodsgetSpeed()
andsetSpeed()
are called accessors. The methodgetSpeed()
is a getter, whilesetSpeed()
is a setter. - The keywords public and private are called access modifiers.
6. Conclusion
Classes define structure; objects bring them to life. Together, they form the foundation of Java programming. Understanding these core concepts is the first step toward building maintainable, scalable Java applications.
You can find the complete code of this article here in GitHub.
Pingback: Enum Type In Java