Categories
discuss

toString() a generic type in Java

How can I print the type of a generic java type?

Reflection? Any tricks?

public class Foo<K> {

    private K element;

    @Override
    public String toString() {
        return "Type: " + K;
    }
}

Answer

element.getClass().getSimpleName() will probably do what you are expecting.

Source: stackoverflow
Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. By using this site, you agree to the Privacy Policy, and Copyright Policy. Content is available under CC BY-SA 3.0 unless otherwise noted. The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 © No Copyrights, All Questions are retrived from public domain..