jQuery in Action, 3rd edition
The moose likes Beginning Java and the fly likes Change the object instance type Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Change the object instance type" Watch "Change the object instance type" New topic

Change the object instance type

mesagir sajib

Joined: Sep 26, 2012
Posts: 1
[Added code tags - see UseCodeTags]

can anyone clarify this topic.
Matthew Brown

Joined: Apr 06, 2010
Posts: 4542

Hi Mesagir. Welcome to the Ranch!

What exactly is your question? Is it about what's happening on line 11? That's a cast. It doesn't actually change the type of the object, but it changes the type of the reference. Without that cast, you couldn't assign the results to a MyClass variable - the compiler would prevent it. Using the cast is telling the compiler "I know that elementAt says it's returning an Object, but I know it's really returning a MyClass, so treat it like that". The JVM then throws an exception at runtime if that isn't actually true.

Using generic collections (e.g an ArrayList<MyClass>) avoids this.
I agree. Here's the link: http://aspose.com/file-tools
subject: Change the object instance type
It's not a secret anymore!