<p>Let's say I have the following class:</p>

<pre><code>public class Test&lt;E&gt; {
    public boolean sameClassAs(Object o) {
        // TODO help!
    }
}
</code></pre>

<p>How would I check that <code>o</code> is the same class as <code>E</code>?</p>

<pre><code>Test&lt;String&gt; test = new Test&lt;String&gt;();
test.sameClassAs("a string"); // returns true;
test.sameClassAs(4); // returns false;
</code></pre>

<p>I can't change the method signature from <code>(Object o)</code> as I'm overridding a superclass and so don't get to choose my method signature.</p>

<p>I would also rather not go down the road of attempting a cast and then catching the resulting exception if it fails.</p>

[{'answer_id': 51603, 'author': 'Slartibartfast', 'author_id': 4433, 'author_profile': 'https://Stackoverflow.com/users/4433', 'pm_score': 2, 'selected': False, 'text': '<p>I could only make it working like this:</p>\n\n<pre><code>public class Test&lt;E&gt; {  \n\n    private E e;  \n\n    public void setE(E e) {  \n        this.e = e;  \n    }\n\n    public boolean sameClassAs(Object o) {  \n\n        return (o.getClass().equals(e.getClass()));  \n    }\n\n    public boolean sameClassAs2(Object o) {  \n        return e.getClass().isInstance(o);  \n    }\n}\n</code></pre>\n'}, {'answer_id': 51615, 'author': 'Nick Fortescue', 'author_id': 5346, 'author_profile': 'https://Stackoverflow.com/users/5346', 'pm_score': 3, 'selected': False, 'text': "<p>The method I've always used is below. It is a pain and a bit ugly, but I haven't found a better one. You have to pass the class type through on construction, as when Generics are compiled class information is lost.</p>\n\n<pre><code>public class Test&lt;E&gt; {\n    private Class&lt;E&gt; clazz;\n    public Test(Class&lt;E&gt; clazz) {\n       this.clazz = clazz;\n    }\n    public boolean sameClassAs(Object o) {\n        return this.clazz.isInstance(o);\n    }\n}\n</code></pre>\n"}, {'answer_id': 51623, 'author': 'Tom Hawtin - tackline', 'author_id': 4725, 'author_profile': 'https://Stackoverflow.com/users/4725', 'pm_score': 6, 'selected': True, 'text': '<p>An instance of <code>Test</code> has no information as to what <code>E</code> is at runtime. So, you need to pass a <code>Class&lt;E&gt;</code> to the constructor of Test.</p>\n\n<pre><code>public class Test&lt;E&gt; {\n    private final Class&lt;E&gt; clazz;\n    public Test(Class&lt;E&gt; clazz) {\n        if (clazz == null) {\n            throw new NullPointerException();\n        }\n        this.clazz = clazz;\n    }\n    // To make things easier on clients:\n    public static &lt;T&gt; Test&lt;T&gt; create(Class&lt;T&gt; clazz) {\n        return new Test&lt;T&gt;(clazz);\n    }\n    public boolean sameClassAs(Object o) {\n        return o != null &amp;&amp; o.getClass() == clazz;\n    }\n}\n</code></pre>\n\n<p>If you want an "instanceof" relationship, use <code>Class.isAssignableFrom</code> instead of the <code>Class</code> comparison. Note, <code>E</code> will need to be a non-generic type, for the same reason <code>Test</code> needs the <code>Class</code> object.</p>\n\n<p>For examples in the Java API, see <code>java.util.Collections.checkedSet</code> and similar.</p>\n'}, {'answer_id': 765241, 'author': 'Ayman', 'author_id': 77222, 'author_profile': 'https://Stackoverflow.com/users/77222', 'pm_score': -1, 'selected': False, 'text': '<p>I was just trying to do the same thing, and one neat trick i just realized is that you can can try a cast, and if the cast fails, ClassCastException will be thrown.  You can can catch that, and do whatever.  </p>\n\n<p>so your sameClassAs method should look like:</p>\n\n<pre><code>public boolean sameClassAs(Object o) {\n    boolean same = false;\n    try {\n        E t = (E)o;\n        same = true;\n    } catch (ClassCastException e) {\n        // same is false, nothing else to do\n    } finally {\n        return same;\n    }\n}\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/51582', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1666/']