frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•2h 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•1h 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.

Data version control over multiple heterogenous storage back ends

https://lakefs.io/blog/multiple-storage-backends-support/
1•edogrider•2m ago•0 comments

It's Far Too Soon to Let Supersonic Flights Rip Across the Country

https://www.jalopnik.com/1865990/supersonic-flights-trying-to-return-to-us/
1•rntn•2m ago•0 comments

The Disappearance of an Internet Domain (2024)

https://every.to/p/the-disappearance-of-an-internet-domain?repost=chagos-transferred-to-mauritius
1•zinekeller•2m ago•0 comments

14,000 babies at risk in Gaza

https://www.theguardian.com/commentisfree/2025/may/22/israel-gaza-aid-plan-displace-control-palestinians
1•jseip•2m ago•0 comments

Bear cub rescued from woods is being raised by humans dressed as bears

https://www.msn.com/en-us/news/us/bear-cub-rescued-from-woods-is-being-raised-by-humans-dressed-as-bears/ar-AA1Fh9eP
1•Stratoscope•3m ago•0 comments

Russian parliament passes bill to track migrants via mobile app

https://novayagazeta.eu/articles/2025/05/20/russian-parliament-passes-bill-to-track-migrants-via-mobile-app-en-news
1•gslin•5m ago•0 comments

That fractal that's been up on my wall for 12 years

https://chriskw.xyz/2025/05/21/Fractal/
4•chriskw•6m ago•0 comments

Toto: Time-Series-Optimized Transformer for Observability

https://github.com/DataDog/toto
1•tanelpoder•8m ago•1 comments

Clarke's Law (Part 2)

https://seths.blog/2025/05/clarkes-law-part-2/
1•herbertl•11m ago•0 comments

No exemption from one star reviews

https://alearningaday.blog/2025/05/22/no-exemption/
1•herbertl•12m ago•0 comments

I used to shoot $500k pharma commercials. I made this for $500 in Veo 3

https://twitter.com/PJaccetturo/status/1925464847900352590
4•enraged_camel•13m ago•0 comments

Write Like a Patent Litigator: Avoid Mistakes Made by Non-Patent Lawyers (2017)

https://repository.law.uic.edu/cgi/viewcontent.cgi?article=1426&context=ripl
1•Tomte•14m ago•0 comments

AI Succession [video] (2023)

https://www.youtube.com/watch?v=NgHFMolXs3U
1•droideqa•15m ago•0 comments

Write Down Your Password (2005)

https://www.schneier.com/blog/archives/2005/06/write_down_your.html
1•Tomte•15m ago•0 comments

Gccx transforms CPX (JSX like syntax) into asm-DOM Virtual DOM (C++)

https://github.com/mbasso/gccx
1•90s_dev•15m ago•0 comments

It's Not Just Data Centers

https://halcyon.io/blog/not-just-data-centers
1•brucefalck•16m ago•0 comments

MCP explained without hype or fluff

https://blog.nilenso.com/blog/2025/05/12/mcp-explained-without-hype-or-fluff/
10•captn3m0•17m ago•0 comments

Administrative court: Cookie banner must contain "Reject all" button

https://www.heise.de/en/news/Administrative-court-Cookie-banner-must-contain-Reject-all-button-10390520.html
5•nabla9•19m ago•2 comments

Why does Elon Musk love this socialist sci-fi series?

https://www.vox.com/culture/413502/iain-banks-culture-series-elon-musk-jeff-bezos-mark-zuckerberg
1•GeoAtreides•21m ago•1 comments

From confectioners to robots – Tor Alva in Mulegns is unveiled

https://ethz.ch/en/news-and-events/eth-news/news/2025/05/from-confectioners-to-robots-tor-alva-in-mulegns-is-unveiled.html
1•gnabgib•22m ago•0 comments

Ask HN: Founders – how are you automating support?

1•jesper______•23m ago•3 comments

Sweden bans paying for OnlyFans content

https://www.euractiv.com/section/tech/news/sweden-bans-paid-online-sexual-acts-in-law-targeting-platforms-like-onlyfans/
5•michalpleban•24m ago•6 comments

OpenAI's Ambitions Just Became Crystal Clear

https://www.theatlantic.com/technology/archive/2025/05/openai-io-jony-ive/682884/
1•toss1•25m ago•0 comments

Why Engineering Teams Should Build Their Own AI Coding Agents

https://qckfx.com/blog/why-engineering-teams-should-build-their-own-ai-coding-agents
1•chw9e•25m ago•0 comments

Near-infrared spatiotemporal color vision enabled by upconversion contact lenses

https://www.cell.com/cell/fulltext/S0092-8674(25)00454-4
4•ArnoVW•26m ago•1 comments

Xiaomi launches in-house XRing O1 chipset to enter high-end SoC arena

https://www.canalys.com/insights/xiaomi-in-house-o1
1•nimar•27m ago•0 comments

Metrics Are Easy–Impact Is Hard

https://eleganthack.com/metrics-are-easy-impact-is-hard/
1•adrianhoward•32m ago•0 comments

Launch HN: WorkDone (YC X25) – AI Audit of Medical Charts

13•digitaltzar•33m ago•2 comments

Accelerating Docker Builds by Halving EC2 Boot Time

https://depot.dev/blog/accelerating-builds-improve-ec2-boot-time
4•Telstrom90•33m ago•0 comments

Bayesian Cognition and the Future of Human-AI Interaction

https://learnbayesstats.com/episode/132-bayesian-cognition-and-the-future-of-human-ai-interaction-tom-griffiths
1•synthetictask•33m ago•0 comments