惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
博客园 - 三生石上(FineUI控件)
小众软件
小众软件
人人都是产品经理
人人都是产品经理
V
Visual Studio Blog
Last Week in AI
Last Week in AI
V
V2EX
博客园_首页
IT之家
IT之家
Jina AI
Jina AI
博客园 - 叶小钗
The Cloudflare Blog
T
Tailwind CSS Blog
腾讯CDC
B
Blog
D
Docker
L
LangChain Blog
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
美团技术团队
Apple Machine Learning Research
Apple Machine Learning Research
爱范儿
爱范儿
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
GbyAI
GbyAI

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Why Objects Are Passed as Arguments in Java – Complete Gu...
Harini · 2026-05-29 · via DEV Community

Harini

In Java, objects are often passed as arguments to methods. This is an important concept in Object-Oriented Programming (OOP) because it helps methods work with real-world data efficiently.

Before understanding why objects are passed as arguments, let us first understand what an object is.


What is an Object in Java?

An object is an instance of a class.

A class is like a blueprint, and an object is the real implementation created from that blueprint.

Example:

class Student {
    String name = "Harini";
    int mark = 90;
}

Here:

  • Student is a class
  • name and mark are data members
  • An object can be created using the new keyword
Student obj = new Student();

Now obj contains the data and behavior of the Student class.


Why Do We Pass Objects as Arguments?

Objects are passed as arguments mainly for the following reasons:

  1. To access object data inside another method
  2. To reduce code complexity
  3. To modify object values
  4. To achieve code reusability
  5. To work with real-world applications efficiently

Let us understand each point in detail.


1. To Access Object Data Inside Another Method

When an object is passed to a method, the method can access all the variables and methods of that object.

Example

class Student {

    String name = "Harini";
    int mark = 90;
}

class Main {

    void display(Student s) {

        System.out.println("Student Name: " + s.name);
        System.out.println("Student Mark: " + s.mark);
    }

    public static void main(String[] args) {

        Student obj = new Student();

        Main m = new Main();

        m.display(obj);
    }
}

Output

Student Name: Harini
Student Mark: 90

Explanation

  • obj is an object of the Student class
  • The object is passed to the display() method
  • Inside the method, s receives the object reference
  • Using s.name and s.mark, we access the object's data

This allows methods to work directly with object information.


2. To Reduce Code Complexity

Instead of passing many individual variables, we can pass a single object.

Without Passing Object

display("Harini", 90, "Chennai", 12345);

If the class contains many fields, the method becomes difficult to manage.

With Passing Object

display(studentObj);

The entire data is available through the object.

Benefits

  • Cleaner code
  • Easy to read
  • Easy to maintain
  • Reduces parameter list size

3. To Modify Object Values

When an object is passed to a method, the method can modify the object's data.

Example

class Employee {

    int salary = 20000;
}

class Main {

    void increaseSalary(Employee e) {

        e.salary = 30000;
    }

    public static void main(String[] args) {

        Employee emp = new Employee();

        Main m = new Main();

        System.out.println("Before Increment: " + emp.salary);

        m.increaseSalary(emp);

        System.out.println("After Increment: " + emp.salary);
    }
}

Output

Before Increment: 20000
After Increment: 30000

Explanation

  • The original object is passed to the method
  • The method changes the salary
  • Since both references point to the same object, the original value changes

How Java Passes Objects

This is one of the most important interview concepts.

Java does NOT pass the actual object.

Java passes the reference of the object by value.


What Does "Reference by Value" Mean?

When an object is created:

Student obj = new Student();

  • obj stores the memory address (reference) of the object
  • When passed to a method, Java copies this reference

Example:

display(obj);

Now:

  • obj and method parameter s point to the same object in memory

That is why object data can be modified inside methods.


Memory Representation

obj  --------->  Student Object
                     name = Harini
                     mark = 90

s    --------->  Same Student Object

Both references point to the same object.