0

Is it possible to set a property on an object using a string as the property name? I am quite new to Java but in JavaScript I would just do something like this:

 Object[propertyNameAsString] = value

After searching around I found how to get a property using a string, but not how to set one. I also came across something called a Properties object, is that what I need to be using?

Basically I just want to loop through an array filled with image names and then set their respective properties onto an object which I will use to store the actual images. Is this possible in Java or should I do it another way?

Edit:

This is what my array looks like:

 static String pngs[] = {"staminaBox", "staminaBoxR", "healthBox", "healthBoxR", "moveBox", "goButton"};

Ideally I want to loop through them and add properties to an assets class like so:

 for (int i = 0; i < pngs.length; i++) {
    Assets.pngs[i] = createImageFromUrl(pngs[i] + ".png");      
 }

But now I'm realizing this wouldn't work because in my assets class I have to previously define the properties like:

 public static Image staminaBox;

And the whole point of me wanting to do this in the first place was so that I didn't have to write out declarations for every single image I wanted to add. I'm used to the loose nature of JavaScript, but does this mean there is no way of getting around these explicit declarations of properties? Will a HashMap let me accomplish this?

  • 2
    You should probably use a Map, where key is the image name and value is your image object. – NeplatnyUdaj Mar 25 '14 at 17:24
  • Show us how your array is declared, and the class of the elements of the array. Objects are accessed through methods. – JB Nizet Mar 25 '14 at 17:25
  • If I understand correctly, you want to transform an array of strings, containing image names, into an array of Image instances (Image being the type of object returned by createImageFromUrl(), which you haven't shown). No problem with that, but you obviosuly need two arrays: an array of string can't contain instances of Image. Don't try to apply JS idioms in Java. They're completely different languages. Java is about string types, classes, encapsulation (no public fields), polymorphism. JS is about loosely typed, dynamic data structures. – JB Nizet Mar 25 '14 at 17:45
  • Sorry I'm not being clear, when I say "Assets.pngs[i]", I'm not referring to a second array, but rather that the string stored at pngs[i] is what I want the property name to be on the Assets object. It's becoming clear that I should abandon any notion of doing this. – BartyParty Mar 25 '14 at 17:51
  • A Map is probably what you're looking for. It's basically what a JS object is: an association of keys and values. – JB Nizet Mar 25 '14 at 18:08

2 Answers2

0

You might be talking about a couple different things. Are you talking about setting the value of variables defined inside a class? If so, you might be able to hack something together using reflection, but it's not a "gimme" in Java like it is in javascript.

You might also look into using a HashMap of properties to their values, or like you said, the Properties object (which is pretty much just a Map itself).

Kevin Workman
  • 39,413
  • 8
  • 61
  • 94
0

You can use the Reflection API. Keep in mind that it will get messy very quickly.

Because Java uses the Getter and Setter pattern, you likely won't be assigning a value to a member variable but calling a method which itself assigns the value.

How do I invoke a Java method when given the method name as a string?

Community
  • 1
  • 1
Charlie
  • 7,227
  • 48
  • 51