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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:39:44+00:00 2026-05-14T15:39:44+00:00

Imagine I have the following situation: Test1.java import java.lang.ref.WeakReference; public class Test1 { public

  • 0

Imagine I have the following situation:

Test1.java

import java.lang.ref.WeakReference;
public class Test1
{
    public WeakReference fieldName;

    public init()
    {
        fieldName = new WeakReference(this);
        Test2.setWeakRef(fieldName);
    }
}

Test2.java

import java.lang.ref.WeakReference;
public class Test2
{    
    public static setWeakRef(WeakReference weakRef)
    {
        //at this point I got weakRef in an other class.. now, how do I get the field name this reference was created with? So that it returns exactly "fieldName", because that's the name I gave it in Test1.java?
    }
}

At the location of the comment I received the weak reference created in an other class. How would I retreive the field name that this weak reference was created with, in this case “fieldName”?

Thanks in advance.

  • 1 1 Answer
  • 2 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-14T15:39:45+00:00Added an answer on May 14, 2026 at 3:39 pm

    The only way would be using reflection. This is true for fields of any type, not only for weak references.

    Field[] fields = Test1.getDeclaredFields();
    
    for (Field field : fields) {
      if (field.getType().equals(WeakReference.class) {
        System.out.println(field.getName());
      }
    }
    

    Of course this example works only if you have a single WeakReference field in Test1.

    Update: In case you have multiple reference fields, you need access to the object itself, in order to get and compare the actual value of a specific field with the value received. Like in this modified code example:

    public class Test2 {    
      public static setWeakRef(Test1 instance, WeakReference weakRef) {
        Field[] fields = Test1.getDeclaredFields();
    
        for (Field field : fields) {
          if (field.getType().equals(WeakReference.class) {
            WeakReference value = (WeakReference) field.get(instance);
            if (value == weakRef) {
              System.out.println(field.getName());
            }
          }
        }
      }
    }
    

    Note that exception handling is left out for the sake of simplicity.

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

Sidebar

Related Questions

Imagine the following situation: I have two branches: DEV and MAIN. I'm working on
I have a question about jQuery load best practice. Imagine the following situation: $(#div).on('click',
Probably a very basic beginner question. Imagine the following situation: I have an ASP.NET
Imagine that i have the following code: <a:repeat value=#{bean.getList()} var=x > <li class=la> <span>
Let me give you the following example: Imagine you have a window class which
imagine you have the following interfaces: public interface IInterfaceA : IInterfaceX { // //
In C#, imagine I have the following object: var myObject = new { Val
Imagine you have the following situation: for i in xrange(100000): account = Account() account.foo
Imagine the following situation: I have project folder, in this folder I have src
Imagine you have the following TreeMap: var dates = new TreeMap[Long, Tuple2[Int, Double]]() I

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.