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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T02:51:32+00:00 2026-06-04T02:51:32+00:00

I have two options in a Servlet regarding the initialization of a variable that

  • 0

I have two options in a Servlet regarding the initialization of a variable that I don’t want to change over the lifetime of the @Singleton Servlet, we’ll call it:

Field a;

Field a will be used internally by the servlet only, there are no accessor methods, and for simplicity’s sake, Field a is something immutable, like a String.

Option 1 – Initialize the field inside a constructor utilizing information gained from dependency injection of the ServletContext, so that the field becomes:

private final Field a;

Option 2 – Set the variable in an overridden init(ServletConfig cfg) method utilizing information gained from the ServletConfig (and ServletContext) so that the field becomes:

private Field a;

Obviously, I would prefer to have the field final, but the field is private and has no accessor methods attached to it, so should I still try to make it final?

So, in summary, should a field be initialized in the constructor and made final, or initialized in the init method and left as non-final with no accessors?

Thank you.

  • 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-04T02:51:33+00:00Added an answer on June 4, 2026 at 2:51 am

    In addition to documenting your intent clearly and making your code easier to understand and maintain, a final modifier provides some important assurances in a multi-threaded application.

    While I’m not sure if it’s guaranteed by the Servlet specification, it seems that in practice, servlet engines should use some memory barrier that will ensure that the internal state of a new servlet instance is visible to all of the threads that will use it to process servlet requests. This is called “safe publication.”

    However, in general, this is not the case. What you describe as “Option 2” is “effective immutability.” The object has a field that isn’t final, but doesn’t allow modification of that field after construction or some initialization step. But, if other threads access this object without going through a memory barrier, they might not see the value assigned to that field by the initializing thread—even if this field was set in the object’s constructor!

    A value assigned during object construction to a final field, however, is guaranteed to be visible to all other threads, even if the object itself is not “safely published.”

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

Sidebar

Related Questions

In my models, I have two options...when I get to a variable that necessitates
I want to parse a large XML file and I have two options: Perl
I have two * Servlet * and i want to run both Servlet on
I would like to have two options menus in Android that are triggered using
I noticed that when naming elements in xaml that we have two options when
I want to make a stand-alone exe with cygwin. I have two options: Staticly
After an Ajax call I reload a web page. I have two options.. I
Hy, I have two options to choose from: Client side(with FLASH or Javascript): pro:
I have a ADO.NET/TSQL performance question. We have two options in our application: 1)
I am implementing slideshow. I have two options for paging between images: 1) to

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.