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

  • SEARCH
  • Home
  • 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 8051577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:19:58+00:00 2026-06-05T07:19:58+00:00

Sometimes I would like to pass an immutable object to a method and change

  • 0

Sometimes I would like to pass an immutable object to a method and change its value inside of the method or I would like the assign another object to the parameter reference (example 1). Or I would like to assign inside of an anonymous inner class another object to a final local variable (example 2). Because all of these cases are not possible, sometimes I use an Atomic(Integer|Long…) class or a list (which is really bad, because a list can contain multiple objects) for this purpose. Alternatively, I could create a class which holds an object and allows changing the object:

public class ReferenceExample {

    public static void main(String[] args) {
        final Reference<String> string = new Reference<>("a");
        // Example 1:
        method(string);

        // Example 2:
        new Thread() {
            @Override
            public void run() {
                string.set("c");
            };
        }.start();
    }

    public static void method(Reference<String> string) {
        string.set("b");
    }

    private static class Reference<T> {
        private T value;

        public Reference() {}

        public Reference(T value) {
            this.value = value;
        }

        public T get() {
            return value;
        }

        public void set(T value) {
            this.value = value;
        }
    }

}

I am wondering if such a class does already exists in Java or a common library (e.g. in an apache common project)? If not, are there any other good solutions for these problems?

  • 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-06-05T07:19:59+00:00Added an answer on June 5, 2026 at 7:19 am

    You’ve found the main solution, but the other two common approaches are

    • AtomicReference, which it looks like you’ve already found
    • A single-element array, which acts more or less as a mutable reference

    That said, you can usually find ways around the need:

    • return the (potentially different) reference from your method
    • implement the method inside a class which contains the mutable reference as a field, which is frequently more appropriate anyway
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Sometimes i would like to search for text containing a new line character and
We are sometimes getting an OutOfMemoryError in production and I would like to be
Sometimes it would be useful to name variables like no programmer should name his
I would like to expose a function that can take an optional anonymous method
I'm having a bunch of custom-made Reference Classes and would like to write coercion
I would like to convert a string that I pass in a function into
In my program, I'm grep-ing via NSTask. For some reason, sometimes I would get
Sometimes when I'm writing XLANG/s code in an expression shape, it would be more
How would you to convert or cast a List<T> to EntityCollection<T> ? Sometimes this
I sometimes need to debug JS in other browsers, and it would be really

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.