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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T22:30:01+00:00 2026-05-30T22:30:01+00:00

How can I instantiate a class instance in java with generics? I am trying

  • 0

How can I instantiate a class instance in java with generics?

I am trying to read data from XML file and then instantiate an object so that I can add the other properties of the object (read from the XML file) into the object. I thought the easiest way (so that I don’t have to read the class method names and find the setters) would be to instantiate the object with all the values in the constructor.

I’m trying to do something like this so I need to have something like: T obj = new Object() but get the objects class

    private static final boolean string_field = true;

    private static <T> T getObject(Element e, String[] fieldNames, boolean[] fieldTypes) {
        Object[] values = new Object[fieldNames.length];
        for (int i=0; i<fieldNames.length; i++) {
            values[i] = (fieldTypes[i]==string_field)? getStringValue(e, fieldNames[i])
                    : getIntegerValue(e, fieldNames[i]);
        }
        return new T(values);
    }

Thanks for your advice.

EDIT:

This is my updated code (untested):

    public static <T> List<T> populateObjectList(Document xmlDoc, String tagName,
            Class clazz, String[] fieldNames, Class[] fieldTypes) {
        List<T> objList = new ArrayList<T>();
        NodeList nl = xmlDoc.getElementsByTagName(tagName);
        if (nl!=null && nl.getLength()>0) {
            for (int i=0; i<nl.getLength(); i++) {
                Element e = (Element) nl.item(i);
                T t;
                try {
                    t = getObject(e, clazz, fieldNames, fieldTypes);
                    objList.add(t);
                } catch (InstantiationException ex) {
                } catch (IllegalAccessException ex) {
                } catch (IllegalArgumentException ex) {
                } catch (InvocationTargetException ex) {
                } catch (NoSuchMethodException ex) {
                }
            }
        }
        return objList;
    }

    private static <T> T getObject(Element e, Class clazz, String[] fieldNames, Class[] fieldTypes)
            throws InstantiationException, IllegalAccessException, IllegalArgumentException,
            InvocationTargetException, NoSuchMethodException {
        Object[] initargs = new Object[fieldNames.length];
        for (int i=0; i<fieldNames.length; i++) {
            initargs[i] = (fieldTypes[i].getName().equals("int"))?
                getIntegerValue(e, fieldNames[i])
                : getStringValue(e, fieldNames[i]);
        }
        return (T) clazz.getConstructor(fieldTypes).newInstance(initargs);
    }

I posted this so that some of you would understand what I’m trying to do.

Thanks again everyone for their advice.

  • 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-30T22:30:02+00:00Added an answer on May 30, 2026 at 10:30 pm

    You cannot instantiate a generic type. Java generics have an annoying feature called type erasure, which basically means that the JVM doesn’t know what type was used for a generic class or method at runtime. This is for backwards compatibility with pre-generic versions of generic classes, such as collections.

    What you can do is add a Class<T> clazz to your parameter list, then replace the end of the method with this:

    Class<?>[] paramTypes = new Class[values.length];
    for (int i = 0; i < paramTypes.length; i++) {
        paramTypes[i] = values[i].getClass();
    }
    return clazz.getConstructor(paramTypes).newInstance(values);
    

    This will throw an exception if the class doesn’t have a constructor that takes the correct type and number of arguments in the correct order.

    CAVEAT: This only works if the constructor’s parameter types are exactly the same as the object types. For instance, if values consists of { "Hello", new Integer(2) }, then this will only find a constructor with signature SomeClass(String, Integer), not one with signature SomeClass(Object, Object).

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

Sidebar

Related Questions

how can i instantiate another class object from a class instance? in the code
I'm trying to create a class that can instantiate arrays at runtime by giving
Given an instantiated JavaScript object/class can I make an Ajax call from that class
Is it possible to assure that only spring can instantiate a class, and not
I want to write a java class that can be instantiated only 5 times
Using a java.lang.reflect.Proxy , I can create a class that takes an InvocationHandler as
Although a static class has only one instance and can't be instantiated, a class
How can I instantiate a class by throwing in a variable name? Consider this
in MXML, there is a Button class which you can instantiate like so: <mx:Button
Can VBA code instantiate and use .NET objects? The specific class I'm interested in

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.