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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:59:13+00:00 2026-06-09T15:59:13+00:00

Consider an interface that has only one method: Object getValue(String data); Suppose this describes

  • 0

Consider an interface that has only one method:

Object getValue(String data);

Suppose this describes data transformation from a String to any other data type (assuming the transformation is valid). For data coming from a textual context, there may be several columns that are of the same data type and thus there would be multiple instances of the same data transformer.

A long while ago I had written code to cache these instances instead of having multiple instances of the same object:

private HashMap<Class<? extends T>, T> map;

public synchronized T getInstance(Class<? extends T> type) throws IllegalArgumentException
{
  T instance = map.get(type);
  if (instance == null)
  {
    try
    {
      instance = type.newInstance();
    }
    catch (Exception ex)
    {
      throw new IllegalArgumentException("The provided class cannot be instantiated: "
              + type.getName() + "\n" + ex);
    }
    map.put(type, instance);
  }
  return instance;
}

Since these classes have no state, it seemed a waste, at the time to have many instances of the same class when a single shared instance would do.

However, it is likely more overhead to maintain the mapping than to have multiple instances of the same object (even though a single instance could be shared). I mean, even though there are multiple instances, the method code is only placed in memory once right? So in my mind, the JVM is effectively keeping a cache for me.

Any thoughts on the matter? Could the singleton instance-cache approach be better?

Thank you for reading my post.

  • 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-06-09T15:59:14+00:00Added an answer on June 9, 2026 at 3:59 pm

    Each time you use new, you create a new object instance that is allocated in the heap. Of course, the executable code belongs to the class, which is only shared instance.

    Anyway, from your text, your object have no state and you are just transforming the String parameter using no additional data. Given that, I would go for an static method.

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

Sidebar

Related Questions

I am trying to interface multiple accounts into a single one. I mean, consider
I am designing a Data Information Interface Layer. I want the users of this
Consider the code below (which has been simplified). I have a service class that
Would you consider using an interface and polymorphism to extend this design to be
Consider the following simple example class, which has a property that exposes a modified
I'm not sure what the technical term for this is, but consider an interface:
Consider an interface like IMyInterface = interface procedure DoSomethingRelevant; procedure Load (Stream : TStream);
Consider the following code: public interface A { public A another(); } public interface
Consider I have the following interface: public interface A { public void b(); }
Consider the following scenario: /** * A sample interface. */ public interface MyInterface {

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.