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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T21:22:59+00:00 2026-06-07T21:22:59+00:00

I’ve always been wondering about some weird aspect of Java generics and the use

  • 0

I’ve always been wondering about some weird aspect of Java generics and the use of wildcards. Let’s say, I have the following API:

public interface X<E> {
    E get();
    E set(E e);
}

And then, let’s say we declare the following methods:

public class Foo {
    public void foo(X<?> x) {
        // This does not compile:
        x.set(x.get());
    }

    public <T> void bar(X<T> x) {
        // This compiles:
        x.set(x.get());
    }
}

From my “intuitive” understanding, X<?> is in fact the same as X<T>, except that the unknown <T> is formally unknown to client code. But inside of foo(), I would guess that the compiler could infer (pseudo JLS code) <T0> := <?>[0], <T1> := <?>[1], etc.... This is what most programmers do explicitly and intuitively. They delegate to a private helper method, which leads to a lot of useless boiler-plate code:

public class Foo {
    public void foo(X<?> x) {
        foo0(x);
    }

    private <T> void foo0(X<T> x) {
        x.set(x.get());
    }
}

Another example:

public class Foo {
    public void foo() {
        // Assuming I could instanciate X
        X<?> x = new X<Object>();
        // Here, I cannot simply add a generic type to foo():
        // I have no choice but to introduce a useless foo0() helper method
        x.set(x.get());
    }
}

In other words, the compiler knows that the wildcard in x.set() is formally the same as the wildcard in x.get(). Why can’t it use that information? Is there a formal aspect in the JLS, that explains this lacking compiler “feature”?

  • 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-07T21:23:01+00:00Added an answer on June 7, 2026 at 9:23 pm

    You wonder why isn’t it supported? An equally valid question is, why should it be supported?

    The way wildcards work, given the declaration X<? extends Y> x, the type of the expression x.get() is not ? extends Y or some type inferred by the compiler, but Y. Now, Y isn’t assignable to ? extends Y, so you can’t pass x.get() to x.set which has the effective signature of void set(? extends Y e). The only thing you could actually pass to it is null, which is assignable to every reference type.

    I follow the JDK developer mailing lists fairly closely, and the general spirit there is that every feature the JLS specifies should pull its own weight – the cost/benefit ratio should be heavy on the benefit side. Now, keeping track of the origin of a value would be quite possible, but it would only solve one special case, which has several other ways to solve it. Consider these classes:

    class ClientOfX {
        X<?> member;
        void a(X<?> param) {
            param.set(param.get());
        }
        void b() {
            X<?> local = new XImpl<Object>();
            local.set(local.get());
        }
        void c() {
            member.set(member.get());
        }
        void d() {
            ClassWeCantChange.x.set(ClassWeCantChange.x.get());
        }
    }
    
    class ClassWeCantChange {
        public static X<?> x;
    }
    

    This obviously doesn’t compile, but with your proposed enhancement, it does. However, we can get three of the four methods compiling without changing the JLS:

    1. For a, when we receive the generic object as a parameter, we can make the method generic and receive an X<T> instead of X<?>.
    2. For b and c, when using a locally declared reference, we don’t need to declare the reference with a wildcard (Eric Lippert of Microsoft’s C# compiler team calls this situation “if it hurts when you do that, then don’t do that!“).
    3. For d, when using a reference whose declaration we can’t control, we have no option but to write a helper method.

    So the smarter type system would really only help in the case where we can’t control the declaration of a variable. And in that situation, the case for doing what you want to do is kind of sketchy – the very act of wildcarding a generic type normally means that the object is either intended to work only as a producer (? extends Something) or a consumer (? super Something) of objects, not both.

    The TL;DR version is that it would bring little benefit (and require wildcards to be something else than what they are today!) while adding complexity to the JLS. If someone comes up with a very compelling case to add it, the spec could be extended – but once it’s there, it’s there forever, and it may cause unforeseen problems down the road, so leave it out until you really need it.

    Edit: The comments contain more discussion about what wildcards are and aren’t.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is

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.