frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: Word-by-word translation aligner tool

https://aligner.tinygods.dev/
1•danipolani•26s ago•0 comments

ChatGPT hallucinating images when asked to restore non existent photo

https://twitter.com/penguinweb3/status/2063196355011424582
1•blumomo•2m ago•0 comments

Ultra-High-Speed Cutting for High-Performance Difficult-to-Machine Composites

https://www.mdpi.com/2075-1702/14/5/468
1•PaulHoule•2m ago•0 comments

FreeBSD 15.1 Delayed to Mid-June Due to Critical x86 Bug Fixes

https://www.phoronix.com/news/FreeBSD_15.1-RC3-Released
2•daesorin•7m ago•0 comments

I don't like computer anymore. [video]

https://www.instagram.com/reel/DZGC6EChvGK/
1•mastazi•10m ago•0 comments

AI Criticism

https://dennisforbes.ca/blog/microblog/2026/06/on_ai_criticism/
1•llm_nerd•13m ago•0 comments

Show HN: DomainTasker – avoid losing domains and surprise renewals

https://domaintasker.com/
2•si_164•20m ago•0 comments

Armed Forces Recipe Service [pdf]

https://www.marines.mil/portals/1/Publications/MCO%20P10110.42B.pdf
1•kmstout•21m ago•1 comments

The Guix Nix Abomination: Leveraging Guix Derivations in Nix

https://fzakaria.com/2026/06/05/the-guix-nix-abomination-leveraging-guix-derivations-in-nix
2•dagenix•30m ago•0 comments

Show HN: Persist – an AI agent that follows up accrued channels till they reply

https://persist.chat
3•Robelk1•35m ago•1 comments

CS Trivia: computer science crosswords

https://cstrivia.com/
2•bluejulius•35m ago•1 comments

A modular impact diverting mechanism for football helmets [pdf]

https://www.sfu.ca/~gwa5/pdf/2020_04.pdf
2•luu•37m ago•0 comments

Is AI Anxiety Causing the Vibecession? An Investigation

https://kevinonthemargin.com/is-ai-anxiety-causing-the-vibecession-an-investigation/
2•m-hodges•38m ago•0 comments

Up-to-Date Evidence Reviews of Health and Longevity Interventions

https://evipedia.ai/
2•negura•44m ago•0 comments

Public Domain Image Archive

https://pdimagearchive.org/
5•davidbarker•47m ago•0 comments

Immortality

https://aabiji.github.io/html/immortality.html
4•aabiji•52m ago•0 comments

The World's 100 Best Clubs

https://www.nightlifeinternational.org/en/congress-awards/100-world-s-best-clubs
3•fauria•56m ago•0 comments

California's tire efficiency rule may increase emissions by shortening lifespan

https://www.autoblog.com/news/californias-fuel-saving-tire-rule-could-make-drivers-replace-tires-...
3•mcchen51•59m ago•0 comments

AI is the greatest money-wasting scheme humanity has ever invented

https://www.telegraph.co.uk/news/2026/06/04/ai-is-the-greatest-money-wasting-scheme-humanity-has-...
4•HotGarbage•1h ago•0 comments

Watch Einstein's simulated brain light up as you ask him questions

https://www.opendria.com/
2•opendria•1h ago•2 comments

Code Is Cheap(er)

https://htmx.org/essays/code-is-cheap/
3•chr15m•1h ago•0 comments

Strain-resilient intrinsically stretchable electrochemical biointerfaces

https://www.science.org/doi/10.1126/science.aed1630
2•OneManHorde•1h ago•1 comments

BunsenLabs Linux Carbon

https://www.bunsenlabs.org/
2•doener•1h ago•0 comments

Human-Like Neural Nets by Catapulting

https://gwern.net/llm-catapult
4•telotortium•1h ago•0 comments

Gnome File Previewer Switches to GTK4, Adds Dark Mode

https://www.phoronix.com/news/GNOME-File-Previewer-GTK4
3•daesorin•1h ago•0 comments

Cursor cuts prices, adds enterprise spend controls amid "tokenomics" reckoning

https://thenewstack.io/cursor-pricing-token-billing/
4•AnhTho_FR•1h ago•0 comments

This 71-year-old jockey has saddled up in more than 53,000 races

https://www.cnn.com/2026/06/05/sport/perry-ouzts-jockey
2•breve•1h ago•0 comments

Ask HN: How do you manage distribution after building?

2•lucasacosta_•1h ago•0 comments

DoD Officially Drops 180 Faiths from Military's Recognized Religion List

https://www.military.com/dod-officially-drops-180-faiths-from-militarys-recognized-religion-list
32•Balgair•1h ago•28 comments

Telegram's Plan to Break the Apple-Google-Meta Monopoly Runs Through Wall Street

https://www.disruptionbanking.com/2026/06/05/telegrams-kremlin-approved-plan-to-break-the-apple-g...
3•bmcdresson•1h ago•1 comments