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.

A Chemical Hunger

https://slimemoldtimemold.com/2021/07/07/a-chemical-hunger-part-i-mysteries/
1•bilsbie•20s ago•0 comments

GPT-6.1 Terra

https://simonwillison.net/2026/Oct/26/gpt-61-terra/
1•hnisjafx40•27s ago•0 comments

Reddit shares plunge as Google's AI search squeezes traffic

https://www.reuters.com/business/reddit-pinterest-shares-tumble-weak-user-revenue-growth-2026-10-30/
2•randycupertino•1m ago•0 comments

NASA's Voyager 1 Conserves Power, Resolves Brief Communications Pause

https://www.jpl.nasa.gov/news/nasas-voyager-1-conserves-power-resolves-brief-communications-pause/
1•Stratoscope•1m ago•0 comments

macOS 27 Golden Gate does not support CoreStorage or HFS+ volumes

https://wadetregaskis.com/macos-27-golden-gate-does-not-actually-support-corestorage-or-hfs-volumes/
3•chmaynard•5m ago•0 comments

Report from the Vatican

https://garymarcus.substack.com/p/report-from-the-vatican
1•chmaynard•9m ago•0 comments

Show HN: Othismo, a Runtime for Sandboxed Objects

https://othismo.com
1•mamidon•9m ago•0 comments

Bidirectional Charging During Grid Outages

https://www.jefftk.com/p/bidirectional-charging-during-grid-outages
1•luu•10m ago•0 comments

I miss the old ways, so I am trying to find a middleground

https://github.com/TheMissenoOne/OpenArc
1•themissenoone•11m ago•0 comments

What Anthropic’s IPO Filing Actually Says

https://www.youtube.com/watch?v=Ic1Jf7VlXRM
3•Betelbuddy•13m ago•0 comments

NPM, "estee" is not a typo of "testem"

https://smoores.dev/post/no_npm_estee_is_not_a_typo/
2•birdculture•15m ago•0 comments

IdentityHub – Add Google, Dropbox, and email login to your SaaS in 5 minutes

https://www.identityhub.tech
1•wildreallywon•17m ago•0 comments

The New Food Movement

https://www.theatlantic.com/culture/2026/10/american-sustenance-omnivore-dilemma/688643/
1•bilsbie•19m ago•0 comments

The Lithium Hypothesis of Obesity

https://www.youtube.com/watch?v=t5NklTNoKDI
1•dakila5•20m ago•0 comments

The Golden Age

https://dark.ronacher.eu/2026/10/31/golden-age/
2•droidjj•22m ago•0 comments

Surviving "The Graduate"

https://www.newyorker.com/culture/the-weekend-essay/dustin-hoffman-on-surviving-the-graduate
2•samizdis•30m ago•0 comments

HackerTube: YouTube built from videos recommended by HN

https://karpathy.bearblog.dev/hacker-tube/
21•karpathy•35m ago•12 comments

Toit. Language for ESP32

https://toit.io/
2•wiradikusuma•36m ago•0 comments

LocalASO – App Store keyword research in 52 countries, on your Mac

https://localaso.com
1•gianmaurice•37m ago•0 comments

The Linux Command Line

https://linuxcommand.org/tlcl.php
2•kristianp•37m ago•0 comments

Show HN: Read your Character.AI data export in the browser, nothing uploaded

https://github.com/anima-universe/character-ai-export-viewer
1•Amavie•39m ago•0 comments

A Surveilled Country Is Not a Free Country

https://brownstone.org/articles/a-surveilled-country-is-not-a-free-country/
3•CGMthrowaway•42m ago•0 comments

Bub 0.6.0

https://github.com/bubbuild/bub/releases/tag/0.6.0
2•chojanshang•45m ago•0 comments

Evolving Agent Teams

https://arxiv.org/abs/2610.36848
1•anigbrowl•46m ago•0 comments

Automate the setup and configuration of your Windows development environment

https://github.com/microsoft/WindowsDeveloperConfig/
1•gjvc•49m ago•0 comments

Anthropic: Scaling Misuse Detection with Self-Assessment of Chats

https://twitter.com/AnthropicAI/status/2115871889549865429
7•cft•52m ago•0 comments

Anthropic blocks users for anything touching training larger models

https://twitter.com/jiahaoshao1/status/2116475922190922088
10•pretext•53m ago•0 comments

Hackers broke into four of Canada's top law firms

https://www.theglobeandmail.com/business/article-law-firms-hackers-how-did-it-happen-client-files...
5•Teever•54m ago•0 comments

Expert Knowledge and the Conditions of Its Validity: The Limits of Judgment

https://medium.com/@smoking2man/expert-knowledge-and-the-conditions-of-its-validity-the-limits-of...
3•Aldus551•57m ago•0 comments

Show HN: Aruma – clean-room self-hosted backend for the official Firebase SDKs

https://github.com/arumadev/aruma
3•osama200•57m ago•1 comments