frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

WordPress Manifesto – 15 Years In, Here's What's Broken

https://marcindudek.dev/blog/wordpress-manifesto/
1•ValentineC•2m ago•0 comments

A Sociologist's Take on the Artemis Moon Round-Trip

https://discursiveanomalies.com/2026/04/15/a-sociologists-take-on-the-artemis-moon-round-trip/
1•panic•5m ago•0 comments

KeePassChi

https://codeberg.org/keepasschi
1•LeoPanthera•5m ago•0 comments

Viral victory: Iran is beating the land of tech bros in the social media wars

https://www.theguardian.com/world/2026/apr/15/iran-beating-us-social-media-wars
2•hebelehubele•6m ago•0 comments

Omega Constant

https://en.wikipedia.org/wiki/Omega_constant
1•cl3misch•8m ago•0 comments

Microsoft Raises Prices for All Surface PCs, Higher Than Mac Equivalents

https://www.macrumors.com/2026/04/14/microsoft-surface-pc-price-hike/
2•mgh2•11m ago•1 comments

Meeting Myself Again: Conversations in the Dark

https://www.patreon.com/posts/155646763
1•raynchad•11m ago•0 comments

Flinku – I built a Firebase Dynamic Links replacement after the shutdown

https://www.flinku.dev/
1•luky_dev•12m ago•0 comments

What if you didn't need to write back end code for your app?

https://linkedrecords.com/getting-started/
1•WolfOliver•15m ago•1 comments

Coupling (Computer Programming)

https://en.wikipedia.org/wiki/Coupling_(computer_programming)
1•downboots•18m ago•0 comments

NASA's Artemis Moon Mission Was a State Failure and a Human Triumph

https://www.bloomberg.com/opinion/features/2026-04-14/nasa-s-artemis-moon-mission-was-a-state-fai...
1•helsinkiandrew•20m ago•1 comments

Curriculum 68: Recommendations for academic programs in Computer Science

https://www.slideshare.net/slideshow/curriculum-68-recommendations-for-academic-programs-in-compu...
1•pramodbiligiri•21m ago•0 comments

Generative AI in 2016 [video]

https://www.youtube.com/watch?v=sfFTvtwLCaU
1•frag•23m ago•0 comments

High-temperature memristors enabled by interfacial engineering

https://www.science.org/doi/10.1126/science.aeb9934
2•joak•28m ago•1 comments

Amazon to Acquire Globalstar and Expand Amazon Leo Satellite Network

https://www.businesswire.com/news/home/20260414237496/en/Amazon-to-Acquire-Globalstar-and-Expand-...
11•homarp•28m ago•0 comments

Qwen OAuth Free tier will be discontinued on 2026-04-15

https://github.com/QwenLM/qwen-code/pull/3207
2•angst•30m ago•0 comments

Roy Fielding's Misappropriated REST Dissertation (2020)

https://twobithistory.org/2020/06/28/rest.html
1•locknitpicker•33m ago•0 comments

REST APIs must be hypertext-driven (2008)

https://roy.gbiv.com/untangled/2008/rest-apis-must-be-hypertext-driven
2•locknitpicker•35m ago•1 comments

The 'Annoyance Economy' Is More Than Just Annoying

https://www.nytimes.com/2026/04/12/business/annoyance-economy-costs.html
1•vinni2•36m ago•0 comments

Unidentified deep sea creature encountered off the coast of Japan

https://phys.org/news/2026-04-rich-biodiversity-japan-deepest-ocean.html
2•lfaw•36m ago•1 comments

Chris Taylor on AI (2005)

https://main.kanoogi.com/updates?updates=kanoogi_updates&topic=0013#blogstart
1•fho•37m ago•0 comments

ASML raises 2026 sales outlook to €36B–€40B

https://www.asml.com/en/news/press-releases/2026/q1-2026-financial-results
2•felipevb•41m ago•0 comments

Acid-Free Paper

https://en.wikipedia.org/wiki/Acid-free_paper
2•tosh•42m ago•0 comments

Burn Peak Health for Healthy Weight Management [pdf]

https://storage.prod.researchhub.com/uploads/papers/users/187827/aceef47a-9ba6-48db-b4f1-eebb0107...
1•Zacspencet•43m ago•0 comments

LazyVim for Ambitious Developers: Second Edition

https://www.patreon.com/posts/lazyvim-for-155631128
1•DavideNL•46m ago•0 comments

People are pretending to be AI chatbots – for fun

https://www.npr.org/2026/04/14/nx-s1-5776842/ai-chatbot-comedy-ben-palmer-chatgpt
5•geox•48m ago•0 comments

Show HN: My favorite local-feeling remotely accessible Claude Code setup

https://gist.github.com/Reebz/99db98ad4d3c45ebed84989a13710788
3•Reebz•51m ago•0 comments

Capture the Narrative: Wargaming Social Media Manipulation

https://capturethenarrative.com/
2•jruohonen•54m ago•0 comments

Popular Flutter GetX repo disappeared briefly

https://github.com/jonataslaw/getx
1•nativeforks•54m ago•0 comments

Make AI say your name correctly

https://hamanlp.org/research/names/
1•boodleboodle•55m ago•0 comments