A comment on Java style

Andreas Schlapbach schlpbch@iam.unibe.ch
Fri Dec 20 10:36:00 GMT 2002


What about

(otherName.getClass() == Object.class)

No need to use Strings here.

Andreas

Jim White wrote:
> Per Bothner wrote:
>  > +    if (otherName.equals("java.lang.Object")
> 
> A spiffy Java idiom for getting class names is:
> 
> Object.class.getName()
> 
> Although the main reason for that is to be immune to package name 
> changes (which would rarely apply to Object), I still prefer to never 
> embed class names as literal strings.  Also supports refactoring tools 
> which may change class names.
> 
> Works if a static is needed too:
> 
> final static String CLASSNAME = MyClass.class.getName();
> 
> jim




More information about the Kawa mailing list