This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
Note that the get method simply returns an element contained in the ArrayList, so the toString implementation depends on the element type. It has nothing to do with how toString is implemented in ArrayList.
Originally posted by memati bas: Latly , even though we only use the members of an object while using get() method, I think we can also use instance variables which do not belong to the objevt class indirectly, is not it??
An ArrayList can hold any type of object reference. In fact, when a reference is added to the List, its type is automatically upcast to Object, which is the base class of all objects.
Prior to Java 1.5, when getting an element from a List, you needed to explicitly downcast the reference back to the desired type.
Since Java 1.5, Lists ensure type safety with generics. So you can create an ArrayList that will only allow specific types to be added, and the downcast is handled automatically.
Here's an illustration...
Joined: Jan 29, 2006
Oh, Thanks, thanks and thanks for your explainatory replies Marc.