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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:05:05+00:00 2026-06-05T10:05:05+00:00

Let’s say I have two constructors in my class: public User (List<Source1> source){ …

  • 0

Let’s say I have two constructors in my class:

public User (List<Source1> source){
...
}

public User (List<Source2> source) {
...
}

Let’s say that both of these constructors provide the same information about a User and are equally valid ways to construct a user for different use cases.

In Java, you can’t do this because of type erasure — Java won’t accept two constructors that have as parameters List< ? >.

So, what is the way to get around this? What is a solution that is not overkill but still respects basic OO? It seems wrong to have to construct a factory method or other interface around this just because Java doesn’t have strong generics support.

Here are the possibilities I can think of:

1) Accept a List<?> as a parameter for the constructor and parse in the constructor which kind of logic you need, or throw an exception if it’s not any of the accepted types.

2) Create a class that accepts either List, constructs the appropriate User object, and returns it.

3) Create wrappers around List<Source1> and List<Source2> that can be passed to the User constructor instead.

4) Subclass this guy with two classes, where all of the functionality is inherited except for the constructor. The constructor of one accepts Source1, the other accepts Source2.

5) Wrap this guy with a builder where are two different builder methods for the two different sources of data for instantiation.

My questions are these:

1) Is the need to do this a flaw with Java, or an intentional design decision? What is the intuition?

2) Which solution is strongest in terms of maintaining good code without introducing unneeded complexity? Why?

This question is similar: Designing constructors around type erasure in Java but does not go into specifics, it just suggests various work-arounds.

  • 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-05T10:05:09+00:00Added an answer on June 5, 2026 at 10:05 am

    The usual approach is to use factory methods:

    public static User createFromSource1(List<Source1> source) {
        User user = new User();
        // build your User object knowing you have Source1 data
        return user;
    }
    
    public static User createFromSource2(List<Source2> source) {
        User user = new User();
        // build your User object knowing you have Source2 data
        return user;
    }
    

    If you only want construction using Source1 or Source2 (ie you don’t have a default constructor), you simply hide your constructor, forcing clients to use your factory methods:

    private User () {
        // Hide the constructor
    }
    

    This problem arises because you can’t name constructors differently, which would be how you’d overcome this if these were normal methods. Because constructor names are fixed as the class name, this code pattern is only way to distinguish then give the same type erasure.

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

Sidebar

Related Questions

Let's say I have a structure named vertex with a method that adds two
Let's say I have the following classes : public class MyProductCode { private String
Let's say we have a table that has 2 million rows. It has two
Let's say we have the following: abstract class A; class B : public A;
Let's say we have: @Entity public class Order { @Id private int id; @OneToMany(mappedBy=order)
Let's say I have a base class 'A' and two classes 'B' and 'C',
Let's say i have two tables in db: Car and Part. Car owns arbitrialy
Let's say I have two tables orgs and states orgs is (o_ID, state_abbr) and
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have an facebook application running using the JS SDK. First user

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.