This week's book giveaway is in the Beginning Java forum.
We're giving away four copies of Murach's Java Programming and have Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Java Programming this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

My serialization class - warning  RSS feed

 
Dawid Skrzypczynski
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I wrote class to write and read serialization objects. I used generalized types but i had one warning whose i don't understand.
Code:


Using:


Program works correctly but in line 8 my Eclipse says: "Type safety: Unchecked cast from Object to T". What does this mean ?
 
Rob Spoor
Sheriff
Posts: 21019
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because of type erasure, the generic type is no longer available at runtime. That means that the JVM cannot check at that point if the cast is valid. If the object is not a T then you will get a ClassCastException, only at a different point in your code. It will be harder to trace back the cause to the unsafe cast.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!