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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T07:39:30+00:00 2026-05-28T07:39:30+00:00

Suppose I have an interface interface Foo<T> { void foo(T x); T bar() }

  • 0

Suppose I have an interface

interface Foo<T> { 
    void foo(T x); 
    T bar() 
}

and an object of this type with unknown parameter: Foo<?> baz. Then I can call baz.foo(baz.bar()).

However, now I need to put the value baz.bar() into a collection and call baz.foo() on it later on. Something like

List<???> list; // can I tell the compiler this is the same type as baz's wildcard?
list.add(baz.bar());
...
baz.foo(list.get(1));

This doesn’t work either:

List<Object> list;
list.add(baz.bar());
...
baz.foo((???) list.get(1)); // I can't write down the type I need to cast to

Is there a way to do this?

EDIT: The above was oversimplified from my actual situation. Say we have

class Bar {
    private final Foo<?> foo;
    private List<???> list; // the type argument can be selected freely

    Bar(Baz baz) {
        foo = baz.getFoo(); // returns Foo<?>, can't be changed 
    }

    void putBar() {
        list.add(foo.bar());
    }

    void callFoo() {
        foo.foo(list.get(0));
    }
}
  • 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-28T07:39:31+00:00Added an answer on May 28, 2026 at 7:39 am

    This is an awkward problem.

    The solution is to find a scope which encloses both the uses of the wildcard, put a type variable on it, replace the wildcards with references to the type variable, and then bind the type variable.

    In your case, it sounds like the class is the necessary scope. So:

    public class Whatever<T> {
        private List<T> list;
        private Foo<T> baz;
    
        public void bazToList() {
            list.add(baz.bar());
        }
    
        public void listToBaz() {
            baz.foo(list.get(1));
        }
    }
    

    The problem is that you have added a type variable to the class. Now, everywhere that this class is used, any mention of its type needs to have a type bound (so Whatever<String>, or Whatever<?>). That can be incredibly annoying if the code using the class doesn’t really care about the type of the things it’s keeping in its list.

    To get around this, you can create a generic holder class. Something like:

    public class Whatever {
        private WhateverHolder<?> holder;
    
        public void bazToList() {
            holder.bazToList();
        }
    
        public void listToBaz() {
            holder.listToBaz();
        }
    
        private static class WhateverHolder<T> {
            private List<T> list;
            private Foo<T> baz;
    
            public void bazToList() {
                list.add(baz.bar());
            }
    
            public void listToBaz() {
                baz.foo(list.get(1));
            }
        }
    }
    

    But that’s pretty ugly. You’re introducing a whole new class, and the runtime overhead of a new object, just to work around some annoying generics.

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

Sidebar

Related Questions

Suppose I have a Java inteface public interface Bar { public void baz(String st)
Suppose we have: interface Foo { bool Func(int x); } class Bar: Foo {
Suppose I have this interface public interface IFoo { ///<summary> /// Foo method ///</summary>
Suppose I have these interfaces: public interface I1 { void foo(); } public interface
Suppose I have this code... foo.h @interface Foo : NSObject { NSString *aString; //
Suppose I have: interface Foo { void doStuff(); } class FooImpl implements Foo {
Suppose I have a method called foo taking 2 Object as parameter. Both objects
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I'm using an interface with a generic type parameter interface Foo<T> { T
Suppose i have a interface like this, interface MyIntf{ void generate(); } and a

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.