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.

We should be talking about zombie reasoning

https://thepursuitofliberalism.substack.com/p/why-we-should-be-talking-about-zombie
1•paulpauper•11s ago•0 comments

Sustainable thermal paper formulation using lignocellulosic biomass fractions

https://www.science.org/doi/10.1126/sciadv.adw9912
1•westurner•1m ago•0 comments

Why is OpenAI so stingy with ChatGPT web search?

https://justin.searls.co/shots/2026-02-01-11h13m37s/
2•mefengl•3m ago•0 comments

Show HN: A simple Rust daemon that monitors URLs and triggers local scripts

https://github.com/skorotkiewicz/upi
1•modinfo•3m ago•0 comments

AGI, ASI, A*I – Do we have all we need to get there?

https://www.johndcook.com/blog/2026/01/30/agi-asi-ai-do-we-have-all-we-need-to-get-there/
1•ibobev•4m ago•0 comments

Once Thought to Support Neurons, Astrocytes Turn Out to Be in Charge

https://www.quantamagazine.org/once-thought-to-support-neurons-astrocytes-turn-out-to-be-in-charg...
2•ibobev•4m ago•0 comments

LLMs Keep on Getting Better

https://probablydance.com/2026/01/31/how-llms-keep-on-getting-better/
1•ibobev•5m ago•0 comments

Light exposure and aspects of cognitive function in everyday life

https://www.nature.com/articles/s44271-025-00373-9
2•PaulHoule•5m ago•0 comments

FOSDEM: Open-Source Is Political – Resist Organize Protect

https://ftp.belnet.be/mirror/FOSDEM/video/2026/janson/SFKNTZ-welcome_to_fosdem_2026.mp4
1•obeleh•7m ago•0 comments

Slurping the Claude Code Word Soup

https://indiantinker.bearblog.dev/slurping-the-claude-code-word-soup/
1•indiantinker•7m ago•0 comments

Show HN: WikiReplay – a time‑lapse diff viewer for Wikipedia

https://wikireplay.com/
1•blurayfin•7m ago•0 comments

Recreating the Chernobyl Reactor 4 control panel

https://www.youtube.com/watch?v=DDlrnJIbkds
1•nar001•7m ago•1 comments

China's genius plan to win the AI race is paying off

https://www.ft.com/content/68f60392-88bf-419c-96c7-c3d580ec9d97
1•fmihaila•8m ago•0 comments

Rosebud: Create Games with AI

https://rosebud.ai/
1•doener•11m ago•0 comments

AI-induced cultural stagnation is no longer speculation − it's happening

https://theconversation.com/ai-induced-cultural-stagnation-is-no-longer-speculation-its-already-h...
3•smartmic•12m ago•0 comments

Is AI "good" yet? – tracking Hacker News sentiment on AI coding

https://www.is-ai-good-yet.com
2•ilyaizen•14m ago•0 comments

Internet world despairs as non-profit .org sold for $$$$ to private equity firm

https://www.theregister.com/2019/11/20/org_registry_sale_shambles/
2•fanf2•14m ago•0 comments

L4Ka: Pistachio Microkernel

https://www.l4ka.org/65.php
2•doener•15m ago•0 comments

AI agent discovers security flaw in OpenClaw, other agents discuss how to fix it

https://www.moltbook.com/post/cbd6474f-8478-4894-95f1-7b104a73bcd5
2•DustinEchoes•16m ago•0 comments

Moltbook's Limitations Beyond the Viral Hype

https://gilpignol.substack.com/p/moltbooks-limitations-beyond-the
2•light_triad•17m ago•0 comments

The L4Re Operating System Framework

https://l4re.org/
3•doener•17m ago•0 comments

Ian's Shoelace Site

https://www.fieggen.com/shoelace/
2•righthand•18m ago•1 comments

Former Google Engineer Found Guilty of Economic Espionage,Theft of AI Technology

https://www.justice.gov/opa/pr/former-google-engineer-found-guilty-economic-espionage-and-theft-c...
3•737min•19m ago•0 comments

Show HN: I made a bookmarks assistant for people who save lots of links and tabs

https://apps.apple.com/us/app/eyeball-bookmarks-assistant/id6670705634
1•quinto_quarto•20m ago•0 comments

The King and the Wizard

https://campedersen.com/eigenpute
1•ecto•21m ago•0 comments

Microsoft's $381B Rout Exposes Dark Side of the AI Binge

https://www.bloomberg.com/news/articles/2026-02-01/microsoft-s-381-billion-rout-exposes-dark-side...
4•zerosizedweasle•23m ago•0 comments

Imaging the Wigner crystal state in a new type of quantum material

https://phys.org/news/2026-01-imaging-wigner-crystal-state-quantum.html
1•rbanffy•26m ago•0 comments

Why Airbnb Never Became a Trillion-Dollar Company

https://gilpignol.substack.com/p/why-airbnb-never-became-a-trillion
1•light_triad•28m ago•0 comments

Crux Ansata

https://gutenberg.net.au/ebooks13/1303501h.html
1•joebig•28m ago•0 comments

Show HN: Subtitle Finder – Find perfectly synced subtitles for your video files

https://subtitlefinder.com
1•lord5et•28m ago•0 comments