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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:21:11+00:00 2026-05-29T07:21:11+00:00

I have a database table that contains a column named type. For every row

  • 0

I have a database table that contains a column named type. For every row in my database column I have to create an object depending on the type. At the moment I use if else statements for that:

if (type.equals("object1")){
    Object1 object1 = new Object1();
}
else if (type.equals("object2")){
    Object2 object2 = new Object2();
}

Somewhat nicer would be to use an enum, as the number of types is limited but is there a possibility to let the creation of an object depend on the value of the String?

I’m open to suggestions that might solve my problem in another way than I am trying to.

  • 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-29T07:21:12+00:00Added an answer on May 29, 2026 at 7:21 am

    You can use

    Object o = Class.forName(type).newInstance();
    

    If you have an int and two Strings as arguments you need.

    Object o = Class.forName(type)
                    .getConstructor(int.class, String.class, String.class)
                    .newInstance(intValue, string1, string2);
    

    Another possibility is to use factory methods

    Object o = getClass().getMethod("create_" + type).invoke(null);
    
    static Object1 create_object1() {
         return new Object1(/* with args */);
    }
    
    static Object2 create_object2() {
         return new Object2(/* with other args */);
    }
    

    but the most flexible approach may be to use a switch

    Object o;
    switch(type) { // in Java 7
        case "object1": o = new Object1(); break;
        case "object2": o = new Object2(); break;
    

    What would be more elegant is using closures in Java 8.

    http://cr.openjdk.java.net/~briangoetz/lambda/lambda-state-final.html

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

Sidebar

Related Questions

I have a database table TravelRequest that contains, amongst other things, the fields SignOffName
I have a database that contains a table that looks a bit like this:
I have two simple tables in my database. A card table that contains Id,
I'm trying to normalize a mysql database.... I currently have a table that contains
I have a table in a sql server 2008 database that contains bunch of
I have a database structure that has a Person table which contains fields such
Should a database table that contains two columns that are foreign keys have a
I have a database with a table that contains Name and CompanyName . They
I have a database Table Named P_Columns. This Table Contains the columns ID Column_Name
I have a database table which contains an ID column and a Name column.

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.