frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

UK scientists developing new Ebola vaccine that could be ready in months

https://www.bbc.co.uk/news/articles/cy82gkr7xzlo
1•asplake•33s ago•0 comments

China's AI optimism isn't what it seems

https://www.chinatalk.media/p/chinas-ai-optimism-isnt-what-it-seems
1•speckx•1m ago•0 comments

Department of War Publishes Second Release of UAP Files

https://www.war.gov/UFO/?releaseDate=Release+02#records
1•keepamovin•1m ago•0 comments

Quadball Europe Announces European Quadball Cup 2026

https://www.quidditcheurope.org/post/quadball-europe-announces-european-quadball-cup-2026
1•throw_await•2m ago•0 comments

Ask HN: How much AI is in your writing?

1•js98•2m ago•0 comments

Open Source Silicon: the RedHat model works for chip design

https://www.youtube.com/watch?v=MAgHmIu1bK8
1•johncole•3m ago•0 comments

Many-Valued Logic

https://en.wikipedia.org/wiki/Many-valued_logic
1•the-mitr•4m ago•0 comments

Show HN: LLM-mock – Record real LLM API responses once, replay them in tests

https://pypi.org/project/llm-mock
1•roman_t•4m ago•0 comments

Agent-Based Modeling for Simulating U.S. Presidential Elections

https://arxiv.org/abs/2512.05982
1•ian_j_butler•4m ago•0 comments

Vibedock – macOS menu bar app to toggle Claude Code MCP servers

https://vibedock.dev/
1•TheoWtmn•5m ago•0 comments

VSCode Feature Request: Permissions, Sandboxing and Update Management (2018)

https://github.com/microsoft/vscode/issues/52116
1•airstrike•5m ago•0 comments

Hollywood Secures Broad "Omnibus" Pirate Site Blocking Order in UK High Court

https://torrentfreak.com/hollywood-secures-broad-omnibus-pirate-site-blocking-order-in-uk-high-co...
1•austinallegro•7m ago•0 comments

LZ77 decode at 9,903 MB/s: how we broke the sequential bottleneck on CPU

https://twitter.com/yasha1971/status/2057485786514125149
1•tosh•8m ago•0 comments

Linux95apps – Small and fast components that mimic Windows 95 on Linux

https://github.com/HalanoSiblee/linux95apps
1•ashitlerferad•8m ago•0 comments

Interactive Polls for Slidev

https://github.com/asm0dey/slidev-polls/
1•asm0dey•9m ago•0 comments

Goes to Eleven (2020)

https://bits.houmus.org/2020-01-28/this-goes-to-eleven-pt1
1•tosh•10m ago•0 comments

Epos Daimon, the Antifascist Magic School for Teens

https://mssv.net/2026/05/22/epos-daimon-the-antifascist-magic-school-for-teens/
1•adrianhon•11m ago•0 comments

Six search engines worth trying now that Google isn't Google anymore

https://techcrunch.com/2026/05/21/six-search-engines-worth-trying-now-that-google-isnt-really-goo...
1•dreamcompiler•11m ago•0 comments

Warp drive with positive invariant energy density, global Hawking-Ellis Type I

https://arxiv.org/abs/2512.18008
1•dan-bailey•13m ago•0 comments

Show HN: Wwwatch, a daily journal of AI tooling news filtered for builders

https://www.wwwatch.dev/
1•massmr•13m ago•0 comments

Diia - Ukraine gov app launched AI agent based on Google Gemini

https://babel.ua/en/news/126979-diia-launched-its-own-ai-agent-in-the-application-what-services-d...
1•defly•14m ago•0 comments

Microsoft accused of leaking Dutch civil servants' names to U.S. government

https://nltimes.nl/2026/05/22/microsoft-accused-leaking-dutch-civil-servants-names-us-government
4•giuliomagnifico•15m ago•0 comments

Introducing Nova, our internal platform for coding agents

https://dropbox.tech/machine-learning/introducing-nova-our-internal-platform-for-coding-agents
1•el_duderino•17m ago•0 comments

Are Humanoid Robots the End of Human Work?

https://nautil.us/are-humanoid-robots-the-end-of-human-work-1281110
1•ibobev•18m ago•0 comments

Navigating AI with paper maps – Adam

https://adsurg.substack.com/p/navigating-ai-with-paper-maps
1•rbanffy•19m ago•0 comments

Alexander Grothendieck Revolutionized 20th-Century Mathematics

https://www.quantamagazine.org/how-alexander-grothendieck-revolutionized-20th-century-mathematics...
1•ibobev•19m ago•0 comments

Two Researchers Are Rebuilding Mathematics from the Ground Up

https://www.quantamagazine.org/two-researchers-are-rebuilding-mathematics-from-the-ground-up-2026...
1•ibobev•19m ago•0 comments

2026 Hugo Awards Nominees

https://www.thehugoawards.org/hugo-history/2026-hugo-awards/
1•throw0101c•19m ago•0 comments

Valve removes free game from Steam after players discover it contains malware

https://www.pcguide.com/news/valve-removes-free-horror-game-from-steam-after-players-discover-it-...
12•gpi•21m ago•3 comments

Helium – Open-source student planner, 15 years in the making

https://landing.heliumedu.com/
2•alexdlaird•22m ago•0 comments