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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T19:50:05+00:00 2026-05-14T19:50:05+00:00

There’s a piece of code that looks like this. The problem is that during

  • 0

There’s a piece of code that looks like this. The problem is that during bootup, 2 initialization takes place. (1) Some method does a reflection on ForumRepository & performs a newInstance() purely to invoke #setCacheEngine. (2) Another method following that invokes #start(). I am noticing that the hashCode of the #cache member variable is different sometimes in some weird scenarios. Since only 1 piece of code invokes #setCacheEngine, how can the hashCode change during runtime (I am assuming that a different instance will have a different hashCode). Is there a bug here somewhere ?

public class ForumRepository implements Cacheable
{
    private static CacheEngine cache;
    private static ForumRepository instance;

    public void setCacheEngine(CacheEngine engine) { cache = engine; }

    public synchronized static void start()
    {
        instance = new ForumRepository();
    }

    public synchronized static void addForum( ... )
    {
        cache.add( .. );
        System.out.println( cache.hashCode() );
        // snipped
    }

    public synchronized static void getForum( ... )
    {
        ... cache.get( .. );
        System.out.println( cache.hashCode() );
        // snipped
    }
}

The whole system is wired up & initialized in the init method of a servlet.
And the init() method looks like this conceptually:

// create an instance of the DefaultCacheEngine
cache = (CacheEngine)Class.forName( "com..DefaultCacheEngine" ).newInstance();
cache.init();

// init the ForumRepository's static member
Object o = Class.forName( "com.jforum....ForumRepository" ).newInstance();     
if( o instanceof Cacheable )
    ((Cacheable)o).setCacheEngine(cache);

// Now start the ForumRepository
ForumRepository.start();

UPDATE I didn’t write this code. It is taken from jforum

UPDATE 2 Solution found. I added a separate comment below describing the cause of the problem. Thanks to everyone.

  • 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-14T19:50:05+00:00Added an answer on May 14, 2026 at 7:50 pm

    You’re going to have to give WAY more information than this, but CacheEngine is probably a mutable data type, and worse, it may even be shared by others. Depending on how CacheEngine defines its hashCode(), this could very well lead to aForumRepository seeing various different hash codes from its cache.

    It’s perfectly fine for the same object, if it’s mutable, to change its hashCode() over a period of time, as long as it’s done in a consistent manner (which is another topic altogether).

    See also

    • Object.hashCode() — make sure you understand the implications of the contract

    On cache being static

    More information has resurfaced, and we now know that the object in question, while mutable, does not @Override hashCode(). However, there seems to be a serious issue in design in making cache a static field of ForumRepository class, with a non-static “setter” setCacheEngine (which looks to be specified by Cacheable).

    This means that there is only incarnation of cache, no matter how many ForumRepository instances are created! In a way, all instances of ForumRepository are “sharing” the same cache!

    JLS 8.3.1.1 static Fields

    If a field is declared static, there exists exactly one incarnation of the field, no matter how many instances (possibly zero) of the class may eventually be created. A static field, sometimes called a class variable, is incarnated when the class is initialized.

    I think it’s important to step back right now and ask these questions:

    • Does cache need to be static? Is this intended?
      • Should instances of ForumRepository have their own cache?
      • … or should they all “share” the same cache?
    • How many instances of ForumRepository will be created?
      • Putting pros and cons of the design pattern aside, should ForumRepository be a singleton?
    • How many times can setCacheEngine be called on a ForumRepository object?
      • Would it benefit from a defensive mechanism of throwing IllegalStateException if it’s called more than once?

    The best recommendations would depend on the answers to the above questions. The third bullet point is something that is immediately actionable, and would reveal if setCacheEngine is getting invoked multiple times. Even if they’re only invoked once for each ForumRepository instance, it’s still effectively a multiple “set” in the current state of affairss, since there’s only one cache.

    A static field with a non-static setter is a design decision that needs to be thoroughly reexamined.

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

Sidebar

Related Questions

There is a column that exists in 2 tables. In table 1, this column
For some reason, after submitting a string like this Jack’s Spindle from a text
There are some Magento Connect extensions that I find myself installing every time I
There are some stdlib functions that throw errors on invalid input. For example: Prelude>
There is my code that I want to alert test once when mousemove ,
There was some progress made to the earlier problem. Now there is a new
I know there's a lot of other questions out there that deal with this
There are plenty of questions and answers to this like this question but I
There is a properly formatted text in Html textarea which looks like : As
There is a <input type=checkbox name=option2 id=checkbox_3 value=3 /> I tried this code <script

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.