JL Gradley
JL Gradley

Reputation: 2031

In Java, any reason to use static final variables for strings used only once?

From just an efficiency standpoint, in Java, are there any advantages to using static final variables if the strings will only be used once in the code?

Upvotes: 1

Views: 457

Answers (1)

Karol Dowbecki
Karol Dowbecki

Reputation: 44970

Most likely no. The Java compiler interns the string constants during compilation so creating the constant manually is not a performance optimization. Moreover G1GC has string deduplication feature as per JEP 192: String Deduplication in G1.

The static final field is usually created to improve the code readability, to give meaning to a specific string value and avoid commenting.

Upvotes: 3

Related Questions