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.

US attorney opens investigation into California elections-sends prosecutor to LA

https://apnews.com/article/california-primary-ballot-counting-trump-investigation-22b06b32abdca1e...
1•petethomas•5m ago•0 comments

The smart TV in your living room is a node in the AI scraping economy

https://blog.includesecurity.com/2026/06/the-smart-tv-in-your-livingroom-is-a-node-in-the-aiscrap...
1•themaxdavitt•6m ago•0 comments

Exploiting ML-DSA bugs [pdf]

https://cr.yp.to/papers/mldsa-20260601.pdf
1•libroot•6m ago•1 comments

Show HN: Documenting an Obscure Japanese Wii Game – and-Kensaku

https://github.com/TylerJaacks/AndKensakuResearch
1•TylerJaacks•8m ago•0 comments

New Treatment for Alzheimer's and Parkinson's Discovered in Japan

https://www.inc.com/lucia-auerbach/future-of-brain-health-how-a-new-scientific-discovery-could-re...
2•nikolay•14m ago•0 comments

Misu

https://en.wikipedia.org/wiki/Misu
2•carabiner•19m ago•0 comments

eLoran

https://en.wikipedia.org/wiki/ELoran
2•jonbaer•22m ago•0 comments

Ultra-fast CSV parsing and encoding for Elixir

https://github.com/jeffhuen/RustyCSV
1•sntran•23m ago•1 comments

The intracies of modern camera lens repair (2024)

https://salvagedcircuitry.com/sigma-45mm.html
7•transistor-man•23m ago•0 comments

Re: Cache: 0-click SXSS on Next.js via reflected headers

https://zhero-web-sec.github.io/research-and-things/re-cache-excessive-reflection-type-confusion-...
1•logickkk1•25m ago•0 comments

Cumulative average BMI and cognitive decline: a 24-year cohort study

https://link.springer.com/article/10.1007/s00415-026-13696-2
3•PaulHoule•27m ago•0 comments

I built an email agent to triage bogus security reports

https://opencomputer.dev/blog/email-security-triage-agent/
1•iacguy•32m ago•0 comments

Why Do Asian Brands Pretend to Be Japanese?

https://www.thechow.net/p/asian-brands-pretending-japanese-miniso
2•herbertl•32m ago•0 comments

Game Theory Text - Thomas Ferguson

https://web.archive.org/web/20050301121109/http://www.math.ucla.edu/~tom/Game_Theory/Contents.html
2•soupspaces•32m ago•0 comments

The Sandbaggers (1978 – 80) Complete Series

https://archive.org/details/the-sandbaggers-1978-80
3•petethomas•34m ago•1 comments

She won a religious exemption from using AI at work

https://www.businessinsider.com/worker-got-religious-exemption-using-ai-at-work-2026-6
9•dgellow•35m ago•3 comments

Silent Ransom Group Impersonating IT Personnel Through Social Engineering [pdf]

https://www.ic3.gov/CSA/2026/260526.pdf
2•gnabgib•39m ago•0 comments

ToTra – open-source LLM gateway with GDPR/EU AI Act compliance

https://github.com/SugaC-275/ToTra
2•SugaC275•40m ago•0 comments

The Shift in Peering Threatening the Internet's Foundations

https://www.internetsociety.org/blog/2026/06/the-shift-in-peering-threatening-the-internets-found...
3•8organicbits•47m ago•0 comments

Trump Urges 'Less Shackled' Pulte to Fire Intelligence-Community Employees

https://www.wsj.com/politics/national-security/trump-urges-less-shackled-pulte-to-fire-intelligen...
2•petethomas•50m ago•0 comments

If you don't fall for these extortionists' calls they'll show up with USB sticks

https://www.theregister.com/cyber-crime/2026/06/05/if-you-dont-fall-for-these-extortionists-calls...
2•Bender•50m ago•0 comments

Small modular nuclear reactor reaches criticality in first test

https://arstechnica.com/science/2026/06/first-us-test-of-modular-reactor-reaches-criticality/
1•Bender•51m ago•0 comments

Spinal cord stimulation for upper limb motor function in people with hemiparesis

https://www.nature.com/articles/s41591-026-04435-1
4•bookofjoe•51m ago•0 comments

Baby botulism outbreak: FDA still doesn't know cause or how to prevent it

https://arstechnica.com/health/2026/06/baby-botulism-outbreak-fda-still-doesnt-know-cause-or-how-...
4•Bender•52m ago•0 comments

Nasdaq falls 4% and suffers worst day since April 2025 traders flee chip stocks

https://www.cnbc.com/2026/06/04/stock-market-today-live-updates.html
6•rawgabbit•54m ago•2 comments

We Ditched Postgres for ClickHouse to Process 12B Caches per Day

https://momentic.ai/blog/postgres-to-clickhouse-migration
6•wuweiweiwu•57m ago•0 comments

You shouldn’t Use SQLite

https://www.hendrik-erz.de/post/why-you-shouldnt-use-sqlite
2•andrewstuart•57m ago•5 comments

GrapheneOS user reported to authorities for using GrapheneOS

https://discuss.grapheneos.org/d/36134-grapheneos-user-reported-to-authorities-for-using-grapheneos
6•Cider9986•58m ago•0 comments

Echoes from Another Place

https://scholarlyfutures.substack.com/p/echoes-from-another-place
2•JohnHammersley•59m ago•0 comments

Omnipod 5 is coming to Loop and Trio this summer

https://nightscout.github.io/omnipod-five/
3•Group_B•1h ago•0 comments