frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•6mo 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•6mo 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•6mo 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: Tiptap AI Toolkit – Now exploring server-side agentic document editing

2•philipisik•1m ago•0 comments

Cyber Warfare and Its Limits: A Response to Soesanto and Gajos

https://www.lawfaremedia.org/article/cyber-warfare-and-its-limits--a-response-to-soesanto-and-gajos
1•hn_acker•3m ago•0 comments

Ask HN: What did you force yourself to do and now you're glad you did?

1•atentaten•3m ago•0 comments

Within the Chrysalis of Prime Intellect

https://www.yacinemahdid.com/p/within-the-chrysalis-of-prime-intellect
1•research_pie•3m ago•0 comments

Why is web becoming soulless?

https://www.jaydip.me/blog/typography-in-the-wild
1•jdsane•4m ago•1 comments

The Bernoulli Manifesto

http://luschny.de/math/zeta/The-Bernoulli-Manifesto.html
1•speckx•4m ago•0 comments

The Penicillin Myth

https://www.asimov.press/p/penicillin-myth
2•mailyk•6m ago•0 comments

Will AI require the same kind of socialized insurance as nuclear energy?

https://www.unite.ai/will-ai-require-the-same-kind-of-socialized-insurance-as-nuclear-energy/
1•50kIters•8m ago•0 comments

Tell HN: Declaration of Independence is 100% AI, according to AI Checker

2•whatamidoingyo•10m ago•0 comments

Open Source Has Too Many Parasocial Relationships

https://pivotnine.com/blog/open-source-has-too-many-parasocial-relationships/
1•zdw•11m ago•0 comments

What Is Toon?

https://jsonmaster.com/what-is-toon
1•rkmahale•13m ago•0 comments

Animation Hasn't Been the Same Since 'Toy Story'

https://www.nytimes.com/2025/11/21/movies/toy-story-anniversary-animation.html
2•thm•13m ago•0 comments

Scientists Just Discovered How Rabies Hijacks Human Cells

https://scitechdaily.com/scientists-just-discovered-how-rabies-hijacks-human-cells/
2•thunderbong•14m ago•0 comments

Show HN: Contrarian Signals – market sentiment indicators

https://contrariansignals.com/
1•victordg•14m ago•0 comments

Valve makes almost $50M per employee

https://twitter.com/cantworkitout/status/1989003859700662727
1•HelloUsername•15m ago•0 comments

A Stand Against Coal Could Push Oakland Toward Bankruptcy

https://www.nytimes.com/2025/11/24/us/oakland-coal-port-budget-bankruptcy.html
1•mitchbob•16m ago•1 comments

Fruits and Decibels: Why your best features are hard to sell

https://quietmoats.substack.com/p/fruits-and-decibels
1•okossi•16m ago•0 comments

Show HN: Creating Multi-Page Manga with Google's Nano Banana Pro

https://github.com/Yutarop/comic-generator
2•ponta17•17m ago•0 comments

Cooking in Maximum Security

https://pluralistic.net/2025/11/24/moca-moka/
2•hn_acker•17m ago•0 comments

Automating Updates to a Digital Vigil

https://ntietz.com/blog/automating-updates-to-a-digital-vigil/
1•todsacerdoti•17m ago•0 comments

Comments on "Glauert's optimum rotor disk revisited"

https://wes.copernicus.org/preprints/wes-2025-105/
1•bouchard•17m ago•1 comments

Container Security: A Developer Guide

https://developer.okta.com/blog/2019/07/18/container-security-a-developer-guide
1•fanf2•18m ago•0 comments

Yaoundé set to sizzle at 38° by 2030: green design could cool Cameroon's capital

https://theconversation.com/yaounde-is-set-to-sizzle-at-38-c-by-2030-how-green-design-could-cool-...
2•PaulHoule•18m ago•0 comments

Why Judging APIs by Syntax Is Misleading You

https://jjenzz.com/judging-apis-by-syntax-is-misleading/
1•speckx•18m ago•0 comments

How Trump is trying to remake American culture

https://www.semafor.com/article/11/23/2025/how-trump-is-trying-to-remake-american-culture-startin...
5•HelloUsername•19m ago•0 comments

Turns out Windows has a package manager

https://rosipov.com/blog/turns-out-windows-has-a-package-manager/
1•8organicbits•19m ago•0 comments

Show HN: Call Anybody from the Browser

https://comza.org
1•anwarlaksir•19m ago•0 comments

Show HN: Wikijumps – Browse Wikipedia visually using well-traveled connections

https://wikijumps.com
2•whb101•20m ago•0 comments

Supply and Demand Are Broken in Programming Education

https://blog.boot.dev/jobs/supply-demand-broken-programming-education/
9•wagslane•21m ago•0 comments

Demystifying Determinism in Durable Execution

https://jack-vanlightly.com/blog/2025/11/24/demystifying-determinism-in-durable-execution
1•rmoff•22m ago•0 comments