-2

I have a small doubt. Is there anyway to get an object on runtime?

Something like this: I have a class say Employee and has following properties name, addrees, id

Now i have an object of Employee but I have to get one of the property depending on certain condition.

So it may be emp.getName() or emp.getID() on runtime.

How do I achieve this?

Regards, Amit

David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • all objects are instantiated at runtime. Retrieving information not statically set in your java code and using it to construct a new object works exactly the same as what you've been doing – ControlAltDel Apr 23 '12 at 17:24

3 Answers3

1

Try

String str = (condition) ? emp.getName() : emp.getID();

or

final String str;
switch(userSelectedField) {
   case "lastname": str = emp.getName().getLastName(); break;
   case "zipcode": str = emp.getCountry().getState().getZipCode(); break;
   default: /* report an error */
}
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • No its not that simple actually. There are hundred of properties and too multilevel. I have to use these properties to be compared for their values depending upon what property user selects at runtime. so it may be emp.getName.getLastName or if he chooses country's zip code it would be emp.getCountry.getState.getZipCode() – AMIT RANA Apr 23 '12 at 17:25
  • If you have many of these try a switch instead. – Peter Lawrey Apr 23 '12 at 17:32
  • Its not possible to call them at compile time. Since there are thousand of possible combination.I have to get these proprty values at runtime – AMIT RANA Apr 23 '12 at 17:53
  • In that case you need a DSL to describe them, You can use reflections to navigate the fields. – Peter Lawrey Apr 23 '12 at 20:05
1

If your objects conform to JavaBeans conventions for getter/setter naming, PropertyUtils.getProperty, from apache's beanutils library, looks like it would do the job nicely, as described in this question.

For example, you could do something like

String field = "name.lastname"; // or could be read dynamically from user input
Object value = PropertyUtils.getProperty(emp, field);

to handle your nested properties.

Community
  • 1
  • 1
David Gelhar
  • 27,873
  • 3
  • 67
  • 84
  • Compared to my Map idea, this will be a bit slower (uses reflection, but unlikely to matter in most apps) and still has the disadvantage that everything is returned as an Object, not a "real" type. However, it has the big advantage that, if the code is already written with 100s of fields, no rewrite to use a Map is required! – user949300 Apr 23 '12 at 18:27
  • Perfect, this solves the problem.Much appreciate the help. Thank you very much. – AMIT RANA Apr 23 '12 at 20:52
0

If there are hundreds of properties (as you mention in a comment) then one option would be to store them all in a Map<String, SomeType>. Access is very easy, pass a String (or an Enum) for the key. The major drawback is that their values would all be of SomeType (probably a String in practice) where you might really want some of them to be Integers, Dates, etc. In that case, there are a couple of options:

  1. The caller has to know the type and parse. For example, the Map stores the Date as a String, "01/23/1967", the caller knows that KEY_BIRTHDAY should be converted to a Date, but you have utility code that the caller uses to convert this to a Date.

  2. Or the Map knows the type, perhaps through another Map<String, Class> with the class of each value, and the Map does the conversion.

user949300
  • 15,364
  • 7
  • 35
  • 66