tags:

views:

82

answers:

6
public class RefName { 
  void outRefName() {
    System.out.println("the current reference name is" + xxx);
  };
};

public static void main(String[] args) {
  RefName rf1 = new RefName();
  rf1.outRefName();   // should be rf1
  RefName rf2 = new RefName();
  rf2.outRefName();   // should be rf2
};

As the code above shows,could I make this happen within Java?

thanks.

+3  A: 

Can't be done. What would the following print?

RefName a = new RefName(), b;
(b = a).outRefName();
gustafc
+2  A: 

rf1 is just the name of the variable, so even if you could get this working, it would not be a method of the class - after all, you could have:

RefName rf1 = new RefName();
RefName rf2 = rf1;

this is the same instance; what should rf1.outRefName() produce? No, I don't think you can do this. In C# there are some hacky ways of doing it (involving captured variables and either reflection or expression-tree inspection), but again - you are getting the variable name - nothing to do with the object. The better approach here may be to give your class a Name member and initialize the name in the constructor:

RefName rf1 = new RefName("myname");

Here, the name of the object is "myname". Not rf1, which is the name of the variable.

Marc Gravell
+1  A: 

Imagine that we are coleagues and when I mention you to the members of my family we use some kind of nickname. Let's say 'The other day my tall colleagu said this'. There's no way for you to know how we do call you. Isn't it?

Boris Pavlović
+2  A: 

If this information is valueable, you need to pass this information in yourself.

public class RefName {

    private String name;

    public RefName(String name) {
        this.name = name;
    }

    public String outRefName() {
        System.out.println(name);
    }

}

-

RefName rf1 = new RefName("rf1");
rf1.outRefName();
BalusC
A: 

This can't be done in Java. I guess you can clearly see why here:

(new RefName()).outRefName()

The "reference" never even got a name.

abyx
+1  A: 

By using Reflection you can find which fields of an Object contain a given instance. This only works for fields, not for variables. Here an incomplete sample:

public class BackRef {

    private class RefName {

        // finds first field containing this object
        public String outRefName() {
            Field[] fields = BackRef.class.getDeclaredFields();
            for (Field field : fields) {
                if (getClass().isAssignableFrom(field.getType())) {
                    field.setAccessible(true);
                    try {
                        if (field.get(BackRef.this) == this)
                            return field.getName();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
            return null;
        }

    }

    private RefName ref1;
    private RefName ref2;

    BackRef() {
        ref1 = new RefName();
        ref2 = new RefName();

        System.out.println(ref1.outRefName());
        System.out.println(ref2.outRefName());
    }

    public static void main(String[] args) {
        new BackRef();
    }
}

It's kind of a (big) hack, but I used it once to display in which field a clicked GUI component was saved...

Carlos Heuberger
+1 this is a big hack.
Jichao