August 30, 2004

Grrr!!!

Something should be made know as soon as possible. Like on the front cover Big Java. After bashing out a lot of repetitive code to access class variables by name, we were told there is a method in Object that does it!!

*beeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeep*

I refer to:
getClass

public final Class getClass()

Returns the runtime class of an object. That Class object is the object that is locked by static synchronized methods of the represented class.


Returns:
the object of type Class that represents the runtime class of the object.


And looking up the class Class (hehe), we are advised of the following methods:
getDeclaredField(String name)
          Returns a Field object that reflects the specified declared field of the class or interface represented by this Class object.

 Field[]
getDeclaredFields()
          Returns an array of Field objects reflecting all the fields declared by the class or interface represented by this Class object.
 Field
getField(String name)
          Returns a Field object that reflects the specified public member field of the class or interface represented by this Class object.

 Field[]
getFields()
          Returns an array containing Field objects reflecting all the accessible public fields of the class or interface represented by this Class object.


Field class has methods to converting the field into any of java's primatives as well as strings. So our code could have been a LOT smaller!

Grr....

Well now hopefully the world knows. Personally I never expected such high level abstraction, but hey, there it is...

Cheers,
Steve