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.

JRR Tolkien's Letter to the Nazis

https://twitter.com/UtterlyInterest/status/2095095032684351852
1•bushwart•40s ago•0 comments

Show HN: Amaze Kings – a browser logic puzzle with 1,400 hand-graded levels

https://kings.amaze-apps.com/
1•amaze-apps-com•5m ago•0 comments

CircleK EMobility Journey Towards Effective Cross-Functional Value Streams

https://www.youtube.com/watch?v=LfzxyRkoO1s
1•aoma_conference•7m ago•0 comments

LWR Is the New Aura: A Pentester's Guide to Salesforce's WebRuntime API

https://www.reco.ai/blog/lwr-salesforce-webruntime-api-pentesting
1•daretograsp•11m ago•0 comments

RubyGems Supply Chain Attack: How Mend Defender Caught Coordinated Flood

https://www.mend.io/blog/inside-the-rubygems-supply-chain-attack/
1•ksec•11m ago•0 comments

How the Tobacco Industry Drove the Rise of Ultra-Processed Foods

https://vcresearch.berkeley.edu/news/how-tobacco-industry-drove-rise-ultra-processed-foods
2•paimapi•11m ago•0 comments

Pushing the quality-cost frontier with MAI-Image-2.6

https://microsoft.ai/news/pushing-the-quality-cost-frontier-with-mai-image-2-6/
2•doomroot13•12m ago•1 comments

Wikimedia Foundation Workers Overwhelmingly Vote to Form Union with CWA

https://wikiworkersunited.org/announcements/2026-09-04-us-wikimedia-foundation-workers-overwhelmi...
1•robin_reala•12m ago•0 comments

Biologist Andrew Newhouse on the genetic revival of the American chestnut

https://www.npr.org/2026/09/05/nx-s1-5954830/biologist-andrew-newhouse-on-the-genetic-revival-of-...
1•Brajeshwar•13m ago•0 comments

Gemini 4 Leak. Google is back

https://twitter.com/ravikiran_dev7/status/2095869859666268606
1•ltononro•15m ago•1 comments

Show HN: HyperCard to HTML Converter

https://github.com/stachon/hc2html
1•druidcz•15m ago•0 comments

Identifying cloud files with DROID

https://www.nationalarchives.gov.uk/blogs/digital/identifying-cloud-files-with-droid/
1•contact9879•16m ago•0 comments

LanguageTool browser extension is going Premium-only

https://languagetool.org/webextension/premium-announcement
1•pentagrama•17m ago•0 comments

Send a Stranger Love

https://sendastranger.love
1•kilroy123•17m ago•0 comments

The Biggest Obstacle to New Cancer Cures

https://www.nytimes.com/2026/09/04/opinion/clinical-trials-drugs-science.html
1•paulpauper•18m ago•0 comments

Praesidias – pre-execution authorization for AI agents

https://github.com/sowainat/praesidias-pabe01
1•sowainat1•19m ago•0 comments

War on Earth could spill over to the moon, Chinese space scientists warn

https://www.scmp.com/news/china/science/article/3366170/war-earth-could-spill-over-moon-chinese-s...
1•bushwart•19m ago•0 comments

Who Was the Most Consequential Emperor in Chinese History?

https://yashenghuang.substack.com/p/who-was-the-most-consequential-emperor
1•paulpauper•19m ago•0 comments

Prompt Humans Not LLMs

https://write.as/cswaezc1bwdjd
2•Gecko4072•20m ago•0 comments

Mein Ingeborg Bachmann Studium

https://marginalrevolution.com/marginalrevolution/2026/09/mein-ingeborg-bachmann-studium.html
1•paulpauper•21m ago•0 comments

FixMonorepo

https://github.com/twigg-vc/fixmonorepo
1•carlos-menezes•22m ago•0 comments

Lets simplify releases using saga, locks, Git, graphs, and shells

https://dispat.dev/
1•yohimik•22m ago•0 comments

R.F. Kuang Asks What It Takes to Be Chinese Enough

https://www.bloomberg.com/news/articles/2026-09-04/r-f-kuang-s-new-novel-taipei-story-explores-ch...
1•theanonymousone•23m ago•0 comments

Is Your Son a Computer Hacker? (2001)

https://www.inadequacy.org/public/stories/2001.12.2.42056.2147.html
1•grubbs•26m ago•0 comments

Kai-Fu Lee: China Will Win the AI Race for Reach

https://www.bloomberg.com/features/2026-kai-fu-lee-weekend-interview/
1•theanonymousone•26m ago•0 comments

ChatGPT bans campaigns from using AI to make ads. They're doing it anyway.

https://www.washingtonpost.com/politics/2026/09/05/chatgpt-bans-campaigns-using-ai-make-ads-theyr...
4•bookofjoe•30m ago•1 comments

Colonialism for clicks: influencers trying to film most isolated tribes

https://www.theguardian.com/world/2026/sep/05/colonialism-for-clicks-influencers-attempting-meet-...
2•theanonymousone•30m ago•0 comments

How Much CO₂ Builds Up in N95 Masks? – A Study [pdf]

http://pi.kylebenzle.com/co2.pdf
1•KyleBenzleKyle•32m ago•0 comments

Tesla Cybercab Tech Details – Initial Findings (Out of Spec Reviews)

https://www.youtube.com/watch?v=VI9OjUufpP0
1•spikels•32m ago•0 comments

121820

https://121820.xyz
1•ankitank•32m ago•0 comments