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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:06:25+00:00 2026-06-13T23:06:25+00:00

How can I create a generic class, which takes the class type from the

  • 0

How can I create a generic class, which takes the class type from the generic type argument that is placed when creating/injecting this generic class?
My goal is to specify eg MyGeneric<User>, and the generic class should then be capable to use the User class in all method calls. Without having to explicitly supplying User.class in the constructor of the generic additionally.

Something like:

class MyGeneric<T> {
    public MyGeneric() {
        someService.create(Class<T>, someString);
    }
}

class Usage {
    @Inject
    MyGeneric<User> myuser;
}

How is this done propertly?

  • 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-13T23:06:26+00:00Added an answer on June 13, 2026 at 11:06 pm

    What you can do is write the instantiation code in a superclass and then extend it for each particular generic type (little or no code is required in subclasses, but the subclasses are mandatory as they’re the only way to avoid type erasure):

    abstract class MyGeneric<T> {
    
        private T instance;
    
        public MyGeneric(String str) {
            // grab the actual class represented by 'T'
            // this only works for subclasses of MyGeneric<T>, not for MyGeneric itself !
            Class<T> genericType = (Class<T>) ((ParameterizedType)getClass().getGenericSuperclass())
                    .getActualTypeArguments()[0];
            try {
                // instantiate that class with the provided parameter
                instance = genericType.getConstructor(String.class).newInstance(str);
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }
    }
    
    class MyUser extends MyGeneric<User> {
        public MyUser() {
            // provide the string to use for instantiating users...
            super("userStr");
        }
    }
    
    class User { /*...*/ }
    

    Edit: made the generic class abstract to enforce the usage of subclasses.

    It can also be used with anonymous classes like:

    new MyGeneric<User>("..string...") {}

    I think this is the closest to your initial goal you can get…

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

Sidebar

Related Questions

I want to create a generic class that takes a type parameter and restrict
My goal is to create a trait that a case class can extend, which
I have an interesting problem. I'd like to create a generic class that can
How to create a generic method which can call overloaded methods? I tried but
I want to create a generic html table to excel file view that can
I am currently trying to create a generic instance factory for which takes an
I am trying to create a generic class to write and read Objects to/from
I'm trying to make a generic class that takes 3 types, either a simple
I want to create a generic function that takes any Map & a String
I'm trying to create an EJB factory class, which works like this: You have

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.