Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6751369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T12:55:08+00:00 2026-05-26T12:55:08+00:00

From what I understand with Java, everything is pass by value. However, when you

  • 0

From what I understand with Java, everything is pass by value. However, when you begin talking about objects which contain other objects things get a bit interesting and i’d like to be able to communicate this better and make sure I fully understand as well. So my take here is that only the primitives within the object are actually copied, on the other hand any objects it contains…the values of those objects are not copied, rather only are copies of those references.

So if we had some object with these members

public class SomeObject {
   private String name;
   private String address;
   private  List<String> friends;

   public SomeObject(String name, String address, List<String> friends) {
      this.name = name;
      this.address = address;
      this.friends = friends;
   }
}

And we did this:

List<String> friendsList = new ArrayList<String>();
        friendsList.add("bob");
        friendsList.add("joe");

        SomeObject so1 = new SomeObject("mike", "123 street", friendsList);

        friendsList.add("zoe");
        friendsList.add("rick");
        SomeObject so2 = new SomeObject("david", "300 ave", friendsList);

        System.out.println(so1.getName() + " " + so1.getAddress());
        for (String friend : so1.getFriends()) {
            System.out.println(friend);
        }

        System.out.println(so2.getName() + " " + so2.getAddress());
        for (String friend : so2.getFriends()) {
            System.out.println(friend);
        }

Our output is this:

mike 123 street
bob
joe
zoe
rick
david 300 ave
bob
joe
zoe
rick

Even though we have created 2 separate objects, they both maintain a reference to the original friendslist. This is why I get a bit tripped up when people say Java is always pass by value.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T12:55:09+00:00Added an answer on May 26, 2026 at 12:55 pm

    Java is passing always parameters by values. In your case the object reference is passed by value. See this article that explain how and why.

    http://javadude.com/articles/passbyvalue.htm

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a multi-threaded Java web crawler. From what I understand of the web,
I am trying to understand how the following code ties into two other .java
I can't understand why Java's HttpURLConnection does not follow an HTTP redirect from an
One thing I don't understand about Scala, is why Null subclasses everything, despite not
I am using DBus in a project. I understand from DBus specification that for
From what I understand, the parent attribute of a db.Model (typically defined/passed in the
From what I understand, due to the same origin policy enforcement in current browsers,
From what I understand, in TDD you have to write a failing test first,
From what I understand of the SDK, this exception is raised when the bindings
From what I understand of REST principles, URLs should represent a single resource, like

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.