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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T10:57:47+00:00 2026-05-30T10:57:47+00:00

I am working on a java application. The is an abstract class Model and

  • 0

I am working on a java application. The is an abstract class Model and 2 derived classes Model 1 and Model 2. Model1 and Model2 behave differently, so I want to allow the user to select a model from the two.
It can be implemented like:

Model model;
If(response==1){
    model = new Model1();
}else if(response==2){
    model = new Model2();
}

Now the problem with this is, that if I add a new class Model3, I have to rewrite the code.
I could create a List, where I would add an instance from each class, then I would add a static method, that would return a new Instance of the class, but I hope it can be done with bit a more style.

Is there possible to create a List, to hold the type of each class. From this List I could create a menu to select a model, and then use the type to create the class the user needs.

So is it possible, to create a List<Class>, then use the Class object as a constructor, to create an instance of the given class?

  • 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-30T10:57:49+00:00Added an answer on May 30, 2026 at 10:57 am

    You can create a list of suppliers:

    static final List<Supplier<Model>> MODEL_MAKERS = ImmutableList.of(
        new Supplier<Model>() { public Model get() { return new Model1(); } },
        new Supplier<Model>() { public Model get() { return new Model2(); } },
        new Supplier<Model>() { public Model get() { return new Model3(); } }
    );
    

    You could muck around with reflection, but that requires stepping outside Java’s normal exception handling mechanisms, requires zero-argument public constructors and public classes which limits how code can change under maintenance in ways that aren’t checked by javac, and just isn’t necessary in this case.

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

Sidebar

Related Questions

I'm working on a Java application which should allow users to optimize their daily
I am working on creating abstract activity classes for my application that I will
I'm working on a Java application which I want to be available over the
I am working on a Java Swing application on OSX and I want it
I'm working on a Java application that uses JavaSpace. We're developing this in Eclipse.
So I am working on a java application, and the customer has requested the
I'm working on a java web-application, trying to be xml-friendly and writing my jsp
I am working on a Java/Struts application that uses Tomcat 6.0.10. It's a typical
I'm working on a Java web application (Adobe Flex front-end, JPA/Hibernate/BlazeDS/Spring MVC backend) and
I am working on the I18n of our java application and we have decided

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.