Since generics are only checked during compile time with Java 5, can they avoid ClassCastExceptions in all
situations?
Since generics are only checked during compile time with Java 5, can they avoid
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
First of all, you should make sure that your code compiles without unckeched warnings. That is a good indicator. To understand why, I suggest you take a look at the sample chapter for generics from Effective Java.
Second of all, generics can’t guard you from code such as:
Third of all, if you’re in some way or another messing with Class Loaders, you might get strange
ClassCastExceptions, such as in this discussion thread. It is mind-numbing to seeSo the answer is no, you can’t get rid of
ClassCastExceptions just by properly using generics.