Master Core Java Programming From Scratch

Clear, interactive, and structured coding lessons designed for absolute beginners.

Classes & Objects in Java

Learn the foundation of object-oriented programming in Java: classes, objects, fields, methods, and object creation.

What is Object-Oriented Programming?

Object-Oriented Programming, or OOP, is a programming approach that organizes software around objects containing state and behavior.

Java is primarily an object-oriented programming language. Classes define the structure and behavior of objects, while objects represent actual instances created from those classes.

What is a Class?

A class is a blueprint or template that defines the data and behavior that objects of that class can have.

Java
class Student {

    String name;
    int age;

    void study() {

        System.out.println(name + " is studying");

    }

}

Here, Student is a class containing two fields and one method.

What is an Object?

An object is an instance of a class. It contains its own state and can use the behavior defined by its class.

Java
Student student = new Student();

The new keyword creates an object, and the variable student stores a reference to that object.

Class vs Object

Class Object
Blueprint or template. Actual instance of a class.
Defines fields and methods. Contains actual state.
Does not represent one specific entity. Represents a specific entity.
Example: Student Example: student

Fields

Fields are variables declared inside a class. They represent the state or properties of an object.

Java
class Employee {

    String name;
    int age;
    double salary;

}

Methods

Methods define behavior that an object or class can perform.

Java
class Employee {

    String name;

    void work() {

        System.out.println(name + " is working");

    }

}

Complete Class Example

Java
class Student {

    String name;
    int age;

    void displayDetails() {

        System.out.println("Name: " + name);
        System.out.println("Age: " + age);

    }

}

Creating Objects

Java
public class Main {

    public static void main(String[] args) {

        Student student = new Student();

        student.name = "Samadhan";
        student.age = 22;

        student.displayDetails();

    }

}
Output
Name: Samadhan
Age: 22

Multiple Objects

A single class can be used to create many independent objects.

Java
Student first = new Student();

first.name = "Sejal";
first.age = 21;


Student second = new Student();

second.name = "Pradnya";
second.age = 18;

Both objects have the same structure but maintain their own field values.

Object References

Variables that store objects normally contain references to those objects rather than the objects themselves.

Java
Student student = new Student();

student.name = "Sejal";
Concept
student
   │
   │ reference
   ↓
┌────────────────────┐
│ Student Object     │
│ name = "Sejal"     │
│ age  = 0           │
└────────────────────┘

null Object Reference

A reference variable can contain null, meaning it does not currently refer to an object.

Java
Student student = null;
Accessing an instance member through a null reference can result in NullPointerException.

Accessing Class Members

The dot operator . is used to access accessible fields and methods through an object reference.

Java
Student student = new Student();

student.name = "Sejal";

student.displayDetails();

Default Values of Instance Fields

Instance fields receive default values when an object is created if they are not explicitly initialized.

Type Default Value
byte, short, int, long 0
float, double 0.0
char \u0000
boolean false
Reference types null

Instance Members

A non-static field or method belongs to an object instance. Each object can have different instance-field values.

Java
class Car {

    String color;

    void displayColor() {

        System.out.println(color);

    }

}

Car car1 = new Car();
Car car2 = new Car();

car1.color = "Red";
car2.color = "Blue";

car1.displayColor();
car2.displayColor();
Output
Red
Blue

Static Members

A static field belongs to the class rather than to each individual object.

Java
class Counter {

    static int count = 0;

    Counter() {

        count++;

    }

}

public class Main {

    public static void main(String[] args) {

        new Counter();
        new Counter();
        new Counter();

        System.out.println(Counter.count);

    }

}
Output
3

The this Keyword

The this keyword refers to the current object. It is commonly used when a parameter has the same name as an instance field.

Java
class Student {

    String name;

    void setName(String name) {

        this.name = name;

    }

}

State and Behavior

Concept Example
State Fields such as name and age.
Behavior Methods such as study() and displayDetails().

Object Creation Flow

Concept
Class definition
       ↓
new keyword
       ↓
Object created
       ↓
Constructor executes
       ↓
Reference returned
       ↓
Object can be used

Constructors are discussed in detail in the next lesson.

Example: CIIT Student👨‍🏫🤓

Consider a student-management example at CIIT Institute. The CIITStudent class represents a student enrolled in a course and stores student details and pending course fees.

Java
class CIITStudent {

    String studentName;
    String courseName;
    double pendingFees;

    void payFees(double amount) {
        pendingFees -= amount;
        System.out.println("Rs. " + amount + " successfully paid for " + courseName + " course.");
    }

    void displayFeeStatus() {
        System.out.println("\n--- CIIT Institute Fee Receipt ---");
        System.out.println("Student Name : " + studentName);
        System.out.println("Course Name  : " + courseName);
        System.out.println("Pending Fees : Rs. " + pendingFees);
        System.out.println("----------------------------------");
    }
}

public class Main {

    public static void main(String[] args) {

        CIITStudent student = new CIITStudent();

        student.studentName = "Sam";
        student.courseName = "Core Java";
        student.pendingFees = 15000.0;

        student.displayFeeStatus();

        System.out.println("\n[Action: Student pays installment]");

        student.payFees(5000.0);

        student.displayFeeStatus();
    }
}

Output

Output
--- CIIT Institute Fee Receipt ---
Student Name : Sam
Course Name  : Core Java
Pending Fees : Rs. 15000.0
----------------------------------

[Action: Student pays installment]
Rs. 5000.0 successfully paid for Core Java course.

--- CIIT Institute Fee Receipt ---
Student Name : Sam
Course Name  : Core Java
Pending Fees : Rs. 10000.0
----------------------------------

Line-by-Line Explanation

Code Explanation
class CIITStudent Defines a class named CIITStudent. The class represents a student at CIIT Institute.
String studentName; Stores the name of the student.
String courseName; Stores the name of the course in which the student is enrolled.
double pendingFees; Stores the remaining course fees. The double type allows decimal values.
void payFees(double amount) Defines the payFees() method. It accepts the amount paid by the student.
pendingFees -= amount; Subtracts the paid amount from the student's pending fees.
void displayFeeStatus() Defines a method that displays the student's current fee information.
CIITStudent student = new CIITStudent(); Creates a new CIITStudent object. The object is stored in the student reference.
student.studentName = "Sam"; Assigns Sam as the student's name.
student.courseName = "Core Java"; Assigns Core Java as the student's course.
student.pendingFees = 15000.0; Sets the initial pending course fees to Rs. 15,000.
student.displayFeeStatus(); Calls the method that displays the student's current fee receipt.
student.payFees(5000.0); Calls payFees() and passes Rs. 5,000 as the payment amount.
pendingFees -= amount; Changes the pending fee from Rs. 15,000 to Rs. 10,000.
student.displayFeeStatus(); Displays the updated fee status after the installment payment.
OOP Concepts Used
  • Class: CIITStudent acts as a blueprint for student objects.
  • Object: student is an object created from the CIITStudent class.
  • Fields: studentName, courseName, and pendingFees store student data.
  • Methods: payFees() and displayFeeStatus() define the behavior of the student object.

Best Practices

  • Use meaningful class names such as Student, Employee, or BankAccount.
  • Follow Java naming conventions.
  • Keep one clear responsibility for each class.
  • Prefer private fields with controlled access in production-oriented object models.
  • Avoid unnecessary static state.

Interview Questions

A class is a blueprint that defines fields and behavior for objects.

An object is an instance of a class created at runtime.

A class defines the structure and behavior; an object is a concrete instance created from that class.

The new expression creates an object and returns a reference to it.
Summary

Classes define the structure and behavior of Java objects. Objects are instances created from classes. Fields represent state, methods represent behavior, and references allow programs to work with objects.