The Artima Developer Community
Sponsored Link

Java Buzz Forum
One of those java features I should have known: class.isInstance()

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Chris Winters

Posts: 931
Nickname: cwinters
Registered: Jul, 2003

Daytime: Java hacker; nighttime: Perl hacker; sleeptime: some of both.
One of those java features I should have known: class.isInstance() Posted: Jun 21, 2005 7:52 AM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Chris Winters.
Original Post: One of those java features I should have known: class.isInstance()
Feed Title: cwinters.com
Feed URL: http://www.cwinters.com/search/registrar.php?domain=jroller.com®istrar=sedopark
Feed Description: Chris Winters on Java, programming and technology, usually in that order.
Latest Java Buzz Posts
Latest Java Buzz Posts by Chris Winters
Latest Posts From cwinters.com

Advertisement
One of the few times I've written a piece of functionality and actually found it later: the isInstance() method of the Class class. It's useful when you're dealing with generic classes and therefore cannot use instanceof:
void ensureAllHaveParent( Collection c, Class parentClass ) {
    for ( Iterator it = c.iterator(); it.hasNext(); ) {
        Object o = it.next();
        if ( ! ( o instanceof parentClass ) ) ... // WRONG!
        if ( ! parentClass.isInstance( o ) )  ... // RIGHT!
    }
}

Read: One of those java features I should have known: class.isInstance()

Topic: Full Feed or Excerpt only in RSS Feeds? Previous Topic   Next Topic Topic: Nokia SNAP Mobile included with SUn's WTK2.3 Beta

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use