Created
April 21, 2011 12:26
-
-
Save lukaspili/934374 to your computer and use it in GitHub Desktop.
Generics implicit cast ?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.util.List; | |
| import java.util.ArrayList; | |
| public class Foo { | |
| public <T> List<T> getGenerics() { | |
| return new ArrayList<T>(); | |
| } | |
| public <T> T getFoo() { | |
| return getGenerics().get(0); // Compilation error | |
| } | |
| public <T> T getBar() { | |
| List<T> bar = getGenerics(); | |
| return bar.get(0); // OK | |
| } | |
| // AND THIS WORKS : | |
| public <T> T getGeneric() { | |
| return null; // we don't care, the problem happens at compilation time | |
| } | |
| public <T> T getFoobar() { | |
| return getGeneric(); // OK | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment