Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7685669
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:18:15+00:00 2026-05-31T19:18:15+00:00

I am looping through ArrayList<Bar> . Foo extends Bar . I’m trying to find

  • 0

I am looping through ArrayList<Bar>. Foo extends Bar. I’m trying to find an instance of Foo in the list.

ArrayList<Bar> list = new ArrayList<Bar>();

// fill list with items that are instances of Foo and Bar

for (int i = 0; i < list.size(); i++)
{
  Bar bar = list.get(i);

  if (bar.getClass().getName().equals("com.me.Foo")) // "if(bar instanceof Foo)" never passes
  {
    System.out.println("bar is an instance of Foo");
    Foo foo = (Foo) bar;
  }
}

If this section of code gets run I get this error:

java.lang.ClassCastException: com.me.Foo cannot be cast to com.me.Foo
// stack

However, in another spot, I have a method that returns an instance of Bar and I can check to see if it’s an instance of Foo using instanceof and then cast Bar to Foo with no problems:

Bar bar = returnBar(); // returns an instance of Bar, but the variable 
                       // it returns could be an instance of Foo
if (bar instanceof Foo)
{
  System.out.println("bar is an instance of Foo");
  Foo foo = (Foo) bar;
}

What is different here?

Here’s the real source of the issue:
http://pastie.org/private/jhyhovn7mm4ghxlxsmweog

I’m working with an API called Bukkit which allows for custom plugins for servers for a game called Minecraft. The documentation can be found here for those really interested in the issue. I’m going to go to the official Bukkit forums and try my question there, since it is becoming more of an API specific issue, but I’ll leave all this here for those that were interested in the problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:18:16+00:00Added an answer on May 31, 2026 at 7:18 pm

    My guess is that you’re loading Foo from two places. In your loop, try printing out bar.getClass().getClassLoader() and Foo.class.getClassLoader(). I suspect they’ll show different classloaders. (It’s possible that you’ve got two classloaders loading from the same place, of course, which is equally bad.)

    Basically, as far as the JVM is concerned, two classes with the same name loaded from different classloaders are entirely different – you can’t cast from one to the other. Assuming I’m right about this, the usual solution is to try to work out why they’re being loaded from different classloaders, and fix that.

    Note that when you use instanceof instead, that checks whether the types are really compatible, so it will notice the different classloaders.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm looping through a list of items, and I'd like to get a request
While looping through records in one list, I want to find a corresponding line
I am looping through a large text file and im looking for lines that
Say I have List<SomeObject> objList = new ArrayList<SomeObject>(); If SomeObject contains a field named
I have a list of embedded entities: @Embedded private List<EmbeddedEntity> embedded = new ArrayList<EmbeddedEntity>();
I have a for loop looping through an ArrayList . If a condition is
I have an ArrayList that I loop and through some logic I would remove
While looping through a Dataset which code snippet should I use, should I go
When looping through a DataRow and encountering types such as DataRow dr; dr[someString] dr[someInteger]
I'm currently looping through a datareader and calling the System.Net.Mail.SmtpClient's Send() method. The problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.