H
hiwa
In the pre-generics era, we did:
Class claz = Class.forName("Foo");
Foo foo = (Foo)(claz.newInstance());
We anticipate we can be freed from casting by generics:
Class<Foo> claz = Class.forName("Foo"); // line 1
Foo foo = claz.newInstance();
But compiler emits 'incompatible types' error for the line 1 above.
We try another:
Class<Foo> claz = (Class<Foo>)(Class.forName("Foo")); // line 1
Foo foo = claz.newInstance();
Then, compiler emits 'unchecked cast' warning for the line 1 above.
What should be the correct syntax for getting a Class<Foo> object
from the forName() method without errors nor warnings?
Thanks in advance.
Class claz = Class.forName("Foo");
Foo foo = (Foo)(claz.newInstance());
We anticipate we can be freed from casting by generics:
Class<Foo> claz = Class.forName("Foo"); // line 1
Foo foo = claz.newInstance();
But compiler emits 'incompatible types' error for the line 1 above.
We try another:
Class<Foo> claz = (Class<Foo>)(Class.forName("Foo")); // line 1
Foo foo = claz.newInstance();
Then, compiler emits 'unchecked cast' warning for the line 1 above.
What should be the correct syntax for getting a Class<Foo> object
from the forName() method without errors nor warnings?
Thanks in advance.