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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T11:04:31+00:00 2026-06-05T11:04:31+00:00

Suppose I have a method like this public void readAndInitialize(StringReader reader, Class className) {

  • 0

Suppose I have a method like this

 public void readAndInitialize(StringReader reader, Class className)
  {
    // Here, I want to to something like this - 

   // obj = new (object of the specified className)

      obj.readAndInitialize(reader);
  }

How to do this in Java?

  • 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-05T11:04:32+00:00Added an answer on June 5, 2026 at 11:04 am

    While everyone is quick to point out Class.forName("com.myorg.MyClass"); and the related newInstance() method, it is important to remember that it will only call a default constructor taking no parameters.

    If you find that you need to call a particular constructor of the class, you need to use reflections to find the correct constructor, and then call it.

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Type;
    import static java.lang.System.out;
    
    public class ConstructorSift {
        public static void main(String... args) {
        try {
            Class<?> cArg = Class.forName(args[1]);
    
            Class<?> c = Class.forName(args[0]);
            Constructor[] allConstructors = c.getDeclaredConstructors();
            for (Constructor ctor : allConstructors) {
            Class<?>[] pType  = ctor.getParameterTypes();
            for (int i = 0; i < pType.length; i++) {
                if (pType[i].equals(cArg)) {
                out.format("%s%n", ctor.toGenericString());
    
                Type[] gpType = ctor.getGenericParameterTypes();
                for (int j = 0; j < gpType.length; j++) {
                    char ch = (pType[j].equals(cArg) ? '*' : ' ');
                    out.format("%7c%s[%d]: %s%n", ch,
                           "GenericParameterType", j, gpType[j]);
                }
                break;
                }
            }
            }
    
            // production code should handle this exception more gracefully
        } catch (ClassNotFoundException x) {
            x.printStackTrace();
        }
        }
    }
    

    lists all constructors, and a tutorial is available here.

    Once you have found the desired constructor, you can call it like so

    import java.lang.reflect.Constructor;
    import java.lang.reflect.Field;
    import java.lang.reflect.InvocationTargetException;
    import java.util.HashMap;
    import java.util.Map;
    import java.util.Set;
    import static java.lang.System.out;
    
    class EmailAliases {
        private Set<String> aliases;
        private EmailAliases(HashMap<String, String> h) {
        aliases = h.keySet();
        }
    
        public void printKeys() {
        out.format("Mail keys:%n");
        for (String k : aliases)
            out.format("  %s%n", k);
        }
    }
    
    public class RestoreAliases {
    
        private static Map<String, String> defaultAliases = new HashMap<String, String>();
        static {
        defaultAliases.put("Duke", "duke@i-love-java");
        defaultAliases.put("Fang", "fang@evil-jealous-twin");
        }
    
        public static void main(String... args) {
        try {
            Constructor ctor = EmailAliases.class.getDeclaredConstructor(HashMap.class);
            ctor.setAccessible(true);
            EmailAliases email = (EmailAliases)ctor.newInstance(defaultAliases);
            email.printKeys();
    
            // production code should handle these exceptions more gracefully
        } catch (InstantiationException x) {
            x.printStackTrace();
        } catch (IllegalAccessException x) {
            x.printStackTrace();
        } catch (InvocationTargetException x) {
            x.printStackTrace();
        } catch (NoSuchMethodException x) {
            x.printStackTrace();
        }
        }
    }
    

    which is again, from the tutorial about reflection.

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

Sidebar

Related Questions

Suppose I have something like this: public abstract class AbstractDataObject { public abstract void
Suppose I have a method like this: public void MyCoolMethod(ref bool scannerEnabled) { try
Suppose I have a class like this: public class ThingManager { List<SomeClass> ItemList; public
Suppose I have a MEF composition like this: public class Composition { [ImportMany(AllowRecomposition =
Suppose you have a method: public void Save(Entity data) { this.repositoryIocInstance.EntitySave(data); } Would you
Lets suppose I have a method like that in MVC public void UpdateUser(User user)
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Suppose I have this: @Transactional(rollbackFor = NotificationException.class) public interface PersonManagerService { public void addPerson(Person
Suppose I have a simple method like this for processing two lists: public static
Suppose I have a method like so: public byte[] GetThoseBytes() { using (System.IO.MemoryStream ms

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.