frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•1y ago
I'm relatively new to Java. I often notice a pattern where there is a list of constant which value are equal to the name.

  class Constant {
    public static final String ALBUM = "album";
    public static final String NAME = "_name";
    public static final String DISPLAY_NAME = "display-name";
    public static final String SERVICE_NAME_METRIC_NAME_PREFIX = "service_name.metric_name";
  }

Here is a public example of this practice I could find: https://developer.android.com/reference/android/provider/MediaStore.MediaColumns

I could understand that this might help in 2 ways refactoring and typo. This reduces chance of typo because you'll get compile error instead of run-time error if you typo a constant. This might also help in refactoring if you ever wants to change the value. but if may use this android public API example, I don't think it's wise to change a field name ever. If it's decommissioned, it's good to keep it so we don't re-use the field. If it's a new better field available, I think it should have a different name. I maybe making a straw man argument here. Let me know. If it's an internal API where such refactoring might make sense -- I still kind of think internal API should also be backward compatible, replacing a string are not a complicated operation in my opinion.

I see that this practice has a cost. One being that in every class that use this API. You need to add an import. It's also often the const is only used once from my experience.

  import static com.example.MediaFields.NAME;
  import static com.example.MediaFields.DISPLAY_NAME;

  String value = json.getString(NAME);
  String value2 = json.getString(DISPLAY_NAME);
vs

  String value = json.getString("name");
  String value2 = json.getString("display_name");
You write 1 line for declaration plus 2 lines for each class using this API. This is not a big deal in terms of LoC and I'm not an LoC police. However, my sense is the cost outweigh the benefit.

What do you think?

Comments

lanna•1y ago
You just made TWO typos: "display-name" vs "display_name" and "_name" vs "name", automatically counter-argumenting your point.

It is also for documentation. With the declared constants, we know all possible values. With plain strings, how am I supposed to know which values to use?

The benefits far outweigh the marginal cost.

ramsicandra•1y ago
The -, _, and leading _ are just variations of white space / separator I have encountered. I think it's possible to document all the allowable values in the Javadoc section of the function that takes in string as their argument.

In the specific android example, I would put it here. Under projection params where it takes in all the Images.Media.* string consts.

https://developer.android.com/reference/android/content/Cont...

Though, if it's a practice of Java Engineer to document allowable enum like string as a constant, then I can say that's a valid argument.

B2B Software Is Still Stuck in the BlackBerry Era. That Is About to Change

https://www.attainoutcomes.com/blog/b2b-software-is-still-stuck-in-the-blackberry-era-that-is-abo...
1•tylerdane•48s ago•1 comments

Show HN: Turo – An Aggressive Token-Saving Proxy for CLI AI Agents

https://github.com/kdeps/turo
1•jjuliano•1m ago•0 comments

Plasma Tunnels Reveal How Dying Satellites Fall to Earth

https://spectrum.ieee.org/space-debris-atmosphere-burn-up
1•pseudolus•1m ago•0 comments

The model didn't escape. OpenAI ran the attack

https://adi2025.substack.com/p/the-model-didnt-escape-openai-ran
1•AdrianDrempetic•2m ago•0 comments

A Startling Glimpse at AI's Ruthless Efficiency

https://www.theatlantic.com/technology/2026/07/openai-hugging-face-hack/688025/
1•paulpauper•3m ago•0 comments

How the American Office Became Inevitable

https://www.theatlantic.com/newsletters/2026/07/office-white-collar-american-dream/688041/
1•paulpauper•3m ago•0 comments

Forgot your Google password? Now you can log in with a selfie

https://arstechnica.com/gadgets/2026/07/google-now-lets-you-log-into-your-account-with-a-selfie/
1•pseudolus•8m ago•1 comments

Intel stock jumps as it rides AI boom to fastest revenue growth in almost 15 yrs

https://www.cnbc.com/2026/07/23/intel-intc-earnings-report-q2-2026.html
1•ekorbia•14m ago•0 comments

Cosmological constant from Standard Model spin

https://zenodo.org/records/21515348
1•kisnorbert•14m ago•0 comments

Andrew Graham-Dixon on Vermeer, a Forgotten Sect, Underrated Artists (Ep. 284)

https://conversationswithtyler.com/episodes/andrew-graham-dixon/
1•paulpauper•15m ago•0 comments

Frontier intelligence, 10x lower cost

https://www.mixlayer.com/
1•becomevocal•17m ago•0 comments

Show HN: Life Tile – Passive time tracker that shows your day as a Mondrian grid

https://apps.apple.com/us/app/life-tile-auto-tracker/id6767332648
1•choyongjoon•19m ago•0 comments

Rising stars of mathematics awarded prestigious 2026 Fields Medal

https://www.nature.com/articles/d41586-026-02169-1
2•maxall4•19m ago•0 comments

Fred Wilson – The Clarity Act

https://avc.xyz/the-clarity-act
1•rmason•20m ago•0 comments

Australia to AI: Produce More Power Than You Burn, Stop Content 'Theft'

https://www.theregister.com/ai-and-ml/2026/07/15/australia-demands-ai-companies-must-produce-more...
2•gnabgib•21m ago•0 comments

Preliminary Assessment of Kimi K3's Cyber Capabilities

https://www.aisi.gov.uk/blog/preliminary-assessment-of-kimi-k3s-cyber-capabilities
2•antondd•21m ago•0 comments

Pixel phones getting ad notifications

3•explodes•23m ago•2 comments

If AI companies avoid AI slop, shouldn't we?

https://www.machinesociety.ai/p/if-ai-companies-avoid-ai-slop-shouldnt
3•mikelgan•28m ago•1 comments

Patreon Lays Off 20 Percent of Its Workforce

https://www.404media.co/patreon-lays-off-20-percent-of-its-workforce/
9•cdrnsf•41m ago•0 comments

Saying Goodbye to My Office

https://manilsuri.substack.com/p/on-saying-goodbye-to-my-office
5•tzury•42m ago•0 comments

Palo Alto Networks Plans to Buy Embrace in Observability Push

https://www.morningstar.com/news/dow-jones/202607219336/palo-alto-networks-plans-to-buy-embrace-i...
2•pranay01•46m ago•0 comments

Measuring the Performance Cost of High Availability in Postgres

https://clickhouse.com/blog/postgresbench-ha
3•saisrirampur•49m ago•0 comments

Middleware that catches when LLM conversations slowly drift off-topic

https://github.com/ojki74/hallucinatoff
1•ojki•54m ago•0 comments

Feral cats fail at urban rodent control (2018)

https://wildlife.org/rats-feral-cats-fail-at-urban-rodent-control/
5•vector_spaces•54m ago•0 comments

Now on the American Road: Bulletproof Cars

https://www.nytimes.com/2026/07/20/style/armored-cars-bulletproof-american-drivers.html
2•bookofjoe•55m ago•1 comments

The Visual 6502

http://visual6502.org/JSSim/index.html
8•infiniteregrets•56m ago•5 comments

How working out at the gym replaced the home improvement project

https://www.cnn.com/2026/07/23/business/gyms-millennials-gen-z-equinox
2•mooreds•59m ago•1 comments

China won the car wars with cheap EVs loaded with technology

https://www.rnz.co.nz/news/world/782063/how-china-won-the-car-wars-with-cheap-evs-loaded-with-tec...
13•billybuckwheat•1h ago•1 comments

The Online MBA

https://theonline.mba
4•multiplesauces•1h ago•1 comments

The Elm Architecture, Without Elm

https://bichanna.github.io/posts/tea-time/
3•theanonymousone•1h ago•0 comments