frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Generative Intuition

https://nikitph.medium.com/generative-intuition-a1048fb2b820
1•loaderchips•36s ago•0 comments

Tell HN: Happy New Year

1•schappim•1m ago•1 comments

Migadu Email: The Missing Email Service for Domains

https://migadu.com/
1•janandonly•2m ago•0 comments

A Beginner's Two-Component Crystal-Style Wi-Fi Detector

https://siliconjunction.wordpress.com/2025/12/12/a-beginners-two-component-crystal-style-wi-fi-de...
2•jensgk•3m ago•0 comments

Exploiting Silent Delivery Receipts to Monitor Users on Instant Messengers

https://github.com/gommzystudio/device-activity-tracker
1•tcsenpai•3m ago•0 comments

Claude Code Anywhere

https://happy.engineering/
1•mellosouls•4m ago•1 comments

Show HN: TimeProofs – Prove when data existed without uploading it

1•timeproofs•5m ago•1 comments

Two Decades of Piracy Reporting: TorrentFreak's Retrospective

https://torrentfreak.com/2025-two-decades-of-piracy-reporting-torrentfreaks-retrospective/
2•throw0101c•6m ago•0 comments

Immutable Infrastructure, Immutable Code

https://aicoding.leaflet.pub/3mbaguyrjek2g
1•gpi•7m ago•0 comments

'Ghost resorts': 100s of ski slopes lie abandoned–will nature reclaim Alps?

https://www.theguardian.com/environment/2025/dec/27/alps-france-skiing-snow-warming-resorts-closi...
2•bookofjoe•7m ago•0 comments

Stages of AI Grief

https://sellsbrothers.com/the-5-stages-of-ai-grief
2•goosebump•9m ago•0 comments

The science of how (and when) we decide to speak out–or self-censor

https://arstechnica.com/science/2025/12/the-science-of-how-and-when-we-decide-to-speak-out-or-sel...
1•asplake•10m ago•0 comments

The Best Cable in the World (For Developers)

https://eliot.blog/p/the-best-cable-in-the-world-for-developers
2•ea016•11m ago•0 comments

Best Free Online Escape Room Games in 2026

https://onlineescaperoom.org
1•weihz5138•16m ago•1 comments

Immigrant Derangement Syndrome

https://paulkrugman.substack.com/p/immigrant-derangement-syndrome
4•rbanffy•17m ago•1 comments

Nvidia GB10's Memory Subsystem, from the CPU Side

https://chipsandcheese.com/p/inside-nvidia-gb10s-memory-subsystem
2•ingve•20m ago•0 comments

Nvidia Groq Update: Everyone Gets Rich, Patent Warfare Begins

https://ossa-ma.github.io/blog/groq-update
4•simonebrunozzi•23m ago•0 comments

Show HN: LoureiroGate – Differentiable Physics Guardrails for PyTorch

https://github.com/Ashioya-ui/loureiro-gate
1•pb_lightmind•30m ago•2 comments

The Cost of Translation

https://www.ramijames.com/thoughts/the-cost-of-translation
9•ramijames•31m ago•0 comments

Vect AI – The Autonomous Marketing OS for 10x Growth

https://www.google.com/search?q=site%3Avect.pro&oq=s&gs_lcrp=EgZjaHJvbWUqCAgDEEUYJxg7MgYIABBFGDwy...
1•WoWSaaS•31m ago•3 comments

Fifteen Most Famous Transcendental Numbers

https://sprott.physics.wisc.edu/pickover/trans.html
7•vismit2000•31m ago•0 comments

TPU Mania

https://thechipletter.substack.com/p/tpu-mania
1•klelatti•31m ago•0 comments

Hide Distractions and Clutter on GitHub

https://codeberg.org/toastal/github-less-social
1•8organicbits•32m ago•0 comments

Assessment of the effects of large-scale PV development in desert areas

https://www.nature.com/articles/s41598-024-72860-8
1•zeristor•32m ago•0 comments

The Shifting World of OSINT with Craig Silverman [video]

https://www.youtube.com/watch?v=Lmj42F6HrPo
1•gessha•34m ago•0 comments

Show HN: An AI social networking app that treats hallucinations as a feature

https://friendtest.fun/
1•funldsaw•39m ago•0 comments

How supermarkets turned loyalty cards into a data treasure trove

https://www.rte.ie/brainstorm/2025/1227/1407120-supermarket-loyalty-cards-customers-data-behaviou...
2•austinallegro•41m ago•2 comments

The world wants more Ube

https://www.nytimes.com/2025/12/29/world/asia/philippines-ube-purple-yam.html
1•lawgimenez•42m ago•0 comments

My Writing Isn't AI Slop–and It Hurts That You Think It Is

https://news.ycombinator.com/submitted?id=haebom
2•haebom•44m ago•1 comments

Show HN: Your backend will fail eventually

https://www.zoyla.app
2•behnamazimi•45m ago•1 comments