• Post Reply Bookmark Topic Watch Topic
  • New Topic

Finding instance variable values at runtime?  RSS feed

 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Is there a way to generically find an objects instance variable names and values? I considered Reflextion. So I looked at the Class object, but I do not see any 'getInstanceVariable()' or what not.
 
Rob Spoor
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out getFields() and getDeclaredFields(). Then, check out the modifiers to filter out static methods (getModifiers() in combination with Modifier.isStatic).
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob Prime wrote:Check out getFields() and getDeclaredFields(). Then, check out the modifiers to filter out static methods (getModifiers() in combination with Modifier.isStatic).


Hi Rob,
I got this far. So I have the names of the fields. But I also need to get the value at each reference. How do I do that? There is no'getValue()' method on Fields.

Class oClass = this.getClass();

Field[] fs = oClass.getDeclaredFields();
for(Field f: fs){
System.out.println("Input Parameter: " + f.getName() + ":");
}
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
restated: How to use the contents of a variable as an object reference?

I've tried a few times without success. I'll be watching this thread with interest.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a method "Field.get()" which returns the value regardless of type; the argument is the object whose field values you're trying to retrieve. There are many other methods "getBoolean()', "getInt()", etc, which return specific types. So, for example,

 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at some of the methods in class Field, which descends as follows...

java.lang.Object
----java.lang.reflect.AccessibleObject
--------java.lang.reflect.Field

it seems to imply that if you know the type of the variable, you can get its value. for example...

int getInt(Object obj)
Gets the value of a static or instance field of type int or of another primitive type convertible to type int via a widening conversion.

seems like a step in the right direction.

and from the same class

Class<?>getType()
Returns a Class object that identifies the declared type for the field represented by this Field object.

seems also worth a looksee. I've never tried this stuff, but I'll be checking it out soon.


edit: ok Ernest snuck in ahead of me, and his answer seems to take it a step further still. regards.
 
M Burke
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes Ernest, that works. I saw the get() method but I could not figure out how to apply it. Thanks


IMO, I think they should include a getValue() as a wrapper to make this object more intuitive.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an analogous problem that this all may help solve.

I have a variable of type char that can have a dozen different values, and along with that char I have two ints, call them int row and int col. I also have a dozen two dimensional arrays. ( yeah I know, no such thing )

anyways consider that the value of char c might be 'k' and I wish to access the array element k[row][col] without doing an if then or a select, on c.

get my drift? I'll definitely have to check this out.
 
Paul Clapham
Sheriff
Posts: 22828
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred Hamilton wrote:I have a variable of type char that can have a dozen different values, and along with that char I have two ints, call them int row and int col. I also have a dozen two dimensional arrays. ( yeah I know, no such thing )

anyways consider that the value of char c might be 'k' and I wish to access the array element k[row][col] without doing an if then or a select, on c.

For that problem you're better off just using a Map with the char as the key and the array as the value. No point in complicating your code with reflection just for that.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Fred Hamilton wrote:I have a variable of type char that can have a dozen different values, and along with that char I have two ints, call them int row and int col. I also have a dozen two dimensional arrays. ( yeah I know, no such thing )

anyways consider that the value of char c might be 'k' and I wish to access the array element k[row][col] without doing an if then or a select, on c.

For that problem you're better off just using a Map with the char as the key and the array as the value. No point in complicating your code with reflection just for that.


It's all interesting, I hope I'm not hijacking this thread... if so let me know, I'll copy this to a new thread and delete.

Ok Paul thanks for that I hadn't considered Map but it makes sense intuitively. I see there is an Interface called Map, but it does not seem that we have to implement it in a class declaration. I have some homework to do there.

ultimately, all my data here is static. it will never change. case in point...

in chess k is the symbol for the Black king. if we are thinking about k[0][0] it means the king occupies the top left corner of the board. The actual contents of k[0][0] represent all the squares the king can move to from [0][0], when the board is empty. You can see that it is static data, it's all about the rules of the game. There is also a lot of data here (8 8x8) arrays to be hard coded, so I will find a way to do it once programatically, then forget about it.

regards.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!