Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Determining if a java.lang.Class object implements Comparable

807606Jun 6 2007 — edited Jun 6 2007
If I have a Class clazz object, is it possible to determine if it implements Comparable without calling newInstance() against it?

This is the code I currently have, but I'd like to do the same thing without creating a variable that will immediately be thrown away.
	private boolean implementsComparable(Class clazz)
	{
		try
		{
			if (clazz.newInstance() instanceof Comparable)
			{
				return true;
			}
		}
		catch (Exception ex)
		{
                        // @TODO something about NPE's
			// ignore all exceptions here
		}
                return false;
	}
Thank you.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 4 2007
Added on Jun 6 2007
2 comments
82 views