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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:12:20+00:00 2026-05-16T11:12:20+00:00

I have two classes with nested generics. Is there a way to get rid

  • 0

I have two classes with nested generics. Is there a way to get rid of the

Type mismatch: cannot convert from Msg<Value<String>> to Msg<Value<?>> error ?
In the last assignment

public class Value<V> {
    V   val;

    public Value(V val) {
        this.val = val;
    }
    @Override
    public String toString() {
        return "" + val;
    }
}

public class Msg<T> {

    T holder;

    public Msg( T holder) {
        this.holder = holder ;
    }
    public String toString() {
        return "" + holder;
    }

    public static void main(String[] args) {
        Msg<Value<String>>strMsg = new Msg(new Value<String>("abc"));
        // This is OK
        Msg<?>objMsg = strMsg;
        // Type mismatch: cannot convert from Msg<Value<String>> to Msg<Value<?>>   
        Msg<Value<?>>objMsg = strMsg;
    }
}
  • 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-16T11:12:21+00:00Added an answer on May 16, 2026 at 11:12 am

    Use the following:

    Msg<? extends Value<?>> someMsg = strMsg;
    

    The problem is that the ? in Msg<Value<?>> objMsg is NOT capable of capture conversion. It’s not “a Msg of Value of some type. It’s “a Msg of Value of ANY type”.

    This also explains why along with the declaration change, I’ve also renamed the variable to someMsg. The Value can’t just be any Object. It must belong to some type (String in this example).


    A more generic example

    Let’s consider a more generic example of a List<List<?>>. Analogously to the original scenario, a List<List<?>> can NOT capture-convert a List<List<Integer>>.

        List<List<Integer>> lolInt = null;
    
        List<List<?>> lolAnything = lolInt;         // DOES NOT COMPILE!!!
        // a list of "lists of anything"
    
        List<? extends List<?>> lolSomething = lolInt;   // compiles fine!
        // a list of "lists of something"
    

    Here’s another way to look at it:

    • Java generics is type invariant
    • There’s a conversion from Integer to Number, but a List<Integer> is not a List<Number>
      • Similarly, a List<Integer> can be capture-converted by a List<?>, but a List<List<Integer>> is not a List<List<?>>
    • Using bounded wildcard, a List<? extends Number> can capture-convert a List<Integer>
      • Similarly, a List<? extends List<?>> can capture-convert a List<List<Integer>>

    The fact that some ? can capture and others can’t also explains the following snippet:

        List<List<?>> lolAnything = new ArrayList<List<?>>(); // compiles fine!
    
        List<?> listSomething = new ArrayList<?>(); // DOES NOT COMPILE!!!
            // cannot instantiate wildcard type with new!
    

    Related questions

    • Multiple wildcards on a generic methods makes Java compiler (and me!) very confused
      • Very long and detailed exploration into this problem
    • Java Generic List<List<? extends Number>>
    • Any simple way to explain why I cannot do List<Animal> animals = new ArrayList<Dog>()?
    • What is the difference between <E extends Number> and <Number>?

    See also

    • Java Generics Tutorial
      • Generics and Subtyping | Wildcards | More Fun with Wildcards
    • Angelika Langer’s Java Generics FAQ
      • What is a bounded wildcard?
      • Which super-subtype relationships exist among instantiations of generic types?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two classes (MVC view model) which inherits from one abstract base class.
I have two classes A and B generated by cxf-codegen-plugin from my WSDL. A
I have two classes, Item and SoldItem . SoldItem inherits from Item adding some
I have two classes, one that inherits from the other. The base class is
how to implement inner outer classes in c# i have two nested classes like
I have two classes, one nested in the other. Public Class Operation Public Property
I have 3 classes, two inherit from 1: public class Employee { private virtual
I found that two nested classes in java have access to each other's private
I have two classes: class Customer { public string Name { get; set; }
What I'm talking about here are nested classes. Essentially, I have two classes that

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.