views:

1614

answers:

5

What is the meaning of the Java warning "Type safety: The cast from Object to List is actually checking against the erased type List"? I get it when I try to cast an Object to a type with generic information, such as in the following code:

Object object = getMyList();
List<Integer> list = (List<Integer>) object;
+9  A: 

This warning is there because Java is not actually storing type information at runtime in an object that uses generics. Thus, if 'object' is actually a List<String>, there will be no ClassCastException at runtime except until an item is accessed from the list that doesn't match the generic type defined in the variable. This can cause further complications if items are added to the list with this incorrect generic type information. Any code still holding a reference to the list but with the correct generic type information will now have an inconsistent list. To remove the warning, try:

List<?> list = (List<?>) object;

However, note that you will not be able to use certain methods such as add because the compiler doesn't know if you are trying to add an object of incorrect type. The above will work in a lot of situations, but if you have to use add or some similarly restricted method, you will just have to suffer the yellow underline in Eclipse (or a SuppressWarning annotation).

Mike Stone
A: 

And this is actually one of the things people have against the current java generics. As far as i remember exactly this could be fixed with reified generics

svrist
A: 

If you want to read more information about Generics and their commonly known uselessness I'd suggest you go to the to this blog

Shadow_x99
+1  A: 

For a comprehensive answer, you can consult a free excerpt (PDF) of the Generics chapter of Java engineer Joshua Bloch's Effective Java 2nd Edition.

Brian Laframboise
+2  A: 

Another great resource for Java generics is the Angelica Langer's Java Generics FAQ

ddimitrov
{"cc_re__cc_re__cc_by_sa_2_5": ["XML in Java</a>\n            </div>\n          </div>\n        </div>\n      </div>\n    </div>\n    <div id=\"footer\">\n      <div class=\"wrapper\">\n        <div id=\"attribution\">The questions, topics and answers come from <a rel=\"nofollow\" target=\"_blank\" href=\"http://serverfault.com\">serverfault.com</a>, <a rel=\"nofollow\" target=\"_blank\" href=\"http://stackoverflow.com\">stackoverflow.com</a> and <a rel=\"nofollow\" target=\"_blank\" href=\"http://superuser.com\">superuser.com</a>, and are licensed under the <a rel=\"nofollow\" target=\"_blank\" href=\"http://creativecommons.org/licenses/by-sa/2.5/\">cc-wiki</a> license.</div>\n        <div id=\"copyright\">\u00a9 2010 <a href=\"http://chadscira.com/\">Chad Scira</a> / icodeforlove</div>\n      </div>\n    </div>\n    <script type=\"text/javascript\">var globals = {};globals['servertime'] = '1512998976';globals['cdn'] = 'http://ansaurus.com/';globals['basedomain'] = 'http://ansaurus.com/';globals['pagetype'] = 'question';</script>\n    <script src=\"http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js\" type=\"text/javascript\"></script>\n    <script"], "copyright__copyright__copyright": ["ar as i remember exactly this could be fixed with reified generics\n\nsvrist\nA:\u00a0\n\nIf you want to read more information about Generics and their commonly known uselessness I'd suggest you go to the to this blog\n\nShadow_x99\n+1\u00a0 A:\u00a0\n\nFor a comprehensive answer, you can consult a free excerpt (PDF) of the Generics chapter of Java engineer Joshua Bloch's Effective Java 2nd Edition.\n\nBrian Laframboise\n+2\u00a0 A:\u00a0\n\nAnother great resource for Java generics is the Angelica Langer's Java Generics FAQ\n\nddimitrov", "ar as i remember exactly this could be fixed with reified generics\n\nsvrist\nA:\u00a0\n\nIf you want to read more information about Generics and their commonly known uselessness I'd suggest you go to the to this blog\n\nShadow_x99\n+1\u00a0 A:\u00a0\n\nFor a comprehensive answer, you can consult a free excerpt (PDF) of the Generics chapter of Java engineer Joshua Bloch's Effective Java 2nd Edition.\n\nBrian Laframboise\n+2\u00a0 A:\u00a0\n\nAnother great resource for Java generics is the Angelica Langer's Java Generics FAQ\n\nddimitrov", "ar as i remember exactly this could be fixed with reified generics\n\nsvrist\nA:\u00a0\n\nIf you want to read more information about Generics and their commonly known uselessness I'd suggest you go to the to this blog\n\nShadow_x99\n+1\u00a0 A:\u00a0\n\nFor a comprehensive answer, you can consult a free excerpt (PDF) of the Generics chapter of Java engineer Joshua Bloch's Effective Java 2nd Edition.\n\nBrian Laframboise\n+2\u00a0 A:\u00a0\n\nAnother great resource for Java generics is the Angelica Langer's Java Generics FAQ\n\nddimitrov"]}