Generic Constructors
It is possible for constructors to be generic, even if their class is not. For example, consider the following short program:
class MyClass {
private double val;
<T extends Number> MyClass(T arg) {
val = arg.doubleValue();
}
void showval() {
System.out.println("val: " + val);
}
}
public class Main {
public static void main(String args[]) {
MyClass test = new MyClass(100);
MyClass test2 = new MyClass(123.5F);
test.showval();
test2.showval();
}
}
Home
Java Book
Language Basics
Java Book
Language Basics
Generics:
- Generic Class
- Generic Bounded Types
- Generic Wildcard Arguments
- Generic Bounded Wildcards
- Generic Method
- Generic Constructors
- Generic Interfaces
- Raw Types and Legacy Code
- Generic Class Hierarchies
- Run-Time Type Comparisons Within a Generic Hierarchy
- Overriding Methods in a Generic Class
- Generic Restrictions
- Generic Array Restrictions