1

Using "String concatenation" I can get a Class Object via its fully qualified class name:

String s = "Foo";// package
String s2 = "bar";// Class
Class c=Class.forName(s+"."+s2);

How do I do the Object equivalent:

String s = "Foo";// portion of Object's name
String s2 = "bar";// another portion of Object's name
JButton foo_Bar = new JButton();

JButton o = Object.forName(s+"_"+s2);// Using "String concatenation" (key phrase) to obtain Object

Or ANY WAY (simple or otherwise) that retains using "String concatenation"

tcooc
  • 20,629
  • 3
  • 39
  • 57
scrapple
  • 27
  • 1
  • Is `foo_Bar` a field or a local variable? Either way, you'd be better off using a `Map` or whatever... – Jon Skeet Aug 22 '14 at 16:18
  • What is this supposed to be? I imagine the answers are going to be different depending on whether this is homework, you trying to learn Java after decades of development in another language or you making a framework that needs to instantiate objects given only their class/interface – Ordous Aug 22 '14 at 16:22
  • 1
    If you're trying to get the _local variable_ named "foo_Bar" at runtime, you can't do that. No way, no how. – Louis Wasserman Aug 22 '14 at 16:59
  • While I am tempted to write code that does look up a variable by its name (for instance through [JVMTI](http://en.wikipedia.org/wiki/Java_Virtual_Machine_Tools_Interface)), I think I will refrain from leading the OP any further astray. @scrapple: I am absolutely sure there are better ways of solving whatever problem you are trying to solve, but not knowing which problem that is, we can not point them out ... – meriton Aug 22 '14 at 18:10
  • 1
    It's totally unclear what you're asking. If you want to access a local variable by it's string name, you can't. (This question comes up about every 2 weeks.) If you want to create an instance of a class named by a string, that's simply a matter of doing newInstance on the Class object retrieved by Class.forName. – Hot Licks Aug 22 '14 at 18:33
  • 1
    As the others suggest, usually when someone wants to do this it's because they're trying to solve the wrong problem. – Hot Licks Aug 22 '14 at 18:35

1 Answers1

0

This is only possible if the object you're trying to get is a class variable (defined outside of a method, as a part of the class).

You can then use Java's Reflection API to access the value of a field by the field name. The code for achieving this will look as follows:

public class Example {
    public JButton foo_bar = new JButton("I am a Button");

    public void fieldByNameExample() throws NoSuchFieldException, IllegalAccessException {
        // The two parts we're going to concatenate to make "foo_bar"
        String partA = "foo";
        String partB = "bar";

        // Gets the Field object representing the field by its name.
        Field field = Example.class.getDeclaredField(partA + "_" + partB);
        // Retrieves the contents of the Field for this object.
        JButton value = (JButton) field.get(this);
        System.out.println(value.getText()); // prints: "I am a Button"
    }
}

You need to catch NoSuchFieldException (thrown when no field with the given name exists), and IllegalAccessException (thrown when the field you're trying to get the contents of is not accessible in the current context) when you call the above method.

As mentioned before, this only works if the variable is declared as a field in the class. It is not possible with variables declared inside methods (as the compiler discards the names of such local variables in the compilation process).

Please note that the Reflection API can be relatively slow, and using it is often not the best solution. You should also look into other solutions for your problem. If you still insist on using Reflection, I advise you to first look into documentation regarding Java Reflection, since the Reflection API is rather difficult to use if you don't know exactly what you're doing.

Chronio
  • 757
  • 3
  • 8