frontpage.
newsnewestaskshowjobs

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.

Meta's New AI Photo Tool Feature Removed

https://gizmodo.com/the-public-got-so-mad-at-metas-new-ai-photo-tool-that-its-scrapped-already-20...
1•etothet•1m ago•0 comments

Do LLMs make code a "materialized view"?

https://www.natemeyvis.com/do-llms-make-code-a-materialized-view/
1•Brajeshwar•4m ago•0 comments

Learning from Brazil and India's Instant Payment Systems [pdf]

https://www.hks.harvard.edu/sites/default/files/centers/mrcbg/Final_AWP_254.pdf
1•alephnerd•7m ago•0 comments

NonStop

https://en.wikipedia.org/wiki/Nonstop
1•skibz•12m ago•0 comments

Who Does Anubis Stop?

https://fzakaria.com/2026/07/09/who-does-anubis-actually-stop
2•supriyo-biswas•15m ago•0 comments

GhosttyFX – JavaFX terminal powered by libghostty

https://github.com/vlaaad/ghosttyfx/
1•vlaaad•16m ago•0 comments

Red Rising Society System

https://red-rising-solar-cartograph.jetrased.chatgpt.site/?era=rising
1•jetrased•18m ago•0 comments

Distro Fighter Game – Linux Distro and Desktop Brawler

https://distrofighter.com/
2•ashitlerferad•18m ago•0 comments

Era: Live Wallpapers for Your Mac

https://mo.software/era
1•megootronic•19m ago•0 comments

India's TCS plans up to 8,900 AI deployment engineers, seeks AI acquisitions

https://www.channelnewsasia.com/business/indias-tata-consultancy-services-plans-up-8900-ai-deploy...
2•rustoo•21m ago•1 comments

Ask HN: Resources for non-web software architecture?

2•xqb64•25m ago•0 comments

Discovering the Hermit in the Garden (2013)

https://blog.oup.com/2013/04/discovering-hermit-in-garden/
1•EndXA•26m ago•0 comments

Nobel-Winning U.S. Chemist Will Move to China to Lead A.I. Institute

https://www.nytimes.com/2026/07/09/science/nobel-winning-us-chemist-will-move-to-china-to-lead-ai...
3•brandonb•27m ago•0 comments

Experimental drug reverses fatty liver disease by repairing the gut

https://www.sciencedaily.com/releases/2026/07/260711010116.htm
2•brandonb•28m ago•0 comments

Comics from March 1981 Issue of Computers and Automation Magazine

https://computeradsfromthepast.substack.com/p/comics-from-march-1981-issue-of-computers
3•rbanffy•31m ago•0 comments

Atari ST Advertising – By Paul Lefebvre

https://www.goto10retro.com/p/atari-st-advertising
3•rbanffy•32m ago•0 comments

SlimeBallBench · AI models play slime soccer

https://slimeballbench.com/
1•erikwiffin•34m ago•0 comments

China builds full-scale US warship replica in desert

https://www.cnn.com/2026/07/11/world/video/china-builds-full-scale-us-warship-replica-in-desert-w...
3•amelius•35m ago•0 comments

5Gi: India's own 5G technology [pdf]

https://ntiprit.gov.in/pdf/gsm2g/All_about_Indias_own_5G_called_5Gi.pdf
1•teleforce•35m ago•0 comments

Where to find more about low tech?

https://damaged.bleu255.com/Low-Tech/
1•tukunjil•39m ago•0 comments

2026-06-02 ITHS Analog Conference – Lab Tour by Bernd Ulmann – Videos and Movies

https://vimeo.com/1204840172
1•rbanffy•39m ago•0 comments

Show HN: LocalMask – local-first CLI that masks PII and secrets before LLMs

https://localmaskpro.com
2•sgury•39m ago•0 comments

The British Museum

https://www.britishmuseum.org/
1•barrister•42m ago•0 comments

The Taste Essay

https://contraptions.venkateshrao.com/p/the-taste-essay
1•jger15•43m ago•0 comments

Ollama vs. Llama.cpp – Quick Benchmark

https://blawg.pages.dev/ollama-vs-llama-server
1•caveman23•48m ago•0 comments

Microsoft adjusts climate agenda as emissions leap

https://trellis.net/article/microsoft-adjusts-climate-agenda-as-emissions-leap/
2•cpach•49m ago•0 comments

The impressive AI demo is dead. Here's what actually reaches production

https://thenewstack.io/ai-production-data-problem/
1•Brajeshwar•51m ago•0 comments

Mystery behind Moana: After 1,700 years, why did Polynesians suddenly sail east?

https://theconversation.com/the-real-mystery-behind-moana-after-1-700-years-why-did-polynesians-s...
2•pseudolus•53m ago•0 comments

People keep sneaking into an empty IBM campus. This town has had enough

https://www.msn.com/en-us/news/crime/people-keep-sneaking-into-an-empty-ibm-campus-this-town-has-...
1•smurda•55m ago•1 comments

Anthropic found a hidden space where Claude puzzles over concepts

https://www.technologyreview.com/2026/07/09/1140293/anthropic-found-a-hidden-space-where-claude-p...
2•pseudolus•57m ago•0 comments