On Aug 11, 12:20 am, JKid314159 <[email protected]> wrote:
> Hi:
>  
> I understand now.  The toString method is placed inside of the MyOwnObject 
> class
> which overrides the toString method of Object class.  The Object class 
> returns:
> nameofthecl...@hashcodelocation with the toString method of the HashSet or 
> Object class, not too sure where it comes from.
By Jove, read the api: 
http://java.sun.com/javase/6/docs/api/java/util/AbstractCollection.html#toString()
At the end of the javadoc for HashSet, you have all the inherited
methods with their location. The toString method of class HashSet is
inherited from AbstractCollection toString method, (just click on the
link) which in turns:
[quote]
Returns a string representation of this collection. The string
representation consists of a list of the collection's elements in the
order they are returned by its iterator, enclosed in square brackets
("[]"). Adjacent elements are separated by the characters ", " (comma
and space). Elements are converted to strings as by String.valueOf
(Object).
[/quote]
That means that first it uses the toString method of
AbstractCollection, then for each element, it uses the valueOf(Object)
method of the String class, which in turns uses the toString method of
each element, which by default, according to the toString method of
Object returns an hexadecimal representation of the hashcode value of
the object, so that if you ovveride the toString method of the object,
it uses the overriden method to represent the object inside the
toString method of the HashSet.

Getters and Setters here have nothing to do with this, except that it
is good pratice to use them systematically in any public methods (aka
toString for example) to ensure you never give access directly to the
fields outside of the class.
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to 
[email protected]
For more options, visit this group at 
http://groups.google.com/group/javaprogrammingwithpassion?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to