티스토리 뷰

공부

[Java] cast object by generic

승가비 2021. 7. 24. 23:53
728x90
public static <T> T convertInstanceOfObject(Object o, Class<T> clazz) {
    try {
        return clazz.cast(o);
    } catch(ClassCastException e) {
        return null;
    }
}

https://stackoverflow.com/questions/14524751/cast-object-to-generic-type-for-returning

 

Cast Object to Generic Type for returning

Is there a way to cast an object to return value of a method? I tried this way but it gave a compile time exception in "instanceof" part: public static T convertInstanceOfObject(Object o...

stackoverflow.com

 

728x90

'공부' 카테고리의 다른 글

[Python] smtplib (with BCC)  (0) 2021.07.25
[SQL] UNION(DISTINCT) & UNION ALL  (0) 2021.07.24
[Linux] clear kerberos (kdestroy)  (0) 2021.07.24
[Python] name of class  (0) 2021.07.24
[IntelliJ] default directory  (0) 2021.06.12
댓글