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.

Ask HN: What dev tools do you rely on that nobody talks about?

1•crcsmnky•1m ago•0 comments

Ask HN: Freelancer? Seeking freelancer? (April 2026)

1•jon_north•2m ago•0 comments

Ask HN: Is AWS me-south-1 down or gone?

1•kaikeru•3m ago•0 comments

Grounded Discovery Labs – a system for engineering discovery

https://groundeddiscoverylabs.com/
1•Whyachi•4m ago•1 comments

Estonia to relaunch Skype as Europe's sovereign platform

https://estonianworld.com/technology/estonia-to-relaunch-skype-as-europes-sovereign-platform/
2•atlasunshrugged•4m ago•0 comments

Ban Ray – Your face is not inventory

https://banray.eu/en/index.html
1•ChrisArchitect•5m ago•0 comments

The smallest ELF executable (2021)

https://nathanotterness.com/2021/10/tiny_elf_modernized.html
1•michelangelo•5m ago•0 comments

Hex editor should color-code bytes

https://simonomi.dev/blog/color-code-your-bytes/
1•Teckla•6m ago•0 comments

How LLMs Got Good: Humility, Tools, and Reinforcement Learning

https://medium.com/@tudor.malene/why-predict-the-next-word-shouldnt-work-this-well-but-does-d0333...
1•tudorhn•7m ago•0 comments

Maggi is healthier than rice – once said a proud computer

https://isrl-research.github.io/
1•lalithaar•7m ago•0 comments

MidnightBSD 4.0.4 Released with Aged and Agectl for Age Verification/Attestation

https://www.phoronix.com/news/MidnightBSD-4.0.4
1•Bender•7m ago•0 comments

AI Panic and the Long History of Bad Predictions

https://www.keepgoingpod.com/p/the-end-is-probably-not-near
2•jdbiggs•7m ago•1 comments

Launch day has arrived for NASA's Artemis II mission–here's what to expect

https://arstechnica.com/space/2026/04/launch-day-has-arrived-for-nasas-artemis-ii-mission-heres-w...
3•Archelaos•8m ago•0 comments

April 1 Linux Patches: Verified Birth Date for File Creation Block Emacs Running

https://www.phoronix.com/news/April-1-Linux-Patches-2026
1•Bender•8m ago•0 comments

The Day AI Died – and Nobody Noticed

https://medium.com/@simbamudonzvo/the-day-ai-died-and-nobody-noticed-d5d2ec555d18
1•TechOnionKing•8m ago•0 comments

Twenty-One Years of Blogging

https://marcin.juszkiewicz.com.pl/2026/04/01/twenty-one-years-of-blogging/
2•speckx•8m ago•0 comments

Claude Code won April Fools Day this year

https://xeiaso.net/notes/2026/claude-code-wins-april-fools/
1•speckx•8m ago•0 comments

The Struggle Is What We Crave

https://dommagnifi.co/2026-04-01-the-struggle-is-what-we-crave/
1•magnificode•9m ago•0 comments

Why Are There So Many Dead Posts on Hacker News

https://kgolubic.com/posts/why-are-there-so-many-dead-posts-on-hacker-news/
1•kgolubic•11m ago•0 comments

Ralph Loop

https://stevekinney.com/writing/the-ralph-loop
1•gooseyGander•11m ago•0 comments

Crypto Firms Launch Pac to Influence US Midterms

https://catenaa.com/markets/cryptocurrencies/crypto-firms-launch-pac-to-influence-us-midterms/
1•malindasp•11m ago•1 comments

How to pick the right pricing model for your AI agent

https://flexprice.io/blog/how-to-pick-the-right-pricing-model-for-ai-agent
3•Hardcoded26•12m ago•0 comments

Blackstone, Ares, Rivals Grilled by Congress over Private Credit

https://www.bloomberg.com/news/articles/2026-04-01/blackstone-ares-rivals-grilled-by-congress-ove...
1•petethomas•12m ago•0 comments

What even is a correct compiler for gas-metered languages?

https://rubber-duck-typing.com/posts/2026-03-14-compiler-correctness-gas-challenge.html
1•sayon•13m ago•0 comments

Build a super simple, crypto-native paywall in a single weekend

https://mi-xin.vercel.app
1•verson•14m ago•0 comments

Photos Leak – no one is safe

https://we-have-it.pythonic.ninja/
2•PythonicNinja•15m ago•1 comments

Crypto needs the yield compromise more than banks do

https://www.americanbanker.com/news/crypto-needs-the-yield-compromise-more-than-banks-do
1•petethomas•15m ago•0 comments

Heirs to Bic Pen Fortune Seek Painting They Say Chauffeur Stole

https://www.bloomberg.com/news/articles/2026-04-01/heirs-to-bic-pen-fortune-seek-painting-they-sa...
1•jbegley•15m ago•0 comments

Pandoc: A Workhorse for Document Conversion

https://lwn.net/Articles/1064692/
1•leephillips•15m ago•0 comments

US patent office revokes Nintendo's patent on summoning characters to battle

https://www.videogameschronicle.com/news/us-patent-office-revokes-nintendos-patent-on-summoning-c...
1•thunderbong•15m ago•0 comments