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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T05:45:21+00:00 2026-06-11T05:45:21+00:00

This is more of a theory question than a solution question, so hear me

  • 0

This is more of a theory question than a solution question, so hear me out.

In C/C++ as well as PHP, you can declare constants. There are usually a couple of ways to do this (#DEFINE for example, or ‘const type‘…) and the ultimate effect of this is that during compilation a replace is done so that all of those named constants become literals. This helps because instead of having to access a memory location to find the data, the data is hardcoded in, but without the downside of hardcoding – recalling the value if it needs to be reused, and changing all of the instances of that value when it needs to be changed.

But Java’s final declaration is slightly inscrutable; because I can create a class with unset final vars and initialize them on construction, it means that they are not precompiled as literals as far as I can tell. Other than guaranteeing that they cannot logically change afte construction, does the final declaration provide any benefit to efficiency?

References to articles are fine, as long as you make note of the part which explains what final really does and what are if any its benefits other than stopping value changes after construction.

As a corollary, is it possible to actually declare compilation-level constants in Java in any other way than simply using literals (a bad idea anyway?)

  • 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-11T05:45:22+00:00Added an answer on June 11, 2026 at 5:45 am

    Java does have constant expressions. See here (link outdated, archived at archive.org) in the java language specification.

    A compile-time constant expression is an expression denoting a value of primitive type or a String that does not complete abruptly and is composed using only the following:

    • Literals of primitive type and literals of type String (§3.10.5)
    • Casts to primitive types and casts to type String
    • The unary operators +, -, ~, and ! (but not ++ or --)
    • The multiplicative operators *, /, and %
    • The additive operators + and -
    • The shift operators <<, >>, and >>>
    • The relational operators <, <=, >, and >= (but not instanceof)
    • The equality operators == and !=
    • The bitwise and logical operators &, ^, and |
    • The conditional-and operator && and the conditional-or operator ||
    • The ternary conditional operator ? :
    • Parenthesized expressions whose contained expression is a constant expression.
    • Simple names that refer to constant variables (§4.12.4).
    • Qualified names of the form TypeName . Identifier that refer to constant variables (§4.12.4).

    But in Java, unlike C/C++, you also have a JIT compiler, so additional inlining is possible. So the bottom line is, don’t worry about until you see a real performance problem.

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

Sidebar

Related Questions

This is more of a theory question than a programming question. As you know
This is a theory question more than an implementation question. What, in your opinion,
I am sorry if this is more of a theory question then a real
This is more of a cryptography theory question, but is it possible that the
This is more of a logic question than a coding question. I am building
This is more of a theory question. If I'm running 50,000 queries that insert
This question is not about any error I'm currently seeing, it's more about theory
My question is based more on theory than syntax, but any syntax suggestions are
This is more of a computer science question than a programming one, but I
I am wondering if there is a way to handle this more elegantly. After

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.