Re: print out object variable values
- From: "Lans Redmond" <sirlanslot@xxxxxxxxxxxxx>
- Date: Mon, 06 Feb 2006 00:12:48 GMT
try using reflection...iterate through the properties in the method then
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();
}
"polilop" <fmatosic@@inet.hr> wrote in message
news:ds1pam$3kk$1@xxxxxxxxxxxxxxxxx
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
.
- Follow-Ups:
- Re: print out object variable values
- From: polilop
- Re: print out object variable values
- References:
- print out object variable values
- From: polilop
- print out object variable values
- Prev by Date: print out object variable values
- Next by Date: Re: print out object variable values
- Previous by thread: print out object variable values
- Next by thread: Re: print out object variable values
- Index(es):
Relevant Pages
|
|