Java-Generic Constructors

Generic Constructors

It is possible for constructors to be generic, even if their class is not. For example, in the following program, ShowValue constructor specifies a parameter of a generic type, even though ShowValue is not a generic class, its constructor is generic.

Program

final


Program Source

class ShowValue {
    
    <T> ShowValue(T ob)
    {
        System.out.println("T Value : "+ob);
    }
}

public class Javaapp {
    
    public static void main(String[] args) {
        
        ShowValue sv1 = new ShowValue(50);
        ShowValue sv2 = new ShowValue("Fifty");
    }
}

Leave a Comment