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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:23:40+00:00 2026-05-23T20:23:40+00:00

class pair<U,V>{ U first; V second; public pair() { first = new U(); //error

  • 0
class pair<U,V>{
U first;
V second;
public pair() {
    first = new U(); //error
    second = new V(); //error
}   
public pair(U f,V s){
    first = f;
    second = s;
}
}

required: class
found: type parameter

Is it possible to initialize first/second with (without-arguments) constructor of U/V type other way?

  • 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-23T20:23:42+00:00Added an answer on May 23, 2026 at 8:23 pm

    Java does not normally allow this because of type erasure. You can specify constructor arguments of type Class<U> and Class<V>, for which you would pass in concrete class types of the given type parameters (i.e., Integer.class and String.class for <Integer> and <String>).

    It is also possible to extract the type using bytecode-level reflection, but quite complicated, and it doesn’t always work in all situations. If you scroll down on this article, you can find the example that makes this possible. I’ve pasted it below for convenience.

    static public Type getType(final Class<?> klass, final int pos) {
        // obtain anonymous, if any, class for 'this' instance
        final Type superclass = klass.getGenericSuperclass();
    
        // test if an anonymous class was employed during the call
        if ( !(superclass instanceof Class) ) {
            throw new RuntimeException("This instance should belong to an anonymous class");
        }
    
        // obtain RTTI of all generic parameters
        final Type[] types = ((ParameterizedType) superclass).getActualTypeArguments();
    
        // test if enough generic parameters were passed
        if ( pos < types.length ) {
            throw RuntimeException(String.format("Could not find generic parameter #%d because only %d parameters were passed", pos, types.length));
        }
    
        // return the type descriptor of the requested generic parameter
        return types[pos];
    }
    

    Edit: for reply to comment:

    class pair<U,V>{
        U first;
        V second;
        public pair(Class<U> cu, Class<V> cv) {
            try {
                first = cu.newInstance();
                second = cv.newInstance();
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }   
        public pair(U f,V s){
            first = f;
            second = s;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is a C#'s analog of std::pair in C++? I found System.Web.UI.Pair class, but
To me at least, the Pair class is a very multi-purpose class. So why
class A : IFoo { } ... A[] arrayOfA = new A[10]; if(arrayOfA is
class AbstractQuery { virtual bool isCanBeExecuted()=0; public: AbstractQuery() {} virtual bool Execute()=0; }; class
class C { T a; public: C(T a): a(a) {;} }; Is it legal?
The code below is first the client code, then the class file. For some
those are the maps: multimap<SortKey,T> firstMap; multimap<SearchKey,pair<SortKey,T>*> secondMap; template <class T,class SortKey, class SearchKey>
class Foo { static bool Bar(Stream^ stream); }; class FooWrapper { bool Bar(LPCWSTR szUnicodeString)
class Tag(models.Model): name = models.CharField(maxlength=100) class Blog(models.Model): name = models.CharField(maxlength=100) tags = models.ManyToManyField(Tag) Simple
class someclass {}; class base { int a; int *pint; someclass objsomeclass; someclass* psomeclass;

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.