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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T08:08:45+00:00 2026-05-28T08:08:45+00:00

I have some classes and for some reason I need a procedure that create

  • 0

I have some classes and for some reason I need a procedure that create some variables of that class dinamically…

Let’s assume I have this code:

for (int i = 0 ; i < 5 ; i++)
{
  ....
  POP tmp = new POP();
  tmp = (POP) ast.convert(aso, POP.class);     
}

I want that the POP class is set dinamically… I almost achieved what I want except for the casting from object to class, I do not know how to write it…

String className = "POP";
Class cl = Class.forName(className);
Class cl2 = POP.class;
cl = (??????) ast.convert(aso, cl2);

How can I solve it?

  • 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-28T08:08:46+00:00Added an answer on May 28, 2026 at 8:08 am

    In your second code snippet, cl will actually be Class<POP> but cl2 is Class<String>, which I guess is not what you expect. Assuming you are coding in Java 5 or newer, you should not use the raw types (Class instead of Class<?>), this will make your code safer and easier to read.

    Also note that you have to use the fully qualified class name, and not just the simple name of your classes. For example

    Class.forName("String"); // Won't work
    Class.forName("java.lang.String"); // Is what you need.
    

    When you have a Class instance you can use reflection to create new instances dynamically:

        Class<?> cl2 = ... ;        
    
        // If the class has a no-arg visible constructor
        Object foo = cl2.newInstance();
    
        // Or using an explicit constructor (here with an integer and a String as arguments)
        Constructor<Class<?>> cons = cl2.getConstructor(Integer.class, String.class);
        Object bar = cons.newInstance(1, "baz");
    

    But maybe what you are trying to achieve could be done using the Abstract Factory pattern ? You provide an object that is able to create instances of the type that you want, and you can choose the factory to use based on the class name.

    http://c2.com/cgi/wiki/wiki?AbstractFactoryPattern

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

Sidebar

Related Questions

I have some classes that, for one reason or another, cannot be or need
Let's say I have some classes like this: abstract class View(val writer: XMLStreamWriter) {
I have some classes layed out like this class A { public virtual void
I have some classes that will do something based on some conditions . The
I have some classes that represent immutable objects (Quantity, Price, Probability). Is there some
I have some classes in my app that don't require an ID to be
In one of my projects, I have some classes that represent entities that cannot
I have some object classes that use inheritance. It seems that I can only
I have a business model with many classes in, some logical entities within this
I have some classes that all implement a certain interface. I want to store

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.