Classes and Objects in Java
Subject: Data Structures and Algorithms · Language: Java · Level: beginner · 6 min read
Understand classes, objects, fields, methods, constructors, and how Java represents real-world entities.
Java is an object-oriented programming language. That means Java programs are mainly built using classes and objects. A class is a blueprint, and an object is a real item created from that blueprint. For example, `Student` can be a class, and Rahul, Priya, or Amit can be objects of that class.
This idea helps us represent real-world things in code. A student has data such as name and marks, and a student can also perform actions such as displaying details or checking pass status.
What Is a Class?
A class defines the structure and behavior of an object. Structure means what data the object will store. Behavior means what actions the object can perform.
A class can contain variables, also called fields, and methods, which are used to perform actions. Fields describe the object, and methods define what the object can do.
```java
class Student {
String name;
int marks;
}
```
Here, `Student` is a class. It has two fields: `name` and `marks`. This class does not store one student by itself; it only defines what every student object should have.
What Is an Object?
An object is created from a class using the `new` keyword. When an object is created, Java allocates memory for the fields defined in the class.
```java
Student s1 = new Student();
s1.name = "Rahul";
s1.marks = 85;
```
Here, `s1` is an object of the `Student` class. The dot operator is used to access fields and methods of an object. So, `s1.name` means the `name` field of object `s1`.
You can create many objects from the same class. Each object gets its own copy of fields.
```java
Student s2 = new Student();
s2.name = "Priya";
s2.marks = 92;
```
Now `s1` and `s2` are two different student objects with different data.
Fields and Methods
Fields store object data. Methods define actions related to that object.
```java
class Student {
String name;
int marks;
void display() {
System.out.println(name + " scored " + marks);
}
}
``` Here, `name` and `marks` are fields, while `display()` is a method. The method uses the object's own data to print details.
Access Modifiers
Access modifiers decide where a field, method, or class member can be used from. They help control visibility and protect data. `public` means the member can be accessed from outside the class. `private` means the member can be used only inside the same class. `protected` is mainly used with inheritance, and default access works inside the same package.
For beginners, the most important habit is to keep data private when it should not be changed directly, and provide methods when controlled access is needed. This idea connects directly with encapsulation.
Constructor
A constructor is a special block used to initialize an object when it is created. It has the same name as the class and does not have a return type. Constructors help us give starting values to fields. Without a constructor, we may need to assign each field separately after creating the object.
this Keyword
`this` refers to the current object. It is commonly used when constructor parameters and class fields have the same name.
```java
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
```
Here, `this.name` means the field of the current object, and `name` means the parameter received by the constructor.
Example Program
```java
class Student {
String name;
int marks;
Student(String name, int marks) {
this.name = name;
this.marks = marks;
}
} public class Main {
public static void main(String[] args) {
Student s1 = new Student("Rahul", 85); System.out.println("Name = " + s1.name);
System.out.println("Marks = " + s1.marks);
}
}
``` This program creates a `Student` class with two fields and one constructor. In `main`, an object is created using `new Student("Rahul", 85)`, and the constructor stores those values inside the object.
Classes and Objects in DSA
In beginner DSA, many problems use simple arrays and variables. But classes become useful when one item has multiple related values. For example, a `Student` may have name, roll number, and marks. A `Node` in a linked list has data and a reference to the next node. A `Pair` can store two values together. These are all natural uses of classes and objects.
Practice Question
Create a `Book` class with title, author, and price. Add a constructor to initialize these values, create one object in `main`, and print its details.