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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:36:31+00:00 2026-05-16T10:36:31+00:00

I would like to instantiate an object from its Class object, using the constructor

  • 0

I would like to instantiate an object from its Class object, using the constructor that accepts a single String argument.

Here is some code that approaches what I want:

Object object = null;
Class classDefinition = Class.forName("javax.swing.JLabel");
object = classDefinition.newInstance();

However, it instantiates the JLabel object with no text. I would like to use the JLabel constructor that accepts a string as the initial text. Is there a way to select a specific constructor from a Class object?

  • 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-16T10:36:32+00:00Added an answer on May 16, 2026 at 10:36 am

    Class.newInstance invokes the no-arg constructor (the one that doesn’t take any parameters). In order to invoke a different constructor, you need to use the reflection package (java.lang.reflect).

    Get a Constructor instance like this:

    Class<?> cl = Class.forName("javax.swing.JLabel");
    Constructor<?> cons = cl.getConstructor(String.class);
    

    The call to getConstructor specifies that you want the constructor that takes a single String parameter. Now to create an instance:

    Object o = cons.newInstance("JLabel");
    

    And you’re done.

    P.S. Only use reflection as a last resort!

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

Sidebar

Ask A Question

Stats

  • Questions 513k
  • Answers 513k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In the past, I have used Java2HTML to convert source… May 16, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer Install the TestNG plug-in and when you're done, you can… May 16, 2026 at 5:50 pm
  • Editorial Team
    Editorial Team added an answer The main problem is that the thread may be terminated… May 16, 2026 at 5:50 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a high CPU/memory bound task that I would like my Scala program
I have a factory class that decides which of four available subclasses it should
Coming to Python from Java, I've been told that factories are not Pythonic. Thus,
We have a .Net object that has a static delegate (a data access strategy)
I am working on a project where I have a class called HGraphic that
My question is pertaining to the best practice for accessing a child object's parent.
I'm relatively new using OOP in PHP. It's helped immensely in the organization and
I am struggling with Javascript. Most of my problems do not arise from lack
I'm building a webapp using php, ajax, javascript, mysql. I've been worring about something
Following on from my recent question on Large, Complex Objects as a Web Service

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.