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.

Solving rubiks cubes "without search"

https://arxiv.org/abs/2508.13113
1•E-Reverance•2m ago•0 comments

Firefox Telemetry Dictionary

https://dictionary.telemetry.mozilla.org/apps/firefox_desktop
1•M95D•3m ago•1 comments

Finding Bugs

https://matklad.github.io/2026/09/19/finding-bugs.html
1•surprisetalk•4m ago•0 comments

What do you think is the prevalence of this phenomenon in current AI discourse?

https://en.wikipedia.org/wiki/Preference_falsification
1•symbolicai•6m ago•0 comments

Economics Nobel Laureates' Letter Supporting the California Billionaire Tax [pdf]

https://gabriel-zucman.eu/files/prop40letter.pdf
3•marojejian•9m ago•2 comments

Markers to Deter Human Intrusion into Waste Isolation Pilot Plant (1993) [pdf]

https://gwern.net/doc/technology/1993-trauth.pdf
1•walrus01•9m ago•0 comments

ZK-JPEG: Zero-Knowledge Image Editing and Compression

https://eprint.iacr.org/2026/2039
2•gslin•11m ago•0 comments

Disney+ changes subscriber agreement to allow ads on every plan

https://www.dexerto.com/tv-movies/disney-changes-subscriber-agreement-to-allow-ads-on-every-plan-...
8•DemiGuru•13m ago•1 comments

Screens Ubiquitous in Classrooms; Teachers Split on Efficacy

https://news.gallup.com/poll/714479/screens-ubiquitous-classrooms-teachers-split-efficacy.aspx
1•hn_acker•14m ago•0 comments

Tiny radio transmitters reveal a hidden survival tactic in birds

https://theconversation.com/tiny-radio-transmitters-reveal-a-hidden-survival-tactic-in-birds-274517
2•bryanrasmussen•15m ago•0 comments

The Working Class Face a 'Class Ceiling' in Elite Occupations

https://www.nytimes.com/2026/09/18/business/class-barrier-academia.html
2•marojejian•23m ago•1 comments

Proxmox runs QEMU-img convert with cache=unsafe; write errors are dropped

https://bugzilla.proxmox.com/show_bug.cgi?id=8054
1•alfonsokuen•24m ago•0 comments

No Easy Fix for Bogus Respondents in Online Opt-In Polls

https://www.pewresearch.org/methods/2026/08/27/no-easy-fix-for-bogus-respondents-in-online-opt-in...
1•hn_acker•25m ago•0 comments

I scraped all the ivf cycles that have ever been posted on the internet

https://eggs.help
1•grapefruitjuice•26m ago•0 comments

A social network where posts are minigames generated from tweet-sized prompts

https://gamecrumbs.online/
1•saint_angels•27m ago•1 comments

Vitaebase – an interactive map of longevity research

https://www.vitaebase.net
1•oibafcodes•28m ago•2 comments

FBI memo warned ICE agents could be targeted by fake 'tradwife' dating profiles

https://www.theguardian.com/us-news/2026/sep/17/fbi-ice-tradwife-doxing
1•hn_acker•28m ago•1 comments

Show HN: Jev to JSON-Schema

https://github.com/Kiln-AI/jev_jsonschema
1•scosman•31m ago•0 comments

Warning Signs [Economics of AI]

https://newleftreview.org/sidecar/posts/warning-signs
1•ckmate-king-2•33m ago•0 comments

A Consciousness Is a Dedekind Cut

https://www.evalapply.org/posts/consciousness-lives-in-a-dedekind-cut/index.html
1•andsoitis•34m ago•0 comments

Frustrated at state of commentary of economic impact of AI

1•erads•34m ago•0 comments

A new kind of AI model from a ChatGPT inventor is thrilling developers

https://techcrunch.com/2026/09/18/a-new-kind-of-ai-model-from-a-chatgpt-inventor-is-thrilling-dev...
1•andsoitis•39m ago•0 comments

Unsolved Historical Ciphers

https://dbourdeau.github.io/cyphersolver/index.html
2•Bluestein•40m ago•0 comments

The lucrative business of reactionary AI-generated posts on Facebook

https://www.lemonde.fr/en/pixels/article/2026/09/19/the-lucrative-business-of-reactionary-ai-gene...
1•geox•41m ago•0 comments

DNS Honeypots Don't Fail at Banners,They Fail at the Protocol

https://medium.com/meetcyber/dns-honeypots-dont-fail-at-banners-they-fail-at-the-protocol-cf21c7e...
1•ls1911•43m ago•0 comments

Open Weights Are Good. Open Source Is Better

https://opensource.org/blog/open-weights-are-good-open-source-is-better
5•theanonymousone•44m ago•1 comments

The senior engineer death spiral

https://sunilpai.dev/posts/the-senior-engineer-death-spiral/
6•napolux•45m ago•1 comments

Show HN: The meal planner that gets all your dishes ready at once

https://robotrecipes.co/mise/
1•maverik•46m ago•0 comments

Your robotic vacuum is a witness: evidence, privacy and the law

https://theconversation.com/your-robotic-vacuum-is-a-witness-what-home-robots-mean-for-evidence-p...
1•hn_acker•46m ago•1 comments

Poster Prompts One hundred design styles as copy-paste prompts

https://john.hartnup.uk/poster-prompts/
1•Bluestein•48m ago•0 comments