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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:54:33+00:00 2026-05-11T17:54:33+00:00

I have a class that maps incoming messages to matching readers based on the

  • 0

I have a class that maps incoming messages to matching readers based on the message’s class. All message types implement the interface message. A reader registers at the mapper class, stating which message types it will be able to handle. This information needs to be stored in the message reader in some way and my approach was to set a private final array from the constructor.

Now, it seems I have some misunderstanding about generics and / or arrays, that I can’t seem to figure out, see the code below. What is it?

public class HttpGetMessageReader implements IMessageReader {
    // gives a warning because the type parameter is missing
    // also, I actually want to be more restrictive than that
    // 
    // private final Class[] _rgAccepted;

    // works here, but see below
    private final Class<? extends IMessage>[] _rgAccepted;

    public HttpGetMessageReader()
    {
        // works here, but see above
        // this._rgAccepted = new Class[1];

        // gives the error "Can't create a generic array of Class<? extends IMessage>"
        this._rgAccepted = new Class<? extends IMessage>[1];

        this._rgAccepted[0] = HttpGetMessage.class;
    }
}

ETA:
As cletus correctly pointed out, the most basic googling shows that Java does not permit generic arrays. I definitely understand this for the examples given (like E[] arr = new E[8], where E is a type parameter of the surrounding class). But why is new Class[n] allowed? And what then is the “proper” (or at least, common) way to do this?

  • 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-11T17:54:33+00:00Added an answer on May 11, 2026 at 5:54 pm

    Java does not permit generic arrays. More information in the Java Generics FAQ.

    To answer your question, just use a List (probably ArrayList) instead of an array.

    Some more explanation can be found in Java theory and practice: Generics gotchas:

    Generics are not covariant

    While you might find it helpful to
    think of collections as being an
    abstraction of arrays, they have some
    special properties that collections do
    not. Arrays in the Java language are
    covariant — which means that if
    Integer extends Number (which it
    does), then not only is an Integer
    also a Number, but an Integer[] is
    also a Number[], and you are free to
    pass or assign an Integer[] where a
    Number[] is called for. (More
    formally, if Number is a supertype
    of Integer, then Number[] is a
    supertype of Integer[].) You might
    think the same is true of generic
    types as well — that List<Number>
    is a supertype of List<Integer>, and
    that you can pass a List<Integer>
    where a List<Number> is expected.
    Unfortunately, it doesn’t work that
    way.

    It turns out there’s a good reason it
    doesn’t work that way: It would break
    the type safety generics were supposed
    to provide. Imagine you could assign a
    List<Integer> to a List<Number>.
    Then the following code would allow
    you to put something that wasn’t an
    Integer into a List<Integer>:

    List<Integer> li = new ArrayList<Integer>();
    List<Number> ln = li; // illegal
    ln.add(new Float(3.1415));
    

    Because ln is a List<Number>, adding
    a Float to it seems perfectly legal.
    But if ln were aliased with li, then
    it would break the type-safety promise
    implicit in the definition of li —
    that it is a list of integers, which
    is why generic types cannot be
    covariant.

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

Sidebar

Related Questions

Anyone have a suggestion for a good utility class that maps values from one
I have a class that implements the IDisposable interface. I am using a webclient
I have a configuration class that maps web.config, something like that: public class SiteConfigurationSection
I have a class with a static std::map member variable that maps char s
I have this class that tries multiple methods of getting data from Google maps
I have a POCO class called Attachment that maps to a table in SqlServer
I have a class that is maps to a field in a database. The
So I have a class with three fields that maps to a table using
I have a class that map objects to objects, but unlike dictionary it maps
I have been assigned to implement an interface to an API that uses XML

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.