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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:53:29+00:00 2026-05-27T09:53:29+00:00

(This is probably a duplicate, but I could not find it – feel free

  • 0

(This is probably a duplicate, but I could not find it – feel free to point it out)

Consider the following Java class:

public class A<T0, T1> {
    public A(T0 t0, T1 t1) {
        ...
    }
}

Instantiating this class is easy using something along the lines of new A<Integer, String>(1, "X").

Suppose now that most instances of this class have a String as the second type parameter T1 and that the object of this type used in the constructor call is also pretty much standard.

If A had not been using generics, a common extension would be an additional constructor without the second argument:

public class A {
    public A(int t0, String t1) {
        ...
    }

    public A(int t0) {
        this(t0, new String("X"));
    }
}

Unfortunately, this does not seem to be possible for a class that does use generics – at least not without a forced cast:

    public A(T0 t0) {
        this(t0, (T1)(...));
    }

The reason? While this constructor only takes a single argument, it still uses two type parameters and there is no way to know a priori that whatever type T1 the user of the class supplies will be compatible with the default value used in the constructor.

A slightly more elegant solution involves the use of a subclass:

 public class B<T0> extends A<T0, String> {
     ...
 }

But this approach forces yet another branch in the class hierarchy and yet another class file with what is essentially boilerplate code.

  • Is there a way to declare a constructor that forces one or more of the type parameters to a specific type? Something with the same effects as using a subclass, but without the hassle?

  • Is there something fundamentally wrong in my understanding of generics and/or my design? Or is this a valid issue?

  • 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-27T09:53:30+00:00Added an answer on May 27, 2026 at 9:53 am

    Easiest method is just to add a static creation method.

    public static <T0> A<T0,String> newThing(T0 t0) {
        return new A<T0,String>(t0, "X");
    }
    

    (Perhaps choose a name appropriate for the particular usage. Usually no need for new String("...").)

    From Java SE 7, you can use the diamond:

    A<Thing,String> a = new A<>(thing);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

NOTE: It's probably a duplicate but I can't find working answer. Following is what
This probably has a simple answer, but I must not have had enough coffee
I know this probably really simple but Im not sure what im doing wrong...
Sorry, I know this is probably a duplicate but having searched for 'python regular
Probably a duplicate, but I can't find the answer by searching with these terms,
Possible Duplicate: PHP: Opening/closing tags & performance? this is probably a stupid question, but
Possible Duplicate: Regex to parse youtube yid your probably thinking not another one, but
This is probably a common situation, but I couldn't find a specific answer on
This is an extension of this question and probably might even be a duplicate
This probably sounds really stupid but I have noo idea how to implement jquery's

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.