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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T16:11:04+00:00 2026-05-12T16:11:04+00:00

Sorry if the title seems confusing, but some examples are in order. Let’s say

  • 0

Sorry if the title seems confusing, but some examples are in order.

Let’s say I have some Java class with a generic type parameter:

public class GenericClass<T> {
}

I can create a variable typed to store an object, with the generic parameter set to, say a String. Java will also let me assign that variable to another variable but with the generic parameter set to the wildcard <?> type:

GenericClass<String> stringy = ...
GenericClass<?> generic = stringy; // OK

However, when working with a class with a generic parameter, if you set the type of that parameter to be generic, you can’t then assign an object of that class to an identically typed/genericized type where the latter (inner/nested) parameter is of the wildcard type <?>:

GenericClass<GenericClass<String>> stringy = ...
GenericClass<GenericClass<?>> generic = stringy; // Compile Error

// And just in case that is confusing, a more
// realistic example involving Collections:
List<GenericClass<String>> stringy = ...
List<GenericClass<?>> generic = stringy; // Compile Error

The specific compile error is:

Type mismatch: cannot convert from List<GenericClass<String>> to List<GenericClass<?>>

Intuitively I would think that the assignment in question should not be a problem. So why is this assignment an 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-12T16:11:05+00:00Added an answer on May 12, 2026 at 4:11 pm

    The problem your facing is denominated Covariance.

    List<GenericClass<String>> stringy = ...
    List<GenericClass<?>> generic = stringy;
    generic.add(new GenericClass<Integer>());
    

    If this wasn’t a compile error, then the last line of code would be possible.

    You could get around the error by doing this:

     List<? extends GenericClass<?>> generic = stringy;
    

    but you can not use add also because you don’t really know what ? extends GenericClass<?> is (Covariance once again). In this case you can only enumerate through the List and expect GenericClass<?>.

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

Sidebar

Related Questions

I'm sorry if my question title seems fundamentally uninformed. Let me explain what I
Sorry for the long title, but it seems most descriptive for my question. Basically,
Sorry for the semi-generic title, but I'm still pretty new at rails and couldn't
Sorry for the title may seem confusing. i have 2 element. with no relation
Sorry about the vague question title, but I have these typedefs here: typedef std::list<AnimationKeyframe>
Sorry for the generic title, but I am no pro when it comes to
I'm sorry if the title is confusing but I couldn't think of anything similar
Sorry the title may not be very clear. Basically I have a wrapper for
Sorry if the title isn't clear enough, but in Javascript you can do this
Sorry if the title doesn't make sense. Basically I have a series of strings

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.