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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:24:49+00:00 2026-06-11T17:24:49+00:00

I have a class representing a pair of two values of the same type

  • 0

I have a class representing a pair of two values of the same type (type which can be any of a specific set of types ):

  public class Pair<E extends AClass>{
      private E var1;
      private E var2; 
  }

This class is used by a framework, so it needs a no-argument constructor in which I have to instantiate the 2 variables (var1, var2):

  public class Pair<E extends AClass>{
      private E var1;
      private E var2; 

      public Pair(){
           var1 = invoke constructor of type E; 
           var2 = invoke constructor of type E 
      }
  }

There are obviously a number of problems here:

  1. In order to instantiate the variables I should somehow know its exact type and invoke that specific type’s constructor; in the best case this means to have a pretty large if else statement in the constructor, something like:

     public Pair(){
           if(var1 instanceof SpecificType1){
              var1 = new SpecificType1(); 
              var2 = new SpecificType2();
           }
      }
    
  2. Even if I do as above, I will have some problems because var1 is declared of type E and I will get a type mismatch error when trying to instantiate SpecficType1 and to assign the resulted object to var1/var2. In order to make it work, I have to cast to E :

       var1 = (E)new SpecificType1();
    

But this destroys the compile time type checking as I’m trying to cast a specific type to a generic type.

Is this a limitation of the Generics in java or is this scenario a bad one for using Generics ?

  • 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-11T17:24:50+00:00Added an answer on June 11, 2026 at 5:24 pm

    In order to instantiate the variables I should somehow know its exact type and invoke that specific type’s constructor; in the best case this means to have a pretty large if else statement in the constructor, something like:

    You’ll run into problems before that.

       if(var1 instanceof SpecificType1){
          var1 = new SpecificType1(); 
          var2 = new SpecificType2();
       }
    

    var1 is null at this point, so var1 instanceof T is false for all T.


    One limitation of Java generics is that generic type parameters are erased so there’s no way that you can reflect on the type parameter from a zero-argument constructor.

    The caller has to provide some context to tell you how to initialize var1 and var2, and the typical way to provide that context is via constructor arguments.


    Your best option is probably to let var1 and var2 start off null and then delay initialization until such time as you can get the context you need.

    Perhaps

    void init(Class<E> type) {
      if (type.isAssignableFrom(ConcreteType1.class)) {
        var1 = type.cast(new ConcreteType1(...));
        var2 = type.cast(new ConcreteType1(...));
      } else { /* other branches */ }
    }
    

    This isn’t perfect since you still can’t distinguish E extends List<String> from E extends List<Number> but it may be good enough for your case, and the .cast method will give you a type-safe cast to E.


    Alternatively, Guava, Guice, and related libraries provide things like the Supplier<E> interface which may come in handy in an init method.

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

Sidebar

Related Questions

I have a JavaScript class representing a car, which is constructed using two parameters,
I have a class representing a set of values that will be used as
I have a Person class which has a String collection of aliases representing additional
I have a C++ class representing a hierarchically organised data tree which is very
I have a class representing a finite-state machine, which should run in a forever
If I have a custom Ruby class representing some string type, as in class
I have a class representing a table on database that is defined: public class
Suppose I have a class representing order lines , eg public class Line {
I have a datastore model representing items in an ecommerce site: class Item(db.Model): CSIN
I have Class and Student objects. Both have collection of another as property. Which

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.