Stupid Newbie Question: get name of instantiated variable?

R

Richard Chrenko

Which is the more risk free approach?

Let's say there's a method that does what gets the name directly from
the environment, like I want, we'll call it getVariableName();

Thingie fred = new Thingie( "fred" );
Thingie larry = new Thingie( "larry" );

fred = larry

println( fred.getName( ) );
println( getVariableName( fred ) );

So, in theory, which would most reliably return the information I wanted?

I considered the approach you suggested, I was just trying to find out
if there was a more dependable way to do it. :)

Ed.

It clearly depends upon what information you want. If you are designing a
debugger, the actual name of the Thingie instance may be helpful. If on
the other hand you are developing object-oriented applications (like the
vast majority of us on this newsgroup), your requirement is quite
puzzling. If you assign larry to fred, you are explicitly saying that you
want the object references larry and fred to access the same object (i.e.
contain the same information). So why differentiate between the two?

Are you by any chance coming to Java from the PHP world? I ask because PHP
permits data manipulations of the type you propose (as well as many other
oddities such as mathematical calculations with string variables!).
 
E

Ed Dana

Thufir said:
In all fairness, your question was "can it be done EASILY", not "can it
be done".

Yes, Thufir, I know. :)

My last statement wasn't intended to attack Tor's position, it was
intended to clarify it. Nothing more.

Ed.
 
E

Ed Dana

Richard said:
It clearly depends upon what information you want. If you are designing
a debugger, the actual name of the Thingie instance may be helpful. If
on the other hand you are developing object-oriented applications (like
the vast majority of us on this newsgroup), your requirement is quite
puzzling. If you assign larry to fred, you are explicitly saying that
you want the object references larry and fred to access the same object
(i.e. contain the same information). So why differentiate between the two?

Are you by any chance coming to Java from the PHP world? I ask because
PHP permits data manipulations of the type you propose (as well as many
other oddities such as mathematical calculations with string variables!).

It was nothing more than a curious question. As I said elsewhere, it is
a bit of information that can be processed.

As to what prompted the question, it comes from my playing around with
REBOL. A language that does allow you to natively interrogate an object
and it variables the fields inside. It can be quite handy.

Imagine an array of objects, the ability to interrogate the objects in
that array and, by virtue of knowing what's in it, the ability to call
specific procedures based on which object you're processing at the
moment. It can be very powerful.

It looks like Reflect can do that, so I guess I'll give it a closer look.

Ed.
 
T

Thufir Hawat

At the moment in Java, I believe this is generally accomplished with
generics. Generics allow you to create a collection of objects, all of
the same type. This obviates the need for casting and reduces run-time
errors. If you have a collection of type Thingies you can be *certain*
that each element indeed is/points to/references (?) an actual Thingie
instance.

I'm sure generics does more than that, too!
 
B

Bryce

That's right. How do I get the name of a variable that has been
instantiaded from a class? I don't want the class name, I want the
variable name.

If I instantiate: String Fred = new String( "Flintstone"), how do I
extract the variable name "Fred?"

As you can see from the many responses, this is not very easy to do
the way you want.

Generally, if you want to assign a name/value to a string and and
object, you use a Map implementation.

For example, if you wanted to associate the name "Fred" with a String
object with the value "Flinstone", you'd do the following:

Map map = new HashMap();
map.put("Fred", new String("Flinstone");

Now, you can locate and retrieve the object by:

String myString = (String)map.get("Fred");

But, if you insist on getting name, here's some example code that uses
reflection:

public class TestReflection {
private String fred = new String("Flinstone");

public String getVariableNameFromValue(String value) throws
IllegalAccessException {
String result = null;

Field[] fields = this.getClass().getDeclaredFields();
for (int i = 0; i < fields.length && result == null; i++) {
Field field = fields;
String fieldValue = (String)field.get(this);

if (fieldValue.equals(value)) {
result = field.getName();
}
}

return result;
}

public static void main(String[] args) throws
IllegalAccessException {
TestReflection test = new TestReflection();

System.out.println(test.getVariableNameFromValue("Flinstone"));
}
}

NOTE: This requires the variable to be a field (i.e., part of the
class). I don't know of any way to do the same for local method
variables.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,161
Latest member
GertrudeMa
Top