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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:16:49+00:00 2026-06-07T14:16:49+00:00

As we know Java uses erasure, so any Generic class cannot do T t

  • 0

As we know Java uses erasure, so any Generic class cannot do

T t = new T();

So I was trying out Java reflection to have a class with Static methods, to instantiate any Class with any Constructor. Here is the code.

import java.lang.reflect.*;

public class GenericNewInstance {
    public static <T> T createInstance(Class<T> cObj) {
        try {
            return cObj.newInstance();
        } catch (InstantiationException e) {
            System.out.println("Instantiation Exception");
            return null;
        } catch (IllegalAccessException e) {
            System.out.println("Illegal Access Exception");
            return null;
        }
    }

    public static <T> T createInstanceUsingRelection(Class<T> c, Object... initArgs) {
        Constructor<T> cTor = null;
        Class<?>[] cObjs = new Class<?>[initArgs.length];
        int i = 0;

        for(Object o : initArgs) {
            cObjs[i++] = o.getClass();
        }

        try {
            cTor = c.getConstructor(cObjs);
        } catch (SecurityException e) {
            System.out.println("security exception. Cannot get Constructor");
            return null;
        } catch (NoSuchMethodException e) {
            System.out.println("NoSuchMethodException Cannot get constructor");
            return null;
        }

        try {
            return cTor.newInstance(initArgs);
        } catch (IllegalArgumentException e) {
            System.out.println("Illegal Argument Exception");
            return null;
        } catch (InstantiationException e) {
            System.out.println("Instantiation Exception");
            return null;
        } catch (IllegalAccessException e) {
            System.out.println("Illegal Access Exception");
            return null;
        } catch (InvocationTargetException e) {
            System.out.println("Invocation Target Exception");
            return null;
        }
    }
}

Example for using this.

Integer i = GenericNewInstance.createInstanceUsingRelection(Integer.class, "0");

So my questions:

  1. Is this the right way to implement it? (or is it verbose?)
  2. What are the typical use cases of doing this?
  3. Can/Should we avoid using Reflection while 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-07T14:16:51+00:00Added an answer on June 7, 2026 at 2:16 pm

    Your code will fail at c.getConstructor(cObjs) since this doesn’t take into account the type hierarchy. If any argument is a subtype of the constructor’s declared param type, this call will not return it. You’ll need quite a lot more type juggling to get it working. I advise you to take a look at the code that already solves this problem. Perhaps you can even use that library as-is, your choice. It’s the implementation code for Clojure, a JVM-based dynamic language that needs exactly this stuff. The library is available from the Maven central repo.

    BTW Your exception handling is redundant. Either just declare throws Exception or catch any Exception and wrap it in a RuntimeException. When something fails, the original exception is the best diagnostic.

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

Sidebar

Related Questions

I know Java's generics are somewhat inferior to .Net's. I have a generic class
I know that Open Office Database uses a java database backend. Does anyone have
I know java well, and have some experience in EclipseLink, Hibernate, JSF, Grails and
As we all know Java program will start executing from the public static void
I'm new to Scala and don't know Java. I want to create a jar
I know that Java have its own garbage collection, but sometimes I want to
I'm debugging some Java code that uses Apache POI to pull data out of
I have been developing a small Java utility that uses two frameworks: Encog and
I have built a horizontal scrolling website. I don't really know much about Java
While debugging Java code that uses reflection in Eclipse, some times I need to

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.