Tutorials Logic, IN info@tutorialslogic.com

OOP Basics in Java Classes, Objects, Constructors

Class, Object, Field, and Method

Java objects combine state with behavior defined by a class. Constructors establish valid state, methods protect invariants, and encapsulation prevents unrelated code from changing fields arbitrarily.

Classes define state and behavior; objects carry concrete state whose validity should be established by constructors and protected by focused methods.

A field stores object state. A method defines behavior. Creating an object with new allocates an instance and lets you call its methods.

Class and Object

Class and Object
class Book {
    String title;
    String author;

    void printDetails() {
        System.out.println(title + " by " + author);
    }
}

public class ObjectDemo {
    public static void main(String[] args) {
        Book book = new Book();
        book.title = "Effective Java";
        book.author = "Joshua Bloch";
        book.printDetails();
    }
}

Constructors and this

A constructor initializes a new object. The this keyword refers to the current object and is often used to distinguish fields from parameters.

Constructor

Constructor
class Employee {
    private String name;
    private double salary;

    Employee(String name, double salary) {
        this.name = name;
        this.salary = salary;
    }

    void printSalary() {
        System.out.println(name + ": " + salary);
    }
}

Object Identity vs State

Two objects can have the same field values but still be different objects in memory. Identity is about which object it is; state is about what values it holds.

Identity Example

Identity Example
class Point {
    int x;
    int y;
}

public class IdentityDemo {
    public static void main(String[] args) {
        Point p1 = new Point();
        Point p2 = new Point();
        p1.x = p2.x = 10;
        p1.y = p2.y = 20;

        System.out.println(p1 == p2); // false
    }
}

Object State and Behavior

A class is a blueprint and an object is a runtime instance created from that blueprint. Fields hold state, methods define behavior, and constructors prepare the object before it is used.

  • Use constructors for required initial state.
  • Use methods to change state safely.
  • Use this to refer to the current object.
  • Keep each class focused on one responsibility.

Thinking in classes, objects, state, and behavior

OOP basics in Java begin with a simple question: what thing does the program need to represent? A class is the blueprint for that thing, an object is one actual instance, fields store its state, and methods describe what it can do. This makes programs easier to organize than keeping unrelated variables and functions scattered everywhere.

Constructors matter because they create objects in a valid starting state. For example, a Student object should probably have a name before it is used. Methods should then operate on that object's own data. When this relationship between state and behavior is clear, later OOP topics become much easier.

  • Class means blueprint; object means actual instance.
  • Fields hold object state.
  • Constructors prepare new objects.
  • Methods should express useful behavior, not just expose every field.

Student object with state and behavior

Student object with state and behavior
class Student {
    String name;
    int marks;

    Student(String name, int marks) {
        this.name = name;
        this.marks = marks;
    }

    boolean passed() {
        return marks >= 40;
    }
}
Before you move on

OOP Basics in Java Classes, Objects, Constructors Mastery Check

4 checks
  • Creating an object with new allocates an instance and lets you call its methods.
  • The this keyword refers to the current object and is often used to distinguish fields from parameters.
  • Two objects can have the same field values but still be different objects in memory.
  • Identity is about which object it is; state is about what values it holds.

Object Model Boundary

  • Mutable state without invariants

    Avoid public fields that let callers create an invalid object. Validate constructor and method inputs and expose behavior instead of unrestricted state changes.

Core Java Questions Learners Ask

It should leave the new object in a valid state.

Private fields prevent callers from bypassing the rules enforced by the class.

No. Classes can also represent services, commands, policies, and technical boundaries.

Browse Free Tutorials

Explore 500+ free tutorials across 20+ languages and frameworks.