I use
if (clazz.getSuperclass().getName() == "java.lang.Object")
Is there a better way?
I use
if (clazz.getSuperclass().getName() == "java.lang.Object")
Is there a better way?
if ( clazz.getSuperclass( ) == Object.class )
There are 2 problems with your original implementation:
getSuperclass
may return null
and you get NPE when you call getName==
instead of equals
). Strangely enough it may work in this case as "java.lang.Object"
string is probably internalized. How about
if (clazz.getSuperclass().equals(java.lang.Object.class))
java.lang.Object
. Probably Object.class.equals( clazz.getSuperclass( ) )
is more robust. Also java.lang.Class
is one of the classes where it's OK to use identity compare.
May 31, 2010 at 21:51
equals
- thus getting myself bitten in the other leg :-)
Jun 1, 2010 at 5:25