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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:57:24+00:00 2026-05-12T11:57:24+00:00

Consider this case: public Class1 { public static final String ONE = ABC; public

  • 0

Consider this case:

public Class1 {
   public static final String ONE = "ABC";
   public static final String TWO = "DEF";
}

public Class2 {

  public void someMethod() {
    System.out.println(Class1.ONE + Class1.TWO);
  }
}

Typically you would expect the compiler to inline the ONE and TWO constants. However, is this behavior guaranteed? Can you deploy at runtime Class2 without Class1 in the classpath, and expect it to work regardless of compilers, or is this an optional compiler optimization?

EDIT: Why on earth do this? Well I have a constant that would be shared between two ends of an application (client and server over RMI) and it would be very convenient in this particular case to put the constant on a class that can only be on one side of that divide (as it is logically the one that owns that constant value) rather than have it in an arbitrary constants class just because it needs to be shared by both sides of the code. At compile time its all one set of source files, but at build time it is divided by package.

  • 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-12T11:57:24+00:00Added an answer on May 12, 2026 at 11:57 am

    It’s guaranteed to be treated as a constant expression, and guaranteed to be interned by section 15.28 of the JLS:

    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 –
    • …

    …

    Compile-time constants of type String
    are always “interned” so as to share
    unique instances, using the method
    String.intern.

    Now, that doesn’t quite say it’s guaranteed to be inlined. However, section 13.1 of the spec says:

    References to fields that are constant
    variables (§4.12.4) are resolved at
    compile time to the constant value
    that is denoted. No reference to such
    a constant field should be present in
    the code in a binary file (except in
    the class or interface containing the
    constant field, which will have code
    to initialize it), and such constant
    fields must always appear to have been
    initialized; the default initial value
    for the type of such a field must
    never be observed.

    In other words, even if the expression itself weren’t a constant, there should be no reference to Class1. So yes, you’re okay. That doesn’t necessarily guarantee that the concatenated value is used in the bytecode, but the bits referenced earlier guarantee that the concatenated value is interned, so I’d be hugely surprised if it didn’t just inline the concatenated value. Even if it doesn’t, you’re guaranteed that it’ll work without Class1.

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

Sidebar

Ask A Question

Stats

  • Questions 219k
  • Answers 219k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer $('.comment_date').each(function() { $(this).html( $(this).attr('title') ); }); I think this should… May 12, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer How about the simple move command? :g/^C/m0 :g/^B/m0 :g/^A/m0 May 12, 2026 at 11:45 pm
  • Editorial Team
    Editorial Team added an answer You can use Type.MakeGenericType and Activator.CreateInstance to make an instance… May 12, 2026 at 11:45 pm

Related Questions

I have a class which looks something like this: public class Test { private
Hmmm. Consider this program, whose goal is to figure out the best way to
Consider this code: class test { public static void main(String[] args) { test inst_test
Are there any cases where we do down casting of objects? If we do,

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.