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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T08:40:45+00:00 2026-05-12T08:40:45+00:00

Java can often infer generics based on the arguments (and even on the return

  • 0

Java can often infer generics based on the arguments (and even on the return type, in contrast to e.g. C#).

Case in point: I’ve got a generic class Pair<T1, T2> which just stores a pair of values and can be used in the following way:

Pair<String, String> pair = Pair.of("Hello", "World");

The method of looks just like this:

public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second) {
    return new Pair<T1, T2>(first, second);
}

Very nice. However, this no longer works for the following use-case, which requires wildcards:

Pair<Class<?>, String> pair = Pair.of((Class<?>) List.class, "hello");

(Notice the explicit cast to make List.class the correct type.)

The code fails with the following error (provided by Eclipse):

Type mismatch: cannot convert from TestClass.Pair<Class<capture#1-of ?>,String> to TestClass.Pair<Class<?>,String>

However, explicitly calling the constructor still works as expected:

Pair<Class<?>, String> pair =
    new Pair<Class<?>, String>((Class<?>) List.class, "hello");

Can someone explain this behaviour? Is it by design? Is it wanted? Am I doing something wrong or did I stumble upon a flaw in the design / bug in the compiler?

Wild guess: the “capture#1-of ?” somehow seems to imply that the wildcard is filled in by the compiler on the fly, making the type a Class<List>, and thus failing the conversion (from Pair<Class<?>, String> to Pair<Class<List>, String>). Is this right? Is there a way to work around this?


For completeness’ sake, here is a simplified version of the Pair class:

public final class Pair<T1, T2> {
    public final T1 first;
    public final T2 second;

    public Pair(T1 first, T2 second) {
        this.first = first;
        this.second = second;
    }

    public static <T1, T2> Pair<T1, T2> of(T1 first, T2 second) {
        return new Pair<T1, T2>(first, second);
    }
}
  • 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-12T08:40:45+00:00Added an answer on May 12, 2026 at 8:40 am

    The reason the constructor works is that you’re explicitly specifying the type parameters. The static method also will work if you do that:

    Pair<Class<?>, String> pair = Pair.<Class<?>, String>of(List.class, "hello");
    

    Of course, the whole reason you have a static method in the first place is probably just to get the type inference (which doesn’t work with constructors at all).

    The problem here (as you suggested) is that the compiler is performing capture conversion. I believe this is as a result of [§15.12.2.6 of the JLS]:

    • The result type of the chosen method is determined as follows:
      • If the method being invoked is declared with a return type of void,
        then the result is void.
      • Otherwise, if unchecked conversion was necessary for the
        method to be applicable then the
        result type is the erasure (§4.6) of
        the method’s declared return type.
      • Otherwise, if the method being invoked is generic, then for 1in, let
        Fi be the formal type parameters of
        the method, let Ai be the actual type
        arguments inferred for the method
        invocation, and let R be the declared
        return type of the method being
        invoked. The result type is obtained
        by applying capture conversion
        (§5.1.10) to R[F1 := A1, …, Fn :=
        An].
      • Otherwise, the result type is obtained by applying capture
        conversion (§5.1.10) to the type given
        in the method declaration.

    If you really want the inference, one possible workaround is to do something like this:

    Pair<? extends Class<?>, String> pair = Pair.of(List.class, "hello");
    

    The variable pair will have a wider type, and it does mean a bit more typing in the variable’s type name, but at least you don’t need to cast in the method call anymore.

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

Sidebar

Ask A Question

Stats

  • Questions 195k
  • Answers 195k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It is expecting an NSArray of MPURLRequestParameter objects. Found the… May 12, 2026 at 6:59 pm
  • Editorial Team
    Editorial Team added an answer Assuming you mean the ScrollTo plugin, the documentation says you… May 12, 2026 at 6:59 pm
  • Editorial Team
    Editorial Team added an answer Brett Porter posted a blog on Configuring Maven HTTP Connections… May 12, 2026 at 6:59 pm

Related Questions

Code like this often happens: l = [] while foo: # baz l.append(bar) #
What do you think is best practice when creating public header files in C++?
I tried to read the related questions and didn't find any new tool. Here
Often I need to read big XML files (> 100 MB) which have a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.