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 376013
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T14:33:29+00:00 2026-05-12T14:33:29+00:00

I stumbled upon a very puzzling feature(?) of Java. It seems that using a

  • 0

I stumbled upon a very puzzling feature(?) of Java.

It seems that using a “new” keyword to replace a method argument kind of shifts that object into a different scope:

import java.util.ArrayList;

public class Puzzle {
    public static void main(String[] args) {
        ArrayList<Integer> outer = new ArrayList<Integer>();
        outer.add(17);
        Puzzle.change(outer);
        outer.add(6);
        System.out.println(outer);

        // excpected output:
        // [23]
        // [23, 6]
        //
        // actual output:
        // [23]
        // [17, 7, 6]
    }

    public static void change(ArrayList<Integer> inner) {
        inner.add(7);
        inner = new ArrayList<Integer>();
        inner.add(23);
        System.out.println(inner);
    }
}

Can anyone explain this oddity? I noticed the same kind of behavior with assignment.

  • 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-12T14:33:29+00:00Added an answer on May 12, 2026 at 2:33 pm

    This is a classic trip-up for Java beginners. Part of the problem is that we lack a common language to identify this. Are parameters passed by value? Objects are passed by reference? Depending on who you are talking to there will be different reactions to the words pass by value and pass by reference, even though everyone wants to say the same thing.

    The way to visualize this is to understand that java variable can be one of two things. It can either contain a primitive or a reference to an object. Think of that reference as an number pointing to a location in memory. It isn’t a pointer in the C++ meaning in that it doesn’t point to a specific memory location, it is more of a handle, in that it lets the JVM look up a specific memory location where the object can be found. But you can visualize it this way:

       ArrayList<Integer> outer = @1234; //The first reference where the ArrayList was created.
    

    You then call inner with the parameters of:

      Puzzle.change(@1234);
    

    Note that you do not pass the outer variable, you pass the value of @1234. outer cannot be changed in any way by being a parameter on a method. This is what we mean when we say pass by value. The value is passed, but it is disconnected from the outer variable.

    Inside Puzzle:

    public static void change(ArrayList<Integer> inner) { // a new reference inner is created.
        //inner starts out as @1234
        inner.add(7);
        //now inner becomes @5678
        inner = new ArrayList<Integer>();
        //The object @5678 is changed.
        inner.add(23);
        //And printed.
        System.out.println(inner);
    }
    

    But outer is still pointing to @1234 as the method could not change that, it never had the outer variable, it just had its contents. However, since the change method started out with a reference to @1234, the object at that location could indeed be changed by the method and the results visible to any other reference to @1234.

    After the change method completes, nothing references the object @5678, so it becomes eligible for garbage collection.

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

Sidebar

Related Questions

So I stumbled upon this "new" graphics engine/technology called Unlimited Detail . This seems
I stumbled upon a very strange bit of PHP code. Could someone explain why
today I stumbled upon a very interesting case (at least for me). I am
I stumbled upon this question: 7 power 7 is 823543. Which higher power of
I recently stumbled upon a global hotkey class ( This one ), it works
Today I stumbled upon the possibility to access a DOM element in Javascript simply
I was building a reverse console (so that the written lines would append themselves
I'm doing a little rails app for a prototype. I started off using a
Most software-development buzzwords of the past few years, have been at times labeled either

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.