I have a class with 50 string variables.
I need to see in my main program which ones are set to some values.
To do this i need to print them to the screen. I'm getting fed up writting
System.out.println(myObject.getThisString);
System.out.println(myObject.getThatString)
.....
As i have to do this few times.
Is there an easy way to print all the values in an object?
Thx
try using reflection...iterate through the properties in the method then
print
see example
public String toString()
{
StringBuffer buff = new StringBuffer();
Field[] fields = this.getClass().getDeclaredFields();
for (int i = 0; i < fields.length; i++)
{
try
{
buff.append(fields[i].getName())
.append("\t=>\t")
.append(fields[i].get(this))
.append("\n");
//System.out.println(fields[i].getName() + " : values: " +
// fields[i].get(this));
}
catch (IllegalAccessException ex)
{
ex.printStackTrace(System.out);
}
catch (IllegalArgumentException ex)
{
ex.printStackTrace(System.out);
}
}
return buff.toString();
}
> I have a class with 50 string variables.
> I need to see in my main program which ones are set to some values.
[quoted text clipped - 7 lines]
>
> Thx
polilop - 06 Feb 2006 18:44 GMT
Thx made my work easier
> try using reflection...iterate through the properties in the method then
> print
[quoted text clipped - 44 lines]
>>
>> Thx