Java Classes and Objects Explained Simply

Mastering the Basics of Object-Oriented Programming in Java

If you're new to Java programming, one of the first concepts you’ll come across is classes and objects. These two terms are fundamental building blocks of Java and the entire concept of object-oriented programming (OOP).

Java Classes and Objects Explained Simply

While these may sound technical at first, understanding them is crucial because they allow you to build real-world applications with ease, modularity, and reusability.

In this guide, we’ll break down Java classes and objects in simple terms, using easy examples and code snippets. Whether you’re preparing for interviews, learning Java from scratch, or brushing up your OOP skills, this article is for you.

What Is Object-Oriented Programming?

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of “objects”. These objects contain both data (fields or attributes) and behavior (methods).

Java is a fully object-oriented language, meaning everything in Java is associated with classes and objects.

What Is a Class in Java?

class is like a blueprint or template for creating objects. It defines the properties (fields) and behaviors (methods) that the object created from the class will have.

Real-World Analogy:

Imagine a Car. A car has:

  • Properties: color, speed, model, brand
     
  • Behaviors: drive(), brake(), honk()
     

Java class for a Car would define these properties and behaviors.

Basic Syntax of a Class

java

CopyEdit

Copy Code

public class Car {

    // Fields (properties)

    String brand;

    int speed;

    // Method (behavior)

    void drive() {

        System.out.println("The car is driving.");

    }

}

This class doesn't do anything on its own. To make it come to life, we need to create an object of this class.

What Is an Object in Java?

An object is an instance of a class. It is the actual entity that exists in memory and performs actions defined in the class.

Using the Car blueprint, you can create multiple cars (objects), each with different values.

Creating an Object in Java

java

CopyEdit

Copy Code

public class Main {

    public static void main(String[] args) {

        // Creating an object

        Car myCar = new Car();

        // Assign values to object fields

        myCar.brand = "Toyota";

        myCar.speed = 100;

        // Call method

        myCar.drive();

    }

}

Output:

csharp

CopyEdit

The car is driving.

Here, myCar is an object of the Car class. We gave it a brand and speed and used its drive() method.

Key Concepts Around Classes and Objects

Let’s now look into a few important OOP terms in Java that relate to classes and objects.

1. Fields or Attributes

These are variables defined in the class that hold state or data about the object.

java

CopyEdit

String brand;

int speed;

2. Methods

Methods are functions inside a class that define the behavior of the objects.

java

CopyEdit

Copy Code

void drive() {

    System.out.println("Driving...");

}

3. Constructor

constructor is a special method that is called automatically when an object is created. It’s used to initialize object values.

java

CopyEdit

Copy Code

public class Car {

    String brand;

    int speed;

    // Constructor

    Car(String b, int s) {

        brand = b;

        speed = s;

    }

    void display() {

        System.out.println("Brand: " + brand + ", Speed: " + speed);

    }

}

java

CopyEdit

Copy Code

public class Main {

    public static void main(String[] args) {

        Car myCar = new Car("Honda", 120);

        myCar.display();

    }

}

Output:

yaml

CopyEdit

Brand: Honda, Speed: 120

4. new Keyword

Used to create objects from a class.

java

CopyEdit

Car myCar = new Car("Tesla", 150);

5. Object Reference

The variable (myCar) does not store the object itself; it stores a reference (memory address) to the object.

Why Use Classes and Objects?

Using classes and objects allows us to:

  • Model real-world entities in code
     
  • Encapsulate data and behavior
     
  • Reuse code through object instantiation
     
  • Organize complex systems easily
     

Examples of Java Classes and Objects

Let’s explore a few more examples to strengthen the concept.

Example 1: Student Class

java

CopyEdit

Copy Code

public class Student {

    String name;

    int roll;

    void displayInfo() {

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

        System.out.println("Roll No: " + roll);

    }

}

java

CopyEdit

Copy Code

public class Main {

    public static void main(String[] args) {

        Student s1 = new Student();

        s1.name = "Arpita";

        s1.roll = 101;

        s1.displayInfo();

    }

}

Example 2: BankAccount Class with Constructor

java

CopyEdit

Copy Code

public class BankAccount {

    String holderName;

    double balance;

    // Constructor

    BankAccount(String name, double initialBalance) {

        holderName = name;

        balance = initialBalance;

    }

    void deposit(double amount) {

        balance += amount;

        System.out.println("Deposited: " + amount);

    }

    void displayBalance() {

        System.out.println("Balance for " + holderName + ": " + balance);

    }

}

java

CopyEdit

Copy Code

public class Main {

    public static void main(String[] args) {

        BankAccount account1 = new BankAccount("John", 5000);

        account1.deposit(1000);

        account1.displayBalance();

    }

}

Encapsulation: One of the Four Pillars of OOP

Encapsulation means keeping data private to the class and exposing it only via methods. This protects data and ensures only valid operations happen.

java

CopyEdit

Copy Code

public class Person {

    private String name;

    public void setName(String newName) {

        name = newName;

    }

    public String getName() {

        return name;

    }

}

Interview-Style Questions

Q1: Can we create objects without using new keyword?

Yes, via cloning, deserialization, or using reflection, but new is the most common and preferred method.

Q2: What happens if we don’t define a constructor?

Java provides a default constructor that initializes object fields to default values.

Q3: What is the default value of an object’s field?

  • int → 0
     
  • boolean → false
     
  • Object references → null

Learn Java OOP with Uncodemy

Want to learn Java with real-world projects and instructor-led training?

Check out the Core Java Programming Course by Uncodemy.

Course Highlights:

  • OOP concepts explained with practical examples
     
  • Live projects for classes, inheritance, polymorphism, etc.
     
  • Java for interviews and certifications
     

Whether you’re a student or aspiring software developer, this course will strengthen your foundation in Java.

Conclusion

Understanding classes and objects is the first and most important step in becoming a skilled Java developer. It allows you to write clean, modular, and reusable code that mirrors real-world systems.

Let’s recap:

  • Class = blueprint
     
  • Object = instance of class
     
  • You define fields and methods in a class and use them via object creation
     
  • Constructors help initialize data
     
  • Java supports full encapsulation, abstraction, and modularity
     

By practicing with multiple real-world examples, you’ll soon become fluent in writing object-oriented programs in Java.

Placed Students

Our Clients

Partners

...

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses