<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>

I could only make it working like this:
```
```
public class Test<E> {  

    private E e;  

    public void setE(E e) {  
        this.e = e;  
    }

    public boolean sameClassAs(Object o) {  

        return (o.getClass().equals(e.getClass()));  
    }

    public boolean sameClassAs2(Object o) {  
        return e.getClass().isInstance(o);  
    }
}
```
```