File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Change the object instance type

 
mesagir sajib
Greenhorn
Posts: 1
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Added code tags - see UseCodeTags]


can anyone clarify this topic.
 
Matthew Brown
Bartender
Posts: 4549
8
Java Netbeans IDE Scala
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic