This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
Hi trying to get output to screen terminal from an arraylist of objects using system.out.print(ln) the terminal window prints out the membory reference used, either by java (or BlueJ), but not the fields of the object I want to retrieve apologies for my ignorance but any advice greatly appreciated as you will all quickly guess - I am very new to Java!!
Hi Kel, Welcome to JavaRanch! If you ask Java to "print" an Object, either by calling System.out.println() on it, or by calling System.out.println() on an ArrayList containing it, what happens is that println() calls the toString() method on that object and then displays the returned String. The default implementation of toString() returns what you're calling the "memory reference" -- it's actually composed of the hash code for the object and the name of the class. If you want the object to print in some other way, you just have to implement your own version of toString() in your class -- i.e.,
Hi Thanks for your speedy and helpful response. Thought I understood this object reference stuff but I am still confused. Hope you don't mind but I will include my code - hope I can get the hang of the code section here!? A further class instantiates instances of "Members" but I have populated the ArrayList with 4 examples for testing. Similar code runs in a different project without trouble and I thought I had carefully transposed the variables and object references to the new class - but it returns the 'Class and Hash code' as you mention. I am even more confused as the 'get method' - "numberOfMembers" correctly returns the int '4' so I think I am pointing the code to the correect class etc? Here's hoping you can point me in the right direction?
[ December 20, 2003: Message edited by: kel dobson ]
author and iconoclast
WorldCitizen -- Welcome to JavaRanch! We don't have too many rules around these parts but we do have our naming policy, which requires that you have a real-sounding name containing first and last components. Most people here use their real name -- it keeps us honest. Please head over here and change yours, pronto. Thanks, pardner!
Joined: Dec 20, 2003
hi Is it OK to continue on this theme please? sorry, but I don't understand what you mean by " . . . member does expose"??
kel, You still haven't answered Marilyn's or Ernest's questions above. Perhaps the following demonstration will clear up the concept. The above example printed Foo@7259da on my system.The above code example printed this Foo's a is 5 on my system.