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 8211603
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T10:28:51+00:00 2026-06-07T10:28:51+00:00

This is an Android app, but presumably it happens the same in Java. I

  • 0

This is an Android app, but presumably it happens the same in Java. I have a type LevelFactory from which I derive Level1Factory, Level2Factory, etc. I want to have an array of these classes so I can instantiate a level given the array index.

I can just have a Class[] and put them in that and then just cast them to LevelFactory when I need to use them, but I was wondering what the proper thing to do is.

This is obviously an error “Incompatible types”:

new Class<LevelFactory>[] {Level1Factory.class,Level2Factory.class};

However, I was surprised to see that this is also an error “Generic array creation”:

new Class<? extends LevelFactory>[] {Level1Factory.class,Level2Factory.class};

The following works, but it gives the “Unchecked assignment” warning when assigned to a variable:

new Class[] {Level1Factory.class,Level2Factory.class};

The last is the only option I can get to work. I just ignore the warning, but I would like to do it using generics if that’s actually possible.

  • 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-06-07T10:28:53+00:00Added an answer on June 7, 2026 at 10:28 am

    Two questions:

    1. Do you really need an Array? Arrays don’t work great with generics. So an ArrayList<LevelFactory> might be the better solution

    2. Do you really need the downcast to the special type (Level1Factory, Level2Factory)? If they have a common super method which is defined in LevelFactory (lets say Level getLevel()) you should not need to downcast them. Just call getLevel() and you get the correct Level instance from your factory.

    Another note because this seems to be a common pitfall:

    new Class<? extends LevelFactory>

    This is not a valid statement (it does not matter if its an array or not). <? extends T> is only valid for the type on the left side. It defines that the generic type of the created Object can be T or derived from T. For Collections this does not mean that they can store objects of T or derived from T (which can a Collections of T anyway).

    1. List<LevelFactory> list = new ArrayList<LevelFactory>() This means you can add objects of LevelFactory, Level1Factory and Level2Factory to list. When you want to receive objects from list they are of type LevelFactory.

    2. List<? extends LevelFactory> list = new ArrayList<LevelFactory>() Means you can receive objects of LevelFactory from list. BUT you cannot add any object to list in a typesafe way because you don’t know the exact generic type of list. That because you can also assign new ArrayList<Level1Factory>() to list. Which means that you can’t even add LevelFactory objects to list because they don’t implement Level1Factory.

    In general <? extends Something> on collections is not what you want in most cases.

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

Sidebar

Related Questions

I have successfully implemented this from android to a java httpservlet on google app
I have an Android app which includes an activity called Help This uses a
I am developing an android app in which I have 3 spinners populated from
my android app crash before load main.xml. This is the exception thrown java.lang.RuntimeException: Unable
I've got an Android app which has a periodic background Service. I want this
I'm developing an Android app but I have a problem. When I try to
I have been trying to get this android service working but I can not
i have published android app but not appearing in android market.i m using android
I have camera preview on my android app but when I add an 'id'
I've read this Android save app settings/data in Internal/External Storage but I don't think

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.