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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T19:46:07+00:00 2026-06-10T19:46:07+00:00

Given an existing code base being used in production containing a class A that

  • 0

Given an existing code base being used in production containing a class A that contains a method that populates N fields of another POJO class B using setters and returns the POJO to the caller and given that all instances of class B will be different in regards to only two field i.e, N – 2 fields will be the same across all instances of class B, would there be any performance gain if class A has a static reference to class B and initializes the fields of class B that don’t change in a static init block? This way, the method in class A that was populating N fields of class B on each call will now have to populate only two fields which are different. This will also eliminate the need to create a new object of type B for each call to the method in class A. What are the implications of this approach in a multi-threaded application?

Alternately, we can declare all the fields in class B that don’t change as static so that the method in class A only sets the fields that change per call. Either ways, will the performance gain be worth it or this change qualifies as premature optimization?

Example :

class A { 
     private static B b = new B(); 

     static {
         b.setSystem("MySystem");
         b.setVersion("1.1");
     }

     public B getB(String name) {
         b.setName(name);
         return B;

     }
}
  • 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-10T19:46:08+00:00Added an answer on June 10, 2026 at 7:46 pm

    given that all instances of class B will be different in regards to only two field

    So I understand that you have more than one instance of B

    This will also eliminate the need to create a new object of type B for each call to the method in class A.

    But now you only have one instance of B – if A changes those 2 fields, all the places in your code that have a reference to that instance of B will see the change (assuming proper synchronization).

    => It is not clear whether you need one or more instances of B.

    What are the implications of this approach in a multi-threaded application?

    If you share the same instance of B accross threads, you need to make sure you use proper synchronization when writing and reading B’s properties.

    Alternately, we can declare all the fields in class B that don’t change as static

    Do you mean final?

    Either ways, will the performance gain be worth it or this change qualifies as premature optimization?

    It think it does qualify indeed. Especially since you don’t seem 100% sure yet about what you need. So same advice as usual: go the easy and simple way. IF performance is not good enough, profile and determine what parts of the code need to be improved.

    If you know you are going to be in a multi-threaded environment, you should probably avoid anything static and mutable – that will make your life easier and limit the risk of concurrency bugs.

    Ideally, (1) try not to share objects accross threads – if you can’t, then (2) try to share immutable objects – if you can’t (3) make sure that you use proper synchronization and that you know what you are doing.

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

Sidebar

Related Questions

I have an existing class that declares a virtual method and defines a default
While building an existing code base on Mac OS using its native build setup
I'm adding new features to an existing code base. Anyway, the current feature I'm
Given the following class structure: class Base { virtual void outputMessage() { cout <<
I have some existing code that retrieves data from a database using ADO.NET that
It seems that the Code First DbContext really uses the given ConnectionString during compile?
I have code external to OpenERP that functions as an existing product pricing system.
In short, the following code calls an existing selector in the super class, and
I have an existing Rails 2.3.x application that uses the follow block of code
I want to insert given text to existing pdf at X,Y location provided. I

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.