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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T13:04:00+00:00 2026-06-08T13:04:00+00:00

I have seen much code where people write public static final String mystring =

  • 0

I have seen much code where people write public static final String mystring = ...
and then just use a value.

Why do they have to do that? Why do they have to initialize the value as final prior to using it?

UPDATE

Ok, thanks all for all your answers, I understand the meaning of those key (public static final). What I dont understand is why people use that even if the constant will be used only in one place and only in the same class. why declaring it? why dont we just use the variable?

  • 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-08T13:04:02+00:00Added an answer on June 8, 2026 at 1:04 pm

    final indicates that the value of the variable won’t change – in other words, a constant whose value can’t be modified after it is declared.

    Use public final static String when you want to create a String that:

    1. belongs to the class (static: no instance necessary to use it).
    2. won’t change (final), for instance when you want to define a String constant that will be available to all instances of the class, and to other objects using the class.
    3. will be a publicly accessible part of the interface that the class shows the world.

    Example:

    public final static String MY_CONSTANT = "SomeValue";
    
    // ... in some other code, possibly in another object, use the constant:
    if (input.equals(MyClass.MY_CONSTANT))
    

    Similarly:

    public static final int ERROR_CODE = 127;
    

    It isn’t required to use final, but it keeps a constant from being changed inadvertently during program execution, and serves as an indicator that the variable is a constant.

    Even if the constant will only be used – read – in the current class and/or in only one place, it’s good practice to declare all constants as final: it’s clearer, and during the lifetime of the code the constant may end up being used in more than one place.

    Furthermore using final may allow the implementation to perform some optimization, e.g. by inlining an actual value where the constant is used.

    Finally note that final will only make truly constant values out of primitive types, String which is immutable, or other immutable types. Applying final to an object (for instance a HashMap) will make the reference immutable, but not the state of the object: for instance data members of the object can be changed, array elements can be changed, and collections can be manipulated and changed.

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

Sidebar

Related Questions

I have seen too much C# and C++ code where the variable naming convention
I have seen some people in SO commenting that Singleton Pattern is an anti-pattern.
Sometimes I have seen some code or part of the project which I could
I'm installing it now. I've seen a few videos where people have Resharper installed
Consider the following code: public ActionResult Index(String URLQuery = http://www.google.com) { HttpWebRequest webRequest; HttpWebResponse
Note: I have seen this and tried to take as much from it as
I have seen this in a lot of code written using Ruby-on-Rails. It seems
It's a pleasure to see how much knowledge people have on here, it's a
Have seen some similar questions: What is the difference between a JavaBean and a
I have seen behaviour in Perforce that I am unable to explain. I took

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.