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.

Trump Suggests U.S. 'Shouldn't Even Have' November Midterms

https://www.forbes.com/sites/alisondurkee/2026/01/15/trump-suggests-us-shouldnt-even-have-novembe...
1•Tadpole9181•1m ago•0 comments

Digg launches its new Reddit rival to the public

https://techcrunch.com/2026/01/14/digg-launches-its-new-reddit-rival-to-the-public/
1•madihaa•1m ago•0 comments

Visualize OpenUSD in Rerun

https://github.com/art-e-fact/usd-rerun-logger
1•Tycho87•1m ago•0 comments

The seam through the center of things – on addiction, God, and grace

https://usefulfictions.substack.com/p/the-seam-through-the-center-of-things
1•sebg•2m ago•0 comments

Are There Accumulating Microplastics in Human Tissue or Not?

https://www.science.org/content/blog-post/are-there-accumulating-microplastics-human-tissue-or-not
1•Tomte•4m ago•0 comments

Show HN: 1Code – open-source Cursor-like UI for Claude Code

https://github.com/21st-dev/1code
4•Bunas•5m ago•1 comments

Apple lost the AI race – now the real challenge starts

https://www.theverge.com/tech/861957/google-apple-ai-deal-iphone-gemini
1•speckx•8m ago•0 comments

Scientists Put Teeth into Water-Driven Gears

https://www.nyu.edu/about/news-publications/news/2026/january/scientists-put-teeth-into-water-dri...
1•wjSgoWPm5bWAhXB•10m ago•0 comments

Show HN: BlogHunter – Generate and host SEO blog posts with AI

https://bloghunter.se/
1•the_plug•11m ago•0 comments

In Memoriam: Remembering Mike Flynn

https://www.computer.org/publications/tech-news/insider-membership-news/in-memoriam-mike-flynn
2•jecel•11m ago•2 comments

Are people avoiding iOS 26 because of Liquid Glass? It's complicated

https://arstechnica.com/gadgets/2026/01/are-people-avoiding-ios-26-because-of-liquid-glass-its-co...
1•speckx•12m ago•0 comments

Southern California has an unlikely AI mecca: the industrial Vernon

https://www.latimes.com/business/story/2025-12-30/southern-california-has-unlikely-ai-mecca-very-...
1•PaulHoule•13m ago•0 comments

Fibonacci in the NYT Pips

https://benkettle.xyz/posts/fibonacci-pips/
2•bkettle•13m ago•0 comments

Reflections on TA-ing Harvard's first AI safety course

https://www.lesswrong.com/posts/gcFB2RT5vpKHbH4ic/reflections-on-ta-ing-harvard-s-first-ai-safety...
1•sebg•14m ago•0 comments

The production and autonomy gap shaping modern drone warfare

https://tots.nerdrums.com/p/affordable-mass
1•Justin_N•14m ago•1 comments

Why Can't I Focus Anymore? The Science of Attention Decline

https://www.focusfit.app/blog/why-cant-i-focus-anymore
2•shenli3514•17m ago•0 comments

"Civil Disobedience" – Henry David Thoreau (1849) [pdf]

https://blogs.law.columbia.edu/uprising1313/files/2017/10/Civil-Disobedience-by-Henry-David-Thore...
2•dataviz1000•18m ago•0 comments

Open source MySQL repository has no commits in more than three months

https://devclass.com/2026/01/13/open-source-mysql-repository-has-no-commits-in-more-than-three-mo...
3•firesteelrain•18m ago•1 comments

Thumby, a Tiny Playable Keychain

https://thumby.us/
1•tosh•20m ago•0 comments

Decreasing human body temperature in United States since Industrial Revolution

https://elifesciences.org/articles/49555
2•paulpauper•22m ago•1 comments

Astronauts splash down to Earth after medical evacuation from space station

https://www.bbc.com/news/articles/c205r8n0276o
2•reconnecting•22m ago•1 comments

OpenAI Codex Zoom Event – 10xing Eng Velocity

https://bvp.zoom.us/webinar/register/WN_bul7bYg6RcCXBuxl30KwRA#/registration
1•bnagda•22m ago•2 comments

Show HN: Skillthis.ai – Generate AI skills using Claude's best practices

https://skillthis.ai
1•barefootsanders•23m ago•1 comments

Burn 0.20.0: Unifying CPU and GPU Kernels with CubeCL

https://burn.dev/blog/release-0.20.0/
2•dabinat•24m ago•0 comments

Visualizing and managing Pipewire audio graphs from Emacs

https://sachachua.com/blog/2026/01/visualizing-and-managing-pipewire-audio-graphs-from-emacs/
1•JNRowe•24m ago•0 comments

Archaeologists uncover Victorian children's schoolwork in east London

https://www.ianvisits.co.uk/articles/archaeologists-uncover-victorian-childrens-schoolwork-in-eas...
1•7777777phil•24m ago•0 comments

UK offshore wind prices come in 40% cheaper than gas in record auction

https://electrek.co/2026/01/14/uk-offshore-wind-record-auction/
7•doener•26m ago•3 comments

Show HN: I built an 11MB offline PDF editor because mobile Acrobat is 500MB

https://revpdf.com/
3•pawandeepsingh•26m ago•1 comments

How to Bounce Postal Mail

https://blog.zgp.org/how-to-bounce-postal-mail/
2•speckx•27m ago•0 comments

Ratelimit is a Feature, not a constraint

https://dantelex.com/blog/ratelimits-are-features
1•lexokoh•28m ago•0 comments