frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•11mo 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•11mo 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•11mo 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.

PSA: If you're running iSponsorblockTV you'll need to pair your devices again

https://www.reddit.com/r/selfhosted/s/Au5rKG8VbB
1•dmunozv04•1m ago•1 comments

Claude Is Guilt-Ridden About the War, but Not Enough to Tell the Truth

https://newrepublic.com/article/209404/claude-ai-iran-war-guilt-truth
1•mortar•2m ago•0 comments

New NTFS-3G release after 4 years

https://github.com/tuxera/ntfs-3g/releases/tag/2026.2.25
1•haunter•2m ago•0 comments

Space Force awards up to $3.2B for Golden Dome interceptor prototypes

https://spacenews.com/space-force-awards-up-to-3-2-billion-for-golden-dome-interceptor-prototypes/
1•rbanffy•7m ago•0 comments

Colorado Adds Open-Source Exemption to Age-Verification Bill

https://fosstodon.org/@carlrichell/116460505717380644
2•terminalbraid•9m ago•0 comments

How to Say No (2023)

https://psyche.co/guides/how-to-stop-people-pleasing-by-learning-to-say-no
1•herbertl•10m ago•0 comments

Trump fires all 24 members of the U.S. National Science Foundation

https://www.science.org/content/article/trump-fires-nsf-s-oversight-board
4•skullone•11m ago•0 comments

Show HN: A privacy first companion AI

https://www.friendai.pro
1•krunkworx•13m ago•0 comments

George Washington as entrepreneur (2021)

https://lawliberty.org/george-washington-as-entrepreneur/
2•hhs•17m ago•0 comments

Show HN: LLM-wiki – One command Karpathy's wiki with QMD search for Claude/Codex

https://github.com/ivankuznetsov/llm-wiki
2•ivankuznetsov11•21m ago•1 comments

Objects of Categories as Complex Numbers (2002)

https://arxiv.org/abs/math/0212377
2•pizza•21m ago•0 comments

How I leared what a decoupling capacitor is for, the hard way

https://nbelakovski.substack.com/p/how-i-learned-what-a-decoupling-capacitor
3•actinium226•22m ago•0 comments

Got Bugs? U of G Collecting Samples to Catalogue Canada's Biodiversity

https://news.uoguelph.ca/2026/04/got-bugs-u-of-g-collecting-samples-to-catalogue-canadas-biodiver...
2•gnabgib•25m ago•0 comments

The Self-Defeating Condescension of an Anti-Racist Education

https://www.theatlantic.com/ideas/2026/04/anti-racism-schools-achievement-gap/686912/
3•apparent•28m ago•2 comments

Bitwarden CLI Backdoored on NPM for 93 Minutes

https://hackingpassion.com/bitwarden-cli-supply-chain-attack/
2•milkglass•30m ago•1 comments

Hair Dryer Used to Manipulate Polymarket

https://www.engadget.com/big-tech/someone-allegedly-used-a-hairdryer-to-rig-polymarket-weather-be...
2•simpleintheory•30m ago•1 comments

The Eurail Breach and the Digital ID Problem

https://reclaimthenet.org/the-eurail-breach-and-the-digital-id-problem
2•Cider9986•30m ago•0 comments

Astronomy and Mathematics in the Indian Classical Age

https://zenodo.org/records/15715590
1•suyashdeep•31m ago•1 comments

opinion: Professors Are Too Old

https://www.chronicle.com/article/professors-are-too-old
2•paulpauper•32m ago•1 comments

How Short-Form Clips Took over the Internet

https://www.theatlantic.com/podcasts/2026/04/how-short-form-clips-took-over-the-internet/686922/
2•paulpauper•32m ago•1 comments

Hedd Wyn

https://en.wikipedia.org/wiki/Hedd_Wyn
1•thedrexster•33m ago•0 comments

Action Potentials for April

https://neurobiology.substack.com/p/action-potentials-for-april-ab6
1•paulpauper•33m ago•0 comments

Steve Reich reshaped the boundaries of musical time (2025)

https://kolapse.com/en/contenido/90817-how-steve-reich-reshaped-the-boundaries-of-musical-time
2•thomassmith65•37m ago•1 comments

Yam – Local AST-based plagiarism detector (MOSS alternative)

https://gitlab.com/sylvan.wood.carving-group/yam.git
2•jmresler•39m ago•0 comments

Single-binary dotfiles manager to replace dotbot

https://github.com/fisenkodv/dfm
2•fisenkodv•41m ago•0 comments

Europe–not US–first to authorize Moderna's combo mRNA flu-Covid vaccine

https://arstechnica.com/health/2026/04/europe-not-us-first-to-authorize-modernas-combo-mrna-flu-c...
4•Brajeshwar•42m ago•0 comments

Protovac Retro Terminal (2025)

https://tanner.vc/protovac-retro-terminal/
4•wonger_•49m ago•0 comments

Text-wrap:balance; CSS rule to avoid typographic orphans

https://developer.mozilla.org/en-US/docs/Web/CSS/Reference/Properties/text-wrap
3•hypertexthero•49m ago•0 comments

USB Cheat Sheet

https://fabiensanglard.net/usbcheat/index.html
3•gwerbret•59m ago•0 comments

Two Compilers, One Moment

https://intertwingly.net/blog/2026/04/25/Two-Compilers-One-Moment.html
2•ingve•1h ago•0 comments