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.

Networking problems after the October Windows update? NTLM might be to blame

https://www.theregister.com/security/2026/10/16/networking-problems-after-the-october-2026-window...
1•wondersky•46s ago•0 comments

Sterile Knowledge: Artificial Intelligence, and the Genealogy of Understanding

https://medium.com/@smoking2man/sterile-knowledge-artificial-intelligence-models-and-the-genealog...
1•Aldus551•2m ago•0 comments

Israeli settlers attack olive framers & Washington Post journalist in West Bank

https://www.theguardian.com/world/2026/oct/16/israeli-settlers-attack-olive-farmers-west-bank-was...
2•0x54MUR41•4m ago•0 comments

Mapping the China AI Ecosystem with Rui Ma [video]

https://www.youtube.com/watch?v=-RDpqweadlE
1•hunglee2•12m ago•0 comments

MIT Scientist (Sabatini) Wins $6M in Sexual Harassment Defamation Suit

https://www.nytimes.com/2026/10/16/us/mit-scientist-sexual-harassment-defamation-lawsuit-david-sa...
2•osnium123•17m ago•0 comments

Ask HN: For those who left Big Tech for early-stage startups, do you regret it?

2•Peaceful-Hours•18m ago•0 comments

Are we sleepwalking into World War III?

https://www.ft.com/content/3a56575e-ecfb-4fa9-af70-01aebd869729
3•JumpCrisscross•20m ago•0 comments

The life and tools of H. C. Fackelman

https://journal.spencerwnelson.com/entries/who_is_fackelman.html
1•spenczar5•20m ago•0 comments

Maybe some hope for the future

https://kadhirvelm.dev/blogs/maybe-some-hope
1•kadhirvelm•24m ago•0 comments

Archive of Our Own

https://archiveofourown.org/
2•debo_•25m ago•0 comments

The Opposite of Love

https://acoup.blog/2026/10/16/teaching-paradox-hearts-of-iron-iv-part-iiib-the-opposite-of-love/
1•Tomte•33m ago•0 comments

Vercat – Vercel on the Go

https://www.vercat.dev
1•kuoxinbuild•33m ago•1 comments

Decoding base64 in Python at 10 gigabytes per second

https://lemire.me/blog/2026/10/17/decoding-base64-in-python-at-10-gigabytes-per-second/
1•smilelamp•34m ago•0 comments

EPGM: Execution Provenance for Budgeted Agent Memory Retrieval

https://arxiv.org/abs/2609.25913
1•axis_official•36m ago•0 comments

Huge Pages can improve Postgres performance

https://clickhouse.com/blog/huge-pages-clickhouse-managed-postgres
2•saisrirampur•39m ago•0 comments

Adversarial stress-testing of long-horizon multi-agent systems

https://arxiv.org/abs/2609.17320
1•anigbrowl•39m ago•0 comments

ASI existential risk: reconsidering alignment as a goal

https://michaelnotebook.com/xriskbrief/
2•reasonableklout•42m ago•0 comments

Beyond Zero-Skipping: Redundancy-Aware Arch. and Systems for Efficient Inference [video]

https://www.youtube.com/watch?v=WF69XRIqSpU
1•matt_d•42m ago•0 comments

The world epidemic of sleep disorders is linked to Vitamin D deficiency (2012)

https://pubmed.ncbi.nlm.nih.gov/22583560/
3•andsoitis•43m ago•0 comments

Roman fort found in Wales could prove myth of emperor's camp is real

https://www.theguardian.com/uk-news/2026/oct/12/mythical-roman-fort-frenni-fawr-wales-emperor
3•gmays•44m ago•0 comments

Peatland Fires Darken Skies in Indonesia

https://science.nasa.gov/missions/aqua/peatland-fires-darken-skies-in-indonesia/
2•teleforce•45m ago•0 comments

Implementing the Lindsey O. Graham Sanctioning Russia and Iran Act of 2026

https://www.whitehouse.gov/presidential-actions/2026/10/implementing-the-lindsey-o-graham-sanctio...
4•defly•48m ago•0 comments

Show HN: Crowned – A live king-of-the-hill billboard for projects

https://www.crowned.wtf/
1•lazzzyyycoder•50m ago•0 comments

NOAA oceanographer explains Kelvin waves and their part in El Niño

https://www.fisheries.noaa.gov/feature-story/kelvin-waves-explained
3•divbzero•55m ago•0 comments

Space: Sandboxed Platform for Agentic Code Execution

https://github.com/perplexityai/SPACE
2•Palmik•57m ago•0 comments

Moon-Like Madagascar

https://science.nasa.gov/earth/earth-observatory/moon-like-madagascar/
2•geox•57m ago•0 comments

I made a New tab page to give you NASA's Image of the day everytime you newtab

https://avycado13.github.io/newpage/
2•avycado13•1h ago•0 comments

Show HN: Llmspeed – feel how fast a local LLM runs before you buy the hardware

https://llmspeed.dev/
2•albinstman•1h ago•0 comments

Tiered caching: the checkbox that cuts origin load and almost nobody checks

https://plimie.com/blog/tiered-caching-origin-load/
1•pcaminog•1h ago•0 comments

CDN cache hit ratio is lying to you

https://plimie.com/blog/cdn-cache-hit-ratio-lying/
1•pcaminog•1h ago•0 comments