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

The Archive Base Latest Questions

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

If I have a class using generic type such as public class Record<T> {

  • 0

If I have a class using generic type such as

public class Record<T> {
    private T value;

    public Record(T value) {
        this.value = value;
    }
}

it is pretty straight forward to type everything during design time, if I know all types that are used such as it is the case in this example:

// I type explicitly
String myStr = "A";
Integer myInt = 1;
ArrayList myList = new ArrayList();

Record rec1 = new Record<String>(myStr);
Record rec2 = new Record<Integer>(myInt);
Record rec3 = new Record<ArrayList>(myList);

What happens if I get a list of objects from “somewhere” where I don’t know the type? How do I assign the type:

// now let's assume that my values come from a list where I only know during runtime what type they have

ArrayList<Object> myObjectList = new ArrayList<Object>();
    myObjectList.add(myStr);
    myObjectList.add(myInt);
    myObjectList.add(myList);

    Object object = myObjectList.get(0);

    // this fails - how do I do that?
    new Record<object.getClass()>(object);
  • 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-25T09:53:30+00:00Added an answer on May 25, 2026 at 9:53 am

    Java generics are not C++ Templates.

    Java generics are a compile time feature, not a run time feature.

    Here is a link to the Java generics Tutorial.

    This can never work with Java:

    new Record<object.getClass()>(object);
    

    You must either use polymorphism (say, each object implements a known interface) or RTTI (instanceof or Class.isAssignableFrom()).

    You might do this:

         class Record
         {
           public Record(String blah) { ... }
           public Record(Integer blah) { ... }
           ... other constructors.
         }
    

    or you might use the Builder pattern.

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

Sidebar

Related Questions

I have a base class Parent like this: using System; using System.Collections.Generic; using System.Text;
I have such generic class of list: using System; using System.Collections; using System.Collections.Generic; using
This is best explained using code. I have a generic class that has a
I have a C# class the returns a List, using System.Collections.Generic Lists not F#
I have a mutable class that I'm using as a key to a generic
I have the simple class using auto-implemented properies: Public Class foo { public foo()
I have an application that opens another class using intent : private void createRepository(){
I have a project that is using a generic list of a custom class.
If have a type hierarchy that starts out with a generic type class A<T>
I am using a fictional example for this. Say, I have a Widget class

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.