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

  • Home
  • SEARCH
  • 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 7690289
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:21:41+00:00 2026-05-31T20:21:41+00:00

I was looking as the question : Instantiate a class from its string name

  • 0

I was looking as the question : Instantiate a class from its string name which describes how to instantiate a class when having its name. Is there a way to do it in Java? I will have the package name and class name and I need to be able to create an object having that particular name.

  • 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-31T20:21:43+00:00Added an answer on May 31, 2026 at 8:21 pm

    Two ways:

    Method 1 – only for classes having a no-arg constructor

    If your class has a no-arg constructor, you can get a Class object using Class.forName() and use the newInstance() method to create an instance (though beware that this method is often considered evil because it can defeat Java’s checked exceptions).

    For example:

    Class<?> clazz = Class.forName("java.util.Date");
    Object date = clazz.newInstance();
    

    Method 2

    An alternative safer approach which also works if the class doesn’t have any no-arg constructors is to query your class object to get its Constructor object and call a newInstance() method on this object:

    Class<?> clazz = Class.forName("com.foo.MyClass");
    Constructor<?> constructor = clazz.getConstructor(String.class, Integer.class);
    Object instance = constructor.newInstance("stringparam", 42);
    

    Both methods are known as reflection. You will typically have to catch the various exceptions which can occur, including things like:

    • the JVM can’t find or can’t load your class
    • the class you’re trying to instantiate doesn’t have the right sort of constructors
    • the constructor itself threw an exception
    • the constructor you’re trying to invoke isn’t public
    • a security manager has been installed and is preventing reflection from occurring
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was looking at this question. Basically having a leading zero causes the number
I was looking at this question: How to remove duplicate elements from an xml
I just finished looking at this question: https://stackoverflow.com/questions/753122/which-cloud-computing-platform-should-i-choose But, I am not certain what
New Question I am looking for a way in Javascript to get the parent
My question is slightly different from Looking for a recommendation for a lightweight mobile
In Objective-C is there a way to ask a Class if there are any
I am looking for a way to create a basic python object which I
So I am looking on question about struct vs class difference... And I do
Looking at another question of mine I realized that technically there is nothing preventing
Looking at this question: Reading Remote MP3 File's ID3 Tags using Java It looks

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.