Java OOP Fundamentals

Learn the building blocks of Object-Oriented Programming: classes, objects, constructors, and access modifiers.

This chapter introduces the foundational building blocks of Java’s Object-Oriented Programming (OOP) model: Classes, Objects, Constructors, the this keyword, static members, and Access Modifiers.

1. Class and Object

A class defines a blueprint for objects. An object is a real instance created from that blueprint.

public class Dog {

    String breed;

    public void bark() {
        System.out.println("Woof!");
    }

    public static void main(String[] args) {
        Dog myDog = new Dog();   // Creating an object
        myDog.bark();            // Calling method
    }
}

2. Constructors

A constructor initializes an object when it is created. It has the same name as the class and has no return type.

public class MyClass {

    int value;

    // Constructor
    public MyClass() {
        value = 10;
    }

    public static void main(String[] args) {
        MyClass obj = new MyClass();
        System.out.println("Value: " + obj.value);
    }
}

3. The this Keyword

The this keyword refers to the current object. It is commonly used when parameter names shadow instance variables.

public class Student {

    String name;

    public Student(String name) {
        this.name = name;  // Refers to instance variable
    }

    public static void main(String[] args) {
        Student s = new Student("Rahul");
        System.out.println("Student Name: " + s.name);
    }
}

4. The static Keyword

A static method or variable belongs to the class, not to individual objects. It can be accessed without creating an instance.

public class Counter {

    static int count = 0;

    public Counter() {
        count++;  // Shared across all objects
    }

    public static void main(String[] args) {

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

        System.out.println("Total objects created: " + Counter.count);
    }
}

5. Access Modifiers

Access modifiers define how a class or its members can be accessed.

ModifierAccessible Within ClassSame PackageSubclassOther Packages
publicYesYesYesYes
protectedYesYesYesNo
default (no modifier)YesYesNoNo
privateYesNoNoNo
Example
public class Employee {

    public String name = "John";      // Accessible anywhere
    private double salary = 50000;    // Only this class
    protected String department = "IT";

    public double getSalary() {       // controlled access
        return salary;
    }

    public static void main(String[] args) {
        Employee e = new Employee();
        System.out.println(e.name);
        System.out.println(e.getSalary());
        System.out.println(e.department);
    }
}

6. Object Lifecycle and Memory in Java

When you create an object in Java using the new keyword, the object is allocated memory in the heap. The reference variable is stored on the stack. Understanding this basic lifecycle helps avoid memory and logic errors.

An object goes through the following stages:

  • Creation – Memory is allocated and constructor is executed
  • Usage – Object is accessed using its reference
  • Eligibility for Garbage Collection – No references point to the object

Try example:

public class MemoryDemo {

    public static void main(String[] args) {

        Student s1 = new Student("Amit");
        s1 = null;   // Object eligible for garbage collection
    }
}

class Student {
    String name;
    Student(String name) {
        this.name = name;
    }
}

Note: Java automatically manages memory using Garbage Collection, so developers do not manually free memory.

7. Real-World Analogy of OOP

Object-Oriented Programming closely matches real-life thinking. For example, consider a Bank Account:

  • The class defines account rules
  • The object represents a customer’s account
  • Variables store balance and account number
  • Methods perform deposit and withdrawal

This structure makes Java programs easier to design, extend, and maintain — especially in large applications like banking, ERP systems, and e-commerce platforms.

8. Common Beginner Mistakes in Java OOP

  • Using too many static variables unnecessarily
  • Making all variables public
  • Not using constructors properly
  • Confusing class with object
  • Ignoring access modifiers

Following OOP principles from the beginning improves code quality and makes advanced concepts like inheritance and polymorphism much easier.

9. Static vs Instance Members (Very Important)

One of the most confusing topics for beginners is the difference between static members and instance members. Understanding this properly will prevent design mistakes later.

Static members belong to the class itself. Only one copy exists, shared across all objects.

Instance members belong to individual objects. Each object gets its own copy.

Try example:

public class Example {

    static int sharedCount = 0;   // static variable
    int individualCount = 0;      // instance variable

    Example() {
        sharedCount++;
        individualCount++;
    }

    public static void main(String[] args) {
        Example a = new Example();
        Example b = new Example();

        System.out.println(a.sharedCount);      // 2
        System.out.println(b.sharedCount);      // 2

        System.out.println(a.individualCount);  // 1
        System.out.println(b.individualCount);  // 1
    }
}

Use static members for constants, utility methods, counters, and shared configuration values.

10. Why Java Uses OOP in Real Applications

Java is widely used in enterprise software because Object-Oriented Programming helps manage large and complex systems.

In real-world applications like ERP systems, banking platforms, and Android apps:

  • Each module is represented as a class
  • Objects represent real business entities
  • Access modifiers protect sensitive data
  • Constructors ensure valid object creation

This approach improves maintainability, testing, and team collaboration. Large systems become easier to scale without rewriting existing code.

11. When OOP May Not Be the Best Choice

While OOP is powerful, it is not always required. Very small scripts, simple utilities, or one-time programs may not need full OOP design.

However, as soon as your application grows, uses multiple features, or handles real data, OOP becomes extremely beneficial.

Good developers choose the right level of abstraction instead of blindly applying patterns.

12. Summary

In this chapter, you learned the foundational pillars of Java OOP: classes, objects, constructors, the this keyword, static members, and access modifiers.

These concepts form the base for advanced topics such as inheritance, polymorphism, abstraction, and interfaces. Mastering them early will make your Java journey significantly smoother.

Next, we will explore how these concepts work together in real systems using OOP Core Concepts.