Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

print out object variable values

Thread view: 
polilop - 04 Feb 2006 08:40 GMT
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
Lans Redmond - 06 Feb 2006 00:12 GMT
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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.