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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:34:06+00:00 2026-06-10T08:34:06+00:00

In the following code, I am getting java.lang.InstantiationException (Below is trimmed down code that

  • 0

In the following code, I am getting java.lang.InstantiationException

(Below is trimmed down code that compiles standalone – in my application I want to maintain an Enum->Class map, and on reading integer values from a file, instantiate appropriate class looking into the map).

How to get rid of the error? Is there a syntax problem? Must I use Interfaces? My understanding here is limited.

class Main {
    abstract class Base {
        Base() {};
        void print() {
            System.out.println("I am in Base");
        }
    }


    class D1 extends Base {
        D1() {};
        @Override 
        void print() {
            System.out.println("I am in D1");
        }
    }

    static Class<? extends Base> getMyClass() {
        return D1.class;
    }


    public static void main(String[] args) {
        try {
            Class<?> cc = getMyClass();
            Object oo = cc.newInstance();
            Base bb = (Base) oo; 
            bb.print();

        } catch (Exception ee) {
            System.out.println(ee);
        };
    }
};
  • 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-10T08:34:07+00:00Added an answer on June 10, 2026 at 8:34 am

    Your code has two problems:

    • Base and D1 are non-static inner classes. It means that they can access fields and methods of their declaring class (Main), therefore they should hold a reference to the instance of Main. Therefore their constructors have an implicit argument of type Main which is used to pass that reference. So, they don’t have no-args constructors and you should use a single-argument constructor instead:

      Object oo = cc.getConstructor(Main.class).newInstance(new Main()); 
      

      Alternatively, you can simply declare them as static, or declare them outside of Main – in this case they won’t be able to access member of Main and won’t require a reference to it.

    • Constructor of D1 should be public. Otherwise you need to call setAccessible(true) to make it accessible for reflection.

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

Sidebar

Related Questions

I am getting java.lang.VerifyError.I am posting my log-file and code below. I have looked
While running the following code i am getting the error java.lang.OutOfMemoryException : java heap
Can anyone tell me why am i getting error message java.lang.NullPointerException in following code??
I am getting the following error: java.lang.StringIndexOutOfBoundsException: length=13243; regionStart=32; regionLength=-39 at java.lang.String.startEndAndLength(String.java:593) at java.lang.String.substring(String.java:1474)
I am getting an javax.el.ELException: java.lang.Short cannot be cast to java.lang.Long for the following
I'm getting the following exception. Caused by: java.lang.ClassCastException: java.math.BigInteger cannot be cast to java.lang.Integer
I'm trying to run the following code: import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; public class Reflection
In the following code, I am getting a compilation error stating that I have
I am having following code for getting image from the web: NSURL *ImageURL =
I am compiling following code and getting following error. How to fix this? Thanks

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.