frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Nvidia releases 8B model with learned 8x KV cache compression

https://huggingface.co/nvidia/Qwen3-8B-DMS-8x
1•alecco•2m ago•0 comments

Notes on Afghanistan

https://mattlakeman.org/2026/01/05/notes-on-afghanistan/
1•phsource•3m ago•0 comments

Oak-killing beetle reaches Ventura County, significantly expanding range

https://www.latimes.com/environment/story/2026-01-10/it-is-scary-oak-killing-beetle-reaches-ventu...
3•PaulHoule•4m ago•0 comments

Uptime Monitoring

https://github.com/AlertSleep/awesome-uptime-monitoring
1•thepatrykooo•4m ago•0 comments

I built Git for Minecraft for a hackathon and won [video]

https://www.youtube.com/watch?v=ZdM-iNpv3nU
1•radeeyate•5m ago•0 comments

How to stand out in the dead internet

https://letters.thedankoe.com/p/the-death-of-value-based-content
1•saikatsg•6m ago•0 comments

Favicons are broken in Google search results

https://www.google.com/search?q=chatgpt
1•sh_tomer•6m ago•1 comments

Detained MN women help ICE agent having a seizure

https://www.startribune.com/detained-by-ice-two-women-became-first-responders-during-agents-seizu...
6•MaysonL•7m ago•0 comments

What usually happens after a VC asks for a demo?

2•stijo•8m ago•0 comments

Pico: The tiniest coding agent (6 LoC)

https://github.com/barghouthi/pico-swe-agent
2•smokesy•9m ago•0 comments

Claudito: A web interface for Claude Code that interacts with the CLI

https://github.com/comfortablynumb/claudito
2•hepha1979•10m ago•0 comments

Show HN: Polymcp – Turn Any Python Function into an MCP Tool for AI Agents

1•justvugg•10m ago•0 comments

Agent Orchestration for the Timid

https://substack.com/inbox/post/185649875
1•markferree•12m ago•0 comments

ICE has killed another person in Minneapolis

https://www.theverge.com/news/867245/ice-has-killed-another-person-in-minneapolis
25•saubeidl•14m ago•11 comments

StackWarp security vulnerability that exploits a synchronization bug AMD Zen 1–5

https://stackwarpattack.com/
3•_____k•16m ago•0 comments

J.K. Lundblad on X: "Rise of the Reflyable Rocket (Part 1)" / X

https://twitter.com/JK_Lundblad/status/2015034130640306649
2•bilsbie•17m ago•0 comments

JSON-render: LLM-based JSON-to-UI tool

https://json-render.dev/
3•rickcarlino•25m ago•0 comments

James Cameron says he has left the US permanently

https://www.theguardian.com/film/2026/jan/23/james-cameron-left-the-us-permanently-covid-new-zealand
6•belter•26m ago•4 comments

Thousands of scientists inflate their CVs with self-published studies

https://english.elpais.com/science-tech/2026-01-22/thousands-of-scientists-inflate-their-cvs-with...
3•belter•27m ago•1 comments

Open Policy Agent

https://www.openpolicyagent.org/
1•locknitpicker•27m ago•0 comments

The US Is Losing Top Tech Talent to India in the Wake of Trump's H-1B Chaos

https://www.bloomberg.com/news/articles/2026-01-23/us-loses-top-tech-talent-to-india-in-wake-of-h...
9•alephnerd•28m ago•2 comments

The Concatative Language XY

http://www.nsl.com/k/xy/xy.txt
3•ofalkaed•30m ago•1 comments

Show HN: Viahuman – Human In loop Notification system for LLMs

https://viahuman.xyz/
1•pdrojack•33m ago•0 comments

The Coming Second Copernican Revolution

https://www.noemamag.com/the-coming-second-copernican-revolution/
2•andsoitis•34m ago•0 comments

How WhatsApp Scaled to Billions of Users

https://singhajit.com/whatsapp-scaling-secrets/
3•arter45•34m ago•1 comments

BirdyChat becomes first European chat app that is interoperable with WhatsApp

https://www.birdy.chat/blog/first-to-interoperate-with-whatsapp
56•joooscha•34m ago•10 comments

Freedom of the Will and the Concept of a Person

https://www.jstor.org/stable/2024717
2•andsoitis•35m ago•0 comments

Proxima Kósmos, Vol. II: Fictions and Futures

https://berggruen.org/eu/library/proxima-kosmos-volume-2
2•andsoitis•36m ago•0 comments

Microsoft Systems Journal (2024)

https://jacobfilipp.com/msj-index/
1•elvis70•37m ago•0 comments

Trump vs. Carney at Davos: One summit, two visions

https://www.chathamhouse.org/2026/01/trump-vs-carney-davos-one-summit-two-visions-independent-thi...
3•rbanffy•38m ago•1 comments