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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T23:35:25+00:00 2026-05-10T23:35:25+00:00

Is it possible to reflectively instantiate a generic type in Java? Using the technique

  • 0

Is it possible to reflectively instantiate a generic type in Java? Using the technique described here I get an error because class tokens cannot be generic. Take the example below. I want to instantiate some subclass of Creator that implements Creator. The actual class name is passed in as a command line argument. The idea is to be able to specify an implementation of Creator at runtime. Is there another way to accomplish what I’m trying to do here?

public interface Creator<T> {     T create(); } public class StringCreator implements Creator<String> {     public String create() { return new String(); } } public class FancyStringCreator implements Creator<String> {     public String create() { return new StringBuffer().toString(); } } public static void main(String[] args) throws Exception {     Class<?> someClass = Class.forName(args[0]);     /*ERROR*/Class<? extends Creator<String>> creatorClass = someClass.asSubclass(Creator.class);     Constructor<? extends Creator<String>> creatorCtor = creatorClass.getConstructor((Class<?>[]) null);     Creator<String> creator = creatorCtor.newInstance((Object[]) null); } 

Edit: I like Marcus’ approach as being the most simple and pragmatic without circumventing the whole generics thing. I can use it in my situation because I can specify that the class passed must be a subclass of StringCreator. But as Ericson pointed out the generic information is still there at the type level, just not at the runtime level so it is still possible to reflectively examine whether a given class implements the correct generic type.

  • 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. 2026-05-10T23:35:26+00:00Added an answer on May 10, 2026 at 11:35 pm

    The generic information is lost in runtime. There is no runtime equivalent of a Creator<String>.class. You could create a type between Creator and StringCreator which fixes the generic type:

    public interface Creator<T> {         T create(); } public interface StringCreator extends Creator<String> { } public class StringCreatorImpl implements StringCreator  {         public String create() { return new String(); } } public class FancyStringCreator implements StringCreator  {         public String create() { return new StringBuffer().toString(); } } public static void main(String[] args) throws Exception {         Class<?> someClass = Class.forName(args[0]);         Class<? extends StringCreator> creatorClass = someClass.asSubclass(StringCreator.class);         Constructor<? extends StringCreator> creatorCtor = creatorClass.getConstructor((Class<?>[]) null);         Creator<String> creator = creatorCtor.newInstance((Object[]) null); } 

    But of course you lose a bit of flexibility, because you cannot use the following creator class:

    public class AnotherCreator implements Creator<String> {     public String create() { return ''; } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 123k
  • Answers 123k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It should just be a matter of deleting the xampp… May 12, 2026 at 1:08 am
  • Editorial Team
    Editorial Team added an answer Even with the more complicated code, you can use an… May 12, 2026 at 1:08 am
  • Editorial Team
    Editorial Team added an answer The error appears to be related to your use of… May 12, 2026 at 1:08 am

Related Questions

I would like to create a delegate and a method that can be used
We have two versions of a managed C++ assembly, one for x86 and one
In Microsoft's UnitTesting namespace ( Microsoft.VisualStudio.TestTools.UnitTesting ) there are AssemblyInitialize and AssemblyCleanup attributes you
One of my developers has started using RegexBuddy for help in interpreting legacy code,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.