class SomeClass<E> {
Map<String, String> someMethod();
}
And usage
SomeClass a = new SomeClass();
a.someMethod().get("foo") // returns object, not string!
//This code would not even compile
String s = a.someMethod().get("foo");
But if I remove generalization (<E>) from SomeClass -- it works.
It also works if I extract this method to interface and use interface in declaration:
interface Foo {
Map<String, String> someMethod();
}
class SomeClass implements Foo {
//.....
}
Foo foo = new SomeClass();
String s = foo.someMethod().getString("A");//it works
Why it happens? Where can I read about it? What is the best work-around? Thanks.
The solution isn't to make SomeClass
non-generic - it's to avoid using the raw type. For example, this works fine:
import java.util.*;
class SomeClass<E> {
Map<String, String> someMethod() {
return null;
}
}
public class Test {
public static void main(String[] args) {
SomeClass<Integer> a = new SomeClass<Integer>();
String value = a.someMethod().get("foo");
}
}
Admittedly it's slightly odd that the method effectively has type erasure applied if you use the raw type - but you should basically avoid doing that to start with.
I can't currently find any explanation for the behaviour of the raw type in the JLS, but section 4.8 does include this warning:
The use of raw types is allowed only as a concession to compatibility of legacy code. The use of raw types in code written after the introduction of genericity into the Java programming language is strongly discouraged. It is possible that future versions of the Java programming language will disallow the use of raw types.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With