Java
Encapsulation
Object-Oriented Programming
Access Modifiers
Class Design

Access private field of another object in same class

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

In Java, a private field is private to the class, not private to one particular instance. That means code inside a class can access the private fields of any other instance of the same class.

This surprises people who read private as "only this object may see the field." Java does not define access that way. Access control is based on the declaring class.

What private Actually Means

When a field is declared private, only code written inside the same top-level class can access it directly. That rule applies whether the code is reading this.value or other.value.

A small example makes it clear:

java
1public class Account {
2    private int balance;
3
4    public Account(int balance) {
5        this.balance = balance;
6    }
7
8    public boolean hasMoreMoneyThan(Account other) {
9        return this.balance > other.balance;
10    }
11
12    public void transferTo(Account other, int amount) {
13        if (amount <= 0 || this.balance < amount) {
14            throw new IllegalArgumentException("invalid transfer");
15        }
16        this.balance -= amount;
17        other.balance += amount;
18    }
19}

Both hasMoreMoneyThan and transferTo access other.balance directly, and that is valid Java because the access happens inside the Account class.

Why The Language Allows This

Java's access modifiers are designed around encapsulating implementation details at the class boundary. If code in the same class could not access private fields of peer instances, common patterns such as copy constructors, equality checks, merges, and internal comparisons would become awkward.

For example, a copy constructor often needs to read private state from another instance:

java
1public class Point {
2    private int x;
3    private int y;
4
5    public Point(int x, int y) {
6        this.x = x;
7        this.y = y;
8    }
9
10    public Point(Point other) {
11        this.x = other.x;
12        this.y = other.y;
13    }
14}

That is still fully encapsulated because the access remains inside Point.

Equality And Comparison

Another common use case is implementing equals or helper comparison methods:

java
1import java.util.Objects;
2
3public class User {
4    private final String id;
5    private final String email;
6
7    public User(String id, String email) {
8        this.id = id;
9        this.email = email;
10    }
11
12    @Override
13    public boolean equals(Object obj) {
14        if (this == obj) return true;
15        if (!(obj instanceof User other)) return false;
16        return Objects.equals(this.id, other.id)
17            && Objects.equals(this.email, other.email);
18    }
19}

Direct access to other.id and other.email is both legal and idiomatic because the code is part of the same class.

What Is Still Forbidden

Code outside the class cannot access those private fields directly, even if it has another instance of the class:

java
1public class Demo {
2    public static void main(String[] args) {
3        Account a = new Account(100);
4        Account b = new Account(50);
5
6        // Not allowed:
7        // System.out.println(a.balance);
8    }
9}

That line fails because Demo is outside the Account class.

Common Pitfalls

The most common mistake is assuming privacy is instance-based rather than class-based. In Java, the access rule is tied to where the code is written, not which object the field belongs to.

Another pitfall is exposing getters only because developers think internal peer access is impossible. If the only reason for a getter is letting one instance read another instance's state inside the same class, that getter may be unnecessary.

A third issue is confusing Java with languages that enforce privacy differently. The exact rule varies across languages, so this behavior should not be assumed outside Java and similar class-based systems.

Summary

  • In Java, private means private to the declaring class.
  • Methods inside a class can read and write private fields on other instances of the same class.
  • This behavior supports copy constructors, comparisons, and internal state transfer.
  • Code outside the class still cannot access those fields directly.
  • The key idea is class-level encapsulation, not per-instance encapsulation.

Course illustration
Course illustration

All Rights Reserved.