Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if the superclass is java.lang.Object

I use

if (clazz.getSuperclass().getName() == "java.lang.Object")

Is there a better way?

like image 905
unj2 Avatar asked Dec 17 '22 00:12

unj2


1 Answers

if ( clazz.getSuperclass( ) == Object.class )

There are 2 problems with your original implementation:

  1. getSuperclass may return null and you get NPE when you call getName
  2. You use identity equality with a String ( == instead of equals ). Strangely enough it may work in this case as "java.lang.Object" string is probably internalized.
like image 81
Alexander Pogrebnyak Avatar answered Jan 03 '23 09:01

Alexander Pogrebnyak