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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:41:16+00:00 2026-06-01T07:41:16+00:00

I have a class in which i have intialized hashmap in static block. Passing

  • 0

I have a class in which i have intialized hashmap in static block. Passing the key, I have retrived the value which is a class. In order to create object for this class. I have used the constructor class to get the constructor and passed arguments and created object.

I have two class in hashmap. To create objectfor EchoExpression I need to pass two arguments and for OutExpression class i need to pass only one argument(String).

  1. Based on the class returned by the key I need to execute which constructor to get and implement, whether the constructor with one argument or two argument.

  2. In EchoExpression, the constructor contains two arguments.

eg:

JXPathExpression check = new JXPathExpression(String expression, Class<?> type)

String belongs to String.class but what class does the Class type argument belongs too? so that i can use it in getting the constructor

public class ExampleFactory {

    private static HashMap<String,Class<?>> hmap = new HashMap<String,Class<?>>();

    static
    {                   
        hmap.put("echo", EchoExpression.class);         
        hmap.put("Out", OutExpression.class);                       
    }

    public void getExpo(String key,String expression)
      {
        Class aClass =map.get(key);

//Constructor implementation for OutExpression where only one argument string is passed

        Constructor constructor = aClass.getConstructor(new Class[]{String.class});

        Object object= constructor.newInstance(expression);

//Need constructor for passing two arguments string and class<?> for EchoExpression

        return null;        
      }                
}
  • 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-01T07:41:18+00:00Added an answer on June 1, 2026 at 7:41 am

    For such a thing you should in all cases try to have unified constructor parameters or a way to store the parameters per class.

    Now for your questions. Class<?> is a reference to a unknown class. Basically to any class. When using it its more or less equal to Class<Object> because all classes got Object as parent.

    For using constructors with multiple arguments you first need to fetch the fitting constructor. At this point its already possible to fetch errors that happen in case the class does not support instances with this configuration of arguments.
    Fetching the constructor works this way:

    aClass.getConstructor(new Class[]{String.class, Object.class, ...});

    This works with as many argument types as you like. Creating the new instance of the class then works this way:

    constructor.newInstance(theString, theObject, ...);

    The function newInstanace is implemented as many arguments as needed. So depending on how many arguments the constructor that was requested requires the newInstance function will be able to work with it.

    For all what you intend… maybe a proposal: Maybe its a option not to load the new objects using reflection but rather by storing instances of those objects and returning copies of the objects created using copy constructors or the clone method. In many cases this is less difficult to get running.

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

Sidebar

Related Questions

Hi I have a class which includes an array, I'm not passing this through
I have a class, which has a boost::asio::io_service object. I want this object stored
I have class A which extends the Activity class. This class is in package
I have class World which manages creation of object... After creation it calls afterCreation
I have class Employee which is something like this. class Emp { int EmpID;
I have a class which is marked with a custom attribute, like this: public
I have a class which has a static const array, it has to be
I have a User class which has a List field namely pt. This field
I have an Object Factory Class which has a method called registerCreator as defined
I have a singleton class which needs to be intialized by castle? I'm a

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.