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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:39:12+00:00 2026-05-30T03:39:12+00:00

This could well be a stupid question, but I’m new to Java, so… I’ve

  • 0

This could well be a stupid question, but I’m new to Java, so…

I’ve currently got some code where currently this is being used
clazz.asSubclass(asSubclassOfClass).getConstructor().newInstance()

I need to pass some arguments to the contructort so I want to change it to: clazz.asSubclass(asSubclassOfClass).getConstructor(params).newInstance(args)

What I don’t understand is what I need to pass in as params and what I need to pass in as args.

Let’s say I wanted to pass in a String “howdy” and some object of type XYZ called XyzObj in. How would I specify that? WHat would I pass as params and what would I pass as args?

  • 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-30T03:39:13+00:00Added an answer on May 30, 2026 at 3:39 am

    In Java this is called Reflection.

    Assuming the class has this constructor, otherwise you will get a NoSuchMethod exception I believe.

    clazz.asSubclass(asSubclassOfClass)
        .getConstructor(String.class,XYZ.class)
        .newInstance("howdy",XyzObj);
    

    Since you are new to Java, let me give you an easier so that you can understand what’s going on under the hood when you do this.

    Assume you have the following class:

    public class ParentClazz{
            String someVar;
        public ParentClazz(){
            someVar="test";
        }
        public ParentClazz(String someVar){
            System.out.println("I have been invoked");
            this.someVar=someVar;
        }
    }
    

    Then you have the following main method:

    public static void main(String[] args) throws ParseException, IllegalArgumentException, SecurityException, InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
               ParentClazz.class.asSubclass(ParentClazz.class).getConstructor(String.class).newInstance("howdy");
        }
    

    If you run this you will notice the console output print message – I have been invoked. This means that using reflection you have invoked the constructor of ParentClazz.

    You can do the same thing if the scenario allows you is by using standard object creation process:

    ParentClazz clazz = new ParentClazz("howdy");
    

    Hope this helps you understand it.

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

Sidebar

Related Questions

This could well be a real stupid question, but I can't seem to sort
This may seem like a basic/stupid/obviously-answered question, but I wanted to check: why use
It looks like it is a stupid question, but i could not figure how
Well the question might seem stupid but I really can't figure it out. How
This might be a really stupid question, but I can't find any information regarding
This might be a very stupid question, but I still dont completely understand and
This is probably a stupid question! I am being forced to use Castle Windsor
This might seem a very stupid question but things are pretty complex for me
This is a bit of a weird one, and I could well be coding
This could be a little off the ballpark, but a friend asked me about

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.