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.

First Genesis Mission Projects Selected to Boost AI-Driven Scientific Discovery

https://www.energy.gov/articles/secretary-energy-chris-wright-announces-first-genesis-mission-pro...
1•gmays•4m ago•0 comments

Show HN: RL bandits pick coding agent's context,Grok 4.5 out-fixes Fable 5

https://github.com/VinvAI/VinvAI
2•sohamac•7m ago•0 comments

PostgreSQL's MVCC is bad. So is everyone else's

https://boringsql.com/posts/mvcc-bad-bad/
1•soheilpro•11m ago•0 comments

X Money officially launches in the US

https://9to5mac.com/2026/07/27/x-money-apple-wallet/
1•slymax•14m ago•0 comments

Abandoned Navy base costs taxpayers $340k pa for internet no one uses

https://taskandpurpose.com/news/abandoned-navy-base-internet-service/
2•Gaishan•19m ago•0 comments

Lilian Weng Resigns from Thinking Machines Lab

https://digg.com/tech/yhf6ei1b
1•doppp•23m ago•0 comments

Big Tech's Money Men Don't Care About the Backlash

https://www.politico.com/news/magazine/2026/07/26/ai-super-pac-operatives-profile-01008227
2•reasonableklout•26m ago•0 comments

Show HN: _done – Expanding AI Capabilities

https://underscoredone.com/
1•onescales•26m ago•0 comments

S.Korea's KOSPI tumbles nearly 5% as chipmakers slump on AI worries

https://www.reuters.com/world/asia-pacific/skoreas-kospi-tumbles-nearly-5-chipmakers-slump-ai-wor...
2•rvz•27m ago•0 comments

QuickLogic eFPGA IP

https://www.quicklogic.com/efpga-ip/
1•peter_d_sherman•30m ago•0 comments

Tag Universal Machine Pitch Deck [pdf]

https://taguniversal.github.io/digital_blockchain_patents/TAG_Pitch_Deck.pdf
1•taguniversalm•32m ago•0 comments

Velonus – now in beta, with AI triage and one-click fix PRs

https://www.velonus.com/
1•AliAmmar15•37m ago•0 comments

Copyright Scammers Weaponized Meta's Rights Manager with Backdating Exploit

https://torrentfreak.com/copyright-scammers-weaponized-metas-rights-manager-with-backdating-exploit/
1•gslin•37m ago•0 comments

Programmable Chip That Can Slow Light on Command

https://scitechdaily.com/scientists-built-a-programmable-chip-that-can-slow-light-on-command/
1•Gaishan•37m ago•0 comments

Vehicle Motion Cues

https://support.apple.com/guide/iphone/iphone-comfortably-riding-a-vehicle-iph55564cb22/ios
2•Austin_Conlon•38m ago•0 comments

The Cyclospora outbreak was, and still is, a communications failure

https://www.cidrap.umn.edu/cyclospora/cidrap-op-ed-cyclospora-outbreak-was-and-still-communicatio...
3•testingonetwo34•45m ago•0 comments

SlopCodeBench

https://www.scbench.ai
3•jlaneve•45m ago•0 comments

Editing text when the markup is stored separately [pdf]

https://www.researchgate.net/publication/340006851_SPEEDy_A_practical_editor_for_texts_annotated_...
2•argimenes•47m ago•0 comments

Meta Got Everything It Wanted in a Secret Louisiana Data Center Deal

https://www.nytimes.com/2026/07/27/technology/meta-data-center-louisiana.html
2•spenvo•48m ago•0 comments

Microsoft finetuned Qwen 27B for computer use

https://huggingface.co/microsoft/Fara1.5-27B
2•georgeven•52m ago•1 comments

Travis Kalanick reveals how A16Z fumbled Uber's Series B in 2011

https://www.youtube.com/watch?v=6OZLg00B5yo
1•dsantbakshsing•53m ago•0 comments

Essential Words

https://pudding.cool/2026/07/essential-words/
1•herbertl•55m ago•0 comments

The gap is not glorious

https://loveofallwisdom.com/blog/2026/07/the-gap-is-not-glorious/
1•herbertl•56m ago•0 comments

I Guess I'm Playing the Long Game Now

https://brainbaking.com/post/2026/07/i-guess-im-playing-the-long-game-now/
2•herbertl•59m ago•0 comments

Caracal AV – Click the link to learn about it

https://caracal-av.pages.dev/
2•telui•1h ago•0 comments

Show HN: Building a new game everyday with AI. Day #106 Zombie Survival Training

https://gamevibe.us/106-zombie-survival-training
1•pzxc•1h ago•0 comments

I built 337 API tools behind a paywall. 3 free calls, then €0.50

https://swarm.gadgethumans.com/x402/
1•apipaywall•1h ago•0 comments

Duress passcode leads to U.S. prosecution of traveler

https://www.thetravel.com/us-customs-and-border-protection-faces-first-ever-type-incident-after-t...
3•exhilaration•1h ago•0 comments

Just in Time Nudging (2017)

https://idiallo.com/blog/just-in-time-nudging
2•foxfired•1h ago•0 comments

VitaSet LLC has applied for YC's Fall 2026 Batch

https://account.ycombinator.com?continue=https%3A%2F%2Fapply.ycombinator.com%2Fapps%2Fda68107c-2e...
1•VitaSetLLC•1h ago•0 comments