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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:17:55+00:00 2026-05-10T17:17:55+00:00

I’m making a mini ORM for a Java program I’m writing… there is a

  • 0

I’m making a mini ORM for a Java program I’m writing… there is a class for each table in my db, all inheriting from ModelBase.

ModelBase is abstract & provides a bunch of static methods for finding & binding objects from the db, for example:

public static ArrayList findAll(Class cast_to_class) {   //build the sql query & execute it  } 

So you can do things like ModelBase.findAll(Albums.class) to get a list of all persisted albums. My problem is that in this static context, I need to get the appropriate sql string from the concrete class Album. I can’t have a static method like

public class Album extends ModelBase {   public static String getSelectSQL() { return 'select * from albums.....';} } 

because there is no polymorphism for static methods in Java. But I don’t want to make getSelectSQL() an instance method in Album because then I need to create an instance of it just to get a string that is really static in behavior.

At the moment, findAll() uses reflection to get the appropriate sql for the class in question:

select_sql = (String)cast_to_class.getDeclaredMethod('getSelectSql', new Class[]{} ).invoke(null, null); 

But that’s pretty gross.

So any ideas? It’s a general problem I’m having time and time again – the inability to specify abstract static methods in classes or interfaces. I know why static method polymorphism doesn’t and can’t work, but that doesn’t stop me from wanting to use it time again!

Is there any pattern/construct that allows me to ensure that concrete subclasses X and Y implement a class method(or failing that, a class constant!)?

  • 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. 2026-05-10T17:17:55+00:00Added an answer on May 10, 2026 at 5:17 pm

    Albeit, I totally agree in the point of ‘Static is the wrong thing to be using here’, I kind of understand what you’re trying to address here. Still instance behavior should be the way to work, but if you insist this is what I would do:

    Starting from your comment ‘I need to create an instance of it just to get a string that is really static in behaviour’

    It is not completely correct. If you look well, you are not changing the behavior of your base class, just changing the parameter for a method. In other words you’re changing the data, not the algorithm.

    Inheritance is more useful when a new subclass wants to change the way a method works, if you just need to change the ‘data’ the class uses to work probably an approach like this would do the trick.

    class ModelBase {     // Initialize the queries     private static Map<String,String> selectMap = new HashMap<String,String>(); static {         selectMap.put( 'Album', 'select field_1, field_2 from album');         selectMap.put( 'Artist', 'select field_1, field_2 from artist');         selectMap.put( 'Track', 'select field_1, field_2 from track');     }      // Finds all the objects for the specified class...     // Note: it is better to use 'List' rather than 'ArrayList' I'll explain this later.     public static List findAll(Class classToFind ) {         String sql = getSelectSQL( classToFind );         results = execute( sql );         //etc...         return ....     }      // Return the correct select sql..     private static String getSelectSQL( Class classToFind ){         String statement = tableMap.get( classToFind.getSimpleName() );         if( statement == null ) {             throw new IllegalArgumentException('Class ' +                   classToFind.getSimpleName + ' is not mapped');         }         return statement;      } } 

    That is, map all the statements with a Map. The ‘obvious’ next step to this is to load the map from an external resource, such as a properties file, or a xml or even ( why not ) a database table, for extra flexibility.

    This way you can keep your class clients ( and your self ) happy, because you don’t needed ‘creating an instance’ to do the work.

    // Client usage:  ... List albums = ModelBase.findAll( Album.class ); 

    …

    Another approach is to create the instances from behind, and keep your client interface intact while using instance methods, the methods are marked as ‘protected’ to avoid having external invocation. In a similar fashion of the previous sample you can also do this

    // Second option, instance used under the hood. class ModelBase {     // Initialize the queries     private static Map<String,ModelBase> daoMap = new HashMap<String,ModelBase>(); static {         selectMap.put( 'Album', new AlbumModel() );         selectMap.put( 'Artist', new ArtistModel());         selectMap.put( 'Track', new TrackModel());     }      // Finds all the objects for the specified class...     // Note: it is better to use 'List' rather than 'ArrayList' I'll explain this later.     public static List findAll(Class classToFind ) {         String sql = getSelectSQL( classToFind );         results = execute( sql );         //etc...         return ....     }      // Return the correct select sql..     private static String getSelectSQL( Class classToFind ){         ModelBase dao = tableMap.get( classToFind.getSimpleName() );         if( statement == null ) {             throw new IllegalArgumentException('Class ' +                   classToFind.getSimpleName + ' is not mapped');         }         return dao.selectSql();     }     // Instance class to be overrided...      // this is 'protected' ...      protected abstract String selectSql(); } class AlbumModel  extends ModelBase {     public String selectSql(){         return 'select ... from album';     } } class ArtistModel  extends ModelBase {     public String selectSql(){         return 'select ... from artist';     } } class TrackModel  extends ModelBase {     public String selectSql(){         return 'select ... from track';     } } 

    And you don’t need to change the client code, and still have the power of polymorphism.

    // Client usage:  ... List albums = ModelBase.findAll( Album.class ); // Does not know , behind the scenes you use instances. 

    …

    I hope this helps.

    A final note on using List vs. ArrayList. It is always better to program to the interface than to the implementation, this way you make your code more flexible. You can use another List implementation that is faster, or does something else, without changing your client code.

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

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There is nothing in any W3C specification that says object… May 11, 2026 at 5:54 pm
  • Editorial Team
    Editorial Team added an answer Create a dictionary of value -> bit-string, that would give… May 11, 2026 at 5:54 pm
  • Editorial Team
    Editorial Team added an answer Since CFWriteStream is toll-free bridged to NSOutputStream you can use… May 11, 2026 at 5:54 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.