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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:39:39+00:00 2026-05-13T06:39:39+00:00

I have many html forms referring to many persistence classes. All the html forms

  • 0

I have many html forms referring to many persistence classes.

All the html forms are generated by one single class HTMLForm by passing in the respective HTMLFields instances:

public class HTMLForm<T>{
 HTMLForm(HTMLFields[] f, Class<T> classt){
  this.stupidSunWontAllowTnewInstance = classt;

  // ... whatever GWT jazz ....
 }

 public T getPersistenceHandlerClass(){
   try{
     return (T) stupidSunWontAllowTnewInstance.newInstance();
   }
   catch (InstantiationException e){}
   catch (IllegalAccessException e){}
 }

 Class<T> stupidSunWontAllowTnewInstance;
}

HTMLForm looks at HTMLFields[] array to generate the respective html forms. Therefore, each set of HTMLFields[] array require a different persistence class. The persistence classes are like

PostalAddr, PersonInfo, ItemDescr, Preferences, etc, etc.

Now since I cannot do T.newInstance(), stupidSunWontAllowTnewInstance would be assigned (a silly necessity due to sun’s architecture of generics) by the HTMLForm constructor and then getPersistenceHandlerClass is used later to get the appropriate persistence handling class.

Since cloud computing charges by the cpu hour, my question is, which would use less cpu, presuming I have about 25 persistence classes to wade thro. The first one above or the one following?

 public T getPersistenceHandlerClass(){
  if (stupidSunWontAllowTnewInstance == PostalAddress.class)
    return new PostalAddress();
  if (stupidSunWontAllowTnewInstance == PersonInfo.class)
    return new PersonInfo();

  if (stupidSunWontAllowTnewInstance == ....
    ....
    ....

  if (stupidSunWontAllowTnewInstance == etc.class)
    return new etc();
 }

or a Map of factories

 public static Map<PersistenceHandlerFactoryInterface> PHFactories;
 public T getPersistenceHandlerClass(){
  return
   PHFactories.get(stupidSunWontAllowTnewInstance).createInstance();
 }

or make a better suggestion (with cpu consumption consideration) on how I should overcome java generics shortcoming, for another way to instantiate from parameter T. (Or critique me why I’m being too heady criticising sun’s generic parametric shortcoming).

  • 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-13T06:39:39+00:00Added an answer on May 13, 2026 at 6:39 am

    I would go with the first form – I very much doubt that any performance difference is going to be significant. If you’re really worried about it though, measure the difference.

    Another alternative is to use an enum:

    public enum PersistenceHandlerFactory
    {
        POSTAL_ADDRESS
        {
            @Override Object newField()
            {
                return new PostalAddress();
            }
        },
        ...;
    
        public abstract Object newField();
    }
    

    Then pass your enum value into the constructor instead of Class<T>. No reflection, no large if/else block, and you don’t accidentally end up trying to use it with a class you don’t support or a type without a public parameterless constructor. The downside is that you don’t have compile-time prevention of:

    new HtmlForm<PersonInfo>(PersistenceHandler.POSTAL_ADDRESS)
    

    You could make an execution-time check for that, but it would be somewhat messy.

    Overall, I’d still go for the first version unless you have a good reason not to.

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

Sidebar

Related Questions

If I have a single page with many forms of existing records: index.html.haml -
I have many pages where I get data entry forms such that one HTML
I have the following HTML with many of these kinds of links: <a class=button
Hi I have webpage data (html converted into xml) consisting of many forms ,
I have many different forms in my app, all of them are using remote:
how can i do. i have many html elements on page. after clicking on
Suppose on my HTML page I have many href s to a link. Using
I am working on an application with many html pages. I have created these
I have an html table with many rows. I'm currently grouping several rows inside
I have a 5×7 HTML table. On many queries, there are fewer than 35

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.