Loading...

Classes and Objects in Java

A Simple Thought

When you look around, everything has properties and actions. A car has a color, brand, and speed, and it can start or stop. A student has a name, roll number, and marks, and can study or take exams.

In Java, you can represent real-world things in the same way by creating classes and objects.
A class defines what something is, and an object is a real version of that class in action.

In this lesson, you will learn how to define a class and create objects from it.

The Project: Student Information System

You will create a small program that stores and displays information about a student. The program will define a class named Student with attributes for name and grade. Then, it will create an object of that class and print the details.

Your output should look like this:

text
5 lines
|
26/ 500 tokens
1
2
3
4
5
========================
   STUDENT INFORMATION
   Name: Ali Khan
   Grade: 10
========================

The program will use a class to describe what a student is and an object to represent a specific student.

The Concept: Classes and Objects

A class is a blueprint that defines the structure and behavior of something.
It can include variables (called fields) and methods (called functions).

An object is a real instance of that class.
When you create an object, you give life to the class.

Here’s a simple example:

java
4 lines
|
13/ 500 tokens
1
2
3
4
class Student {
    String name;
    int grade;
}
Code Tools

This defines a class called Student with two fields: name and grade.
You can now create an object from this class in another part of your program:

java
3 lines
|
16/ 500 tokens
1
2
3
Student s1 = new Student();
s1.name = "Ali Khan";
s1.grade = 10;
Code Tools

Here, s1 is an object, and you’re assigning data to its fields.

Why This Matters

Classes and objects are at the heart of object-oriented programming, which is one of the most powerful ideas in software development.
They help you:

  • Model real-world things naturally
  • Reuse code easily
  • Keep large programs organized

For example, a school management system can have classes for students, teachers, and courses.
Each class defines what the thing is, and each object represents one real example of it.

Let’s Build the Student Program

Here’s the complete code:

java
19 lines
|
123/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Student {
    String name;
    int grade;
}

public class StudentInfo {
    public static void main(String[] args) {
        System.out.println("========================");
        System.out.println("   STUDENT INFORMATION");

        Student s1 = new Student();
        s1.name = "Ali Khan";
        s1.grade = 10;

        System.out.println("   Name: " + s1.name);
        System.out.println("   Grade: " + s1.grade);
        System.out.println("========================");
    }
}
Code Tools

When you run this program, it prints the student’s information using an object created from the Student class.
The class defines the data structure, and the object stores the actual information.

How It Works

  1. The Student class defines two variables: name and grade.
  2. The main method creates an object called s1 using new Student().
  3. The program assigns values to s1.name and s1.grade.
  4. Finally, it prints those values using the object’s data.

This is the foundation of building real-world models in programming.

Learn Together with AI

If you are using an AI Copilot, you can explore how classes and objects work by giving it small, focused tasks.

Try this prompt:
“Write a Java program that defines a class named Car with fields for brand and color, and print the details of one car.”

Then ask for changes like:

  • “Add another car object with different details.”
  • “Add a method that prints all car details in one line.”
  • “Add a new field for model year and display it too.”

You’ll quickly see how easy it is to model real-world things with classes and objects.

Practice Challenge

Create a program that defines a class called Book with fields for title and author.
Then create an object of that class and print the information as shown below:

text
5 lines
|
30/ 500 tokens
1
2
3
4
5
========================
   BOOK INFORMATION
   Title: The Java Journey
   Author: Maria Ahmed
========================

Example Solution

java
19 lines
|
127/ 500 tokens
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class Book {
    String title;
    String author;
}

public class BookInfo {
    public static void main(String[] args) {
        System.out.println("========================");
        System.out.println("   BOOK INFORMATION");

        Book b1 = new Book();
        b1.title = "The Java Journey";
        b1.author = "Maria Ahmed";

        System.out.println("   Title: " + b1.title);
        System.out.println("   Author: " + b1.author);
        System.out.println("========================");
    }
}
Code Tools

When you run this program, the output will match the format above.
By creating a class and object, you’ve modeled a real-life concept — a book — inside your program.

Coming Up Next

You now know how to define a class and create objects in Java.
Next, you will learn how to use constructors, which are special methods that let you set up an object automatically when it is created.