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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T16:51:38+00:00 2026-06-17T16:51:38+00:00

I need to pass the class of a generic type into a class’s constructor.

  • 0

I need to pass the class of a generic type into a class’s constructor. The class is SpiceRequest from the RoboSpice Android library for a reference to the constructor.

It seems odd that the class requires passing the generic’s class into the contstructor, when it could be accessed from the generic type itself, in this case RESULT.class, but maybe I’m wrong about this. Anyway, I’m not looking to change the library’s code, but rather need to use a generic type for the generic type of SpiceRequest, Map<String, ? extends Object>. Here’s my code:

SpiceRequest<Map<String, ? extends Object>> request =
        new SpiceRequest<Map<String, ? extends Object>>(???) {
            ...
        };

And the signature of the SpiceRequest constructor:

public SpiceRequest(final Class<RESULT> clazz) {
    ...
}

For ??? I have tried Map.class with the compiler error: The constructor SpiceRequest<Map<String,? extends Object>>(Class<Map>) is undefined.

Map<String, ? extends Object>.class gives the error: Syntax error on tokens, PrimitiveType expected instead, specifically underlining ? extends Object. It also gives the same error as Map.class.

And Map.<String, ? extends Object>class gives the same compiler error as well.

What is the correct way to get the generic class Class<Map<String, ? extends Object>>?

  • 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-17T16:51:38+00:00Added an answer on June 17, 2026 at 4:51 pm

    There are no class literals for concrete parameterized types or wildcard parameterized types. From Angelika Langer’s generics tutorial:

    Wildcard parameterized types lose their type arguments when they are
    translated to byte code in a process called type erasure. As a side
    effect of type erasure, all instantiations of a generic type share
    the same runtime representation, namely that of the corresponding raw
    type
    . In other words, parameterized types do not have type
    representation of their own. Consequently, there is no point to
    forming class literals such as List<?>.class , List<? extends
    Number>.class
    and List<Long>.class, since no such Class objects
    exist. Only the raw type List has a Class object that represents its
    runtime type. It is referred to as List.class.

    There are no class literals for concrete parameterized types for the same reasons, which in a nutshell are type erasure.

    For your purposes, you will just need to do an unchecked cast of the class literal:

    Class<Map<String, ?>> c = (Class<Map<String, ?>>)(Class<?>)Map.class;
    

    Note that the double cast through Class<?> is necessary because a direct conversion from Class<Map> to Class<Map<String, ?>> is illegal.

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

Sidebar

Related Questions

I need to call a super constructor that requires me to pass a .class
I am trying pass class as value in hashmap. I need to get the
I have posted my class below and with sample data. I need to pass
I need to pass an extra parameter :mobilejs => true from jQuery to a
I need to pass a variable which is captured from client side via jquery
I need to pass a parameter from an EditText and when I click the
I need to pass audio data into a 3rd party system as a 16bit
I have a generic type that I am injecting into a service. Because of
i am trying to invoking method using reflect, here i need to pass generic
Is it possible to create a Class object purely from a generic parameter? For

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.