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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:32:37+00:00 2026-05-25T11:32:37+00:00

I want a hard reference class in my Java code, but, of course, there

  • 0

I want a hard reference class in my Java code, but, of course, there isn’t one. Is there some other way to do what I want, or should I make my own class?

This comes up with anonymous classes in methods where I want the anonymous class to set the return value for the method.

For example, given

interface Greeting {
    void greet();
}

I want code like the following:

// Does not compile
static void hello(final String who) {
    String returnValue;

    Greeting hello = new Greeting() {
        public void greet() {
            returnValue = "hello" + who;
        }
    };
    hello.greet();
    System.out.println(returnValue);
}

I can fake it using a list:

static void hello(final String who) {
    final List<String> returnValue = new ArrayList<String>();
    Greeting hello = new Greeting() {
        public void greet() {
            returnValue.add("hello" + who);
        }
    };

    hello.greet();
    System.out.println(returnValue.iterator().next());
}

But I want to not use a list. I can write a StrongReference class that solves this:

static class StrongReference<T> {
    private T referent;
    public void set(T referent) {
        this.referent = referent;
    }
    public T get() {
        return referent;
    }
}

which makes my method clearer:

static void hello(final String who) {
    final StrongReference<String> returnValue = new StrongReference<String>();
    Greeting hello = new Greeting() {
        public void greet() {
            returnValue.set("hello" + who);
        }
    };

    hello.greet();
    System.out.println(returnValue.get());
}

For my contrived example, I could have greet() return a String, but I’m working with much more complex classes, where the setting is deep within a database call that the base class manages. The instances have many different types they want to return, so I’ve just been using the List trick.

My questions are: Is there a better way to do this? What’s wrong with my StrongReference class? Has anyone written a StrongReference in a library somewhere?

  • 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-25T11:32:38+00:00Added an answer on May 25, 2026 at 11:32 am

    If you want something from the standard API, perhaps an AtomicReference would do?

    It has void set(V value) and a V get() methods. Unless you have multiple threads involved, just see the synchronization mechanism as a bonus 😉

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

Sidebar

Related Questions

I want to modify a connection string that's hard-coded in a Java application (jar
Why is it so hard to do this in Java? If you want to
This might be a little hard to explain, but I will try. I want
I'm having a hard time figuring out how to pass by reference in Java.
Basically, I want to have my program retrieve various variables from the hard drive
Want the function to sort the table by HP but if duplicate HPs then
this is a follow up question from this one , I don't want to
I prefer to use long identifiers to keep my code semantically clear, but in
I have a situation where I want to copy the output assembly from one
Is there room for issue in the following code in terms of multiple users

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.