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

  • Home
  • SEARCH
  • 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 653331
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:22:13+00:00 2026-05-13T22:22:13+00:00

Typically, I’ve seen people use the class literal like this: Class<Foo> cls = Foo.class;

  • 0

Typically, I’ve seen people use the class literal like this:

Class<Foo> cls = Foo.class;

But what if the type is generic, e.g. List? This works fine, but has a warning since List should be parameterized:

Class<List> cls = List.class

So why not add a <?>? Well, this causes a type mismatch error:

Class<List<?>> cls = List.class

I figured something like this would work, but this is just a plain ol’ syntax error:

Class<List<Foo>> cls = List<Foo>.class

How can I get a Class<List<Foo>> statically, e.g. using the class literal?

I could use @SuppressWarnings("unchecked") to get rid of the warnings caused by the non-parameterized use of List in the first example, Class<List> cls = List.class, but I’d rather not.

Any suggestions?

  • 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-13T22:22:14+00:00Added an answer on May 13, 2026 at 10:22 pm

    You can’t due to type erasure.

    Java generics are little more than syntactic sugar for Object casts. To demonstrate:

    List<Integer> list1 = new ArrayList<Integer>();
    List<String> list2 = (List<String>)list1;
    list2.add("foo"); // perfectly legal
    

    The only instance where generic type information is retained at runtime is with Field.getGenericType() if interrogating a class’s members via reflection.

    All of this is why Object.getClass() has this signature:

    public final native Class<?> getClass();
    

    The important part being Class<?>.

    To put it another way, from the Java Generics FAQ:

    Why is there no class literal for concrete parameterized types?

    Because parameterized type has no exact runtime type representation.

    A class literal denotes a Class
    object that represents a given type.
    For instance, the class literal
    String.class denotes the Class
    object that represents the type
    String and is identical to the
    Class object that is returned when
    method getClass is invoked on a
    String object. A class literal can
    be used for runtime type checks and
    for reflection.

    Parameterized types lose their type
    arguments when they are translated to
    byte code during compilation 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 in forming class literals
    such as List<String>.class ,
    List<Long>.class and List<?>.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.

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

Sidebar

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.