KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
What is the benefit of having generic constructor for a non-generic class? Java spec permits the following: class NonGeneric { <T> NonGeneric() { } ... NonGeneric ref = new <String> NonGeneric(); } Can one come up with a realistic example of when it enhances the typesafety of the class? How would it be better than using Generic in the first place. I understand that Java designers wanted the constructors to be more consistent with methods. Given that constructors can have side-effects, generic constructors can use generics to mutate some parameters whose references aren't retained, as in <T> NonGeneric(T obj, List<T> list) { list.add(obj); // Don't hold a reference to list }
Tags (comma-separated)
Save Edits
Cancel