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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:14:39+00:00 2026-06-10T11:14:39+00:00

Here’s a real life example I had experienced before, and this is something that

  • 0

Here’s a real life example I had experienced before, and this is something that makes me ponder and question this mystery.

Before: I have no idea how to instantiate a class dynamically just by using the name of the class.

After: I looked around in someone else’s Java source code, being determined to look for something unrelated to the above, like how to do collision detection, and just accidentally came across a piece of code that answers correctly my question of how to instantiate a class dynamically.

The piece of code in question is this:

A a = (A) Class.forName("A").newInstance();

Where A is the name of a Java source file, A.java.

From here, I was not only intrigued about it, but also starting to wonder, how a Java programmer learn all of this if they were given only the Java language documentation.

How do they know where to look for in the documentation, if they are practicing Java language?
I know that novice Java programmers get some experiences from expert Java programmers, but where do the expert Java programmers learn all of this, if they at first don’t know where to look for in the documentation?

Or do they just read from page 1 to the end of the documentation, and follow along closely as much as possible, and start from there? That would take a long time to finish, and it would be an impressive feat to do.

  • 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-10T11:14:41+00:00Added an answer on June 10, 2026 at 11:14 am

    Usually you search google for the Java docs… that send you to the Oracle website that contains it. http://docs.oracle.com/javase/7/docs/api/

    On some cases only the API is necessary, but when you need a full formed example you could search for code samples or snippets, or the problem you want to solve itself (i.e. “java instantiate class dinamically”).

    On the issue of the dynamic instantiation there are a lot of ClassLoader things that can be done but the most basic exemple is similar to the one you found… but there is a catch.

    When you reference the A class on your code the jvm automatically loads it for you.
    When you do

    A a = (A)Class.forName("A").newInstance();
    

    You will reference the A class you already have a grip of.

    What you could do is create an interface that the classes you want to instantiate have to implement, like this:

    String classPath = ...
    AInterface a = (AInterface)Class.forName(classPath).newInstance();
    

    And you use it from there.

    But you have to remember to add the try-catch block, because the newInstance method will only know if the referenced class has a default constructor when it tries ti instantiate it at runtime, and if it doesn’t have there will be an exception.

    The same goes for a exception thrown by the contructor itself, it will be encapsulated and thrown back at you.

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

Sidebar

Related Questions

Here is the code in a function I'm trying to revise. This example works
Here is an example. foreach (var doc in documents) { var processor = this.factory.Create();
Here's what I'm trying to accomplish with this program: a recursive method that checks
Here's a coding problem for those that like this kind of thing. Let's see
Here is an example: I write html code inside of textarea, then I swap
Here is the css: #content ul { font-size: 12px; } I am trying this:
Here's an example query: DECLARE @table table (loc varchar(10)) INSERT INTO @table VALUES ('134a'),
Here is an example: I have the generic type called Account. I wish to
Here is the problem that I am trying to solve. I have two folders
Here's a query that works fine: SELECT rowid as msg_rowid, a, b, c FROM

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.