frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Progress on Starbase Pads Ahead of Block 3 Starships

https://www.nasaspaceflight.com/2026/03/progress-starbase-pads-block-3-starships/
1•JumpCrisscross•6m ago•0 comments

When the bill comes due: the economics of AI coding tools

https://daniakash.com/blog/when-the-bill-comes-due
1•DaniAkash•9m ago•0 comments

Exceptional fake SSD clone of Samsung 990 Pro is almost impossible to spot

https://www.tomshardware.com/pc-components/ssds/exceptional-fake-ssd-clone-of-samsung-990-pro-is-...
3•speckx•18m ago•1 comments

Kemforge, a Quantum-Safe curl alternative to test ML-KEM

https://github.com/ConnectingApps/kemforge
1•DaanAcohen•18m ago•0 comments

Show HN: a Rust CLI to automatically swap monitor focus based on your gaze

https://github.com/pranavkarthik10/miru
3•treexs•21m ago•1 comments

Why Semi-Autonomous, Not Autonomous?

https://reikon.io/blog/semi-autonomous/
1•Monotoko•23m ago•1 comments

Don't Trust, Verify

https://daniel.haxx.se/blog/2026/03/26/dont-trust-verify/
2•lwhsiao•26m ago•0 comments

From latent spaces to JWTs: how agents taught me backend

https://blog.cobanov.dev/blog/agents
2•cobanov•28m ago•0 comments

When Brands Wear an Insult as a Badge of Honor

https://sloanreview.mit.edu/article/when-brands-wear-an-insult-as-a-badge-of-honor/
2•gnabgib•30m ago•0 comments

Lifetime subscriptions don't mean what you think they mean

https://productimpossible.com/review/lifetime-subscriptions-trap/
4•lest•35m ago•1 comments

Camden, N.J., Cut Its Murder Rate to a 40-Year Low

https://www.nytimes.com/interactive/2026/03/27/headway/camden-nj-murder-rate-gun-violence.html
1•JumpCrisscross•36m ago•0 comments

Gaza toddler released from Israeli custody with 'cigarette burn' wounds

https://web.archive.org/web/20260000000000*/https://www.middleeasteye.net/news/gaza-toddler-relea...
15•Imustaskforhelp•39m ago•10 comments

Off Grid Fteepee

https://klumpen.eu/
1•worik•40m ago•0 comments

Rivian Made Car Dealers Back Down in Washington. More States May Be Next

https://www.wsj.com/business/autos/rivian-made-car-dealers-back-down-in-washington-more-states-ma...
1•JumpCrisscross•41m ago•0 comments

Please Stop Talking about "Zoomers" and "Gen Alpha"

https://www.richardhanania.com/p/please-stop-talking-about-zoomers
2•paulpauper•43m ago•0 comments

Did Paul Conyngham use AI to develop a cancer treatment for his dog?

https://hedonicescalator.substack.com/p/did-paul-conyngham-really-use-ai
1•paulpauper•44m ago•0 comments

Sylve – Proxmox Alternative by FreeBSD

https://sylve.io/
2•imbobbytables•46m ago•0 comments

The Revenge of the Data Scientist

https://hamel.dev/blog/posts/revenge/
1•hamelsmu•47m ago•0 comments

Nothing new to see here

https://feld.com/archives/2026/03/nothing-new-to-see-here/
7•guiambros•52m ago•3 comments

The Nap Room Didn't Love Me Back

https://www.thenation.com/article/society/the-nap-room-didnt-love-me-back/
2•mitchbob•53m ago•1 comments

Show HN: testing Ansible playbooks *fast*

https://github.com/lucasluitjes/ansible-lxd-boilerplate
2•lucasluitjes•54m ago•0 comments

Make Space for Every Voice: Reflect, Headline, Discussion

https://pointc.co/make-space-for-every-voice/
1•walterbell•57m ago•0 comments

A silly little trivia game I made

https://veritas.axiomatik.net/
1•stealth_dev•58m ago•0 comments

The American Soldier [pdf]

https://gwern.net/doc/psychology/1949-stouffer-theamericansoldier-v1-adjustmentduringarmylife.pdf
1•hbarka•59m ago•0 comments

Hardware Image Compression

https://www.ludicon.com/castano/blog/2026/03/hardware-image-compression/
1•luu•1h ago•0 comments

Humans Are Amazing

https://v5.chriskrycho.com/journal/humans-are-amazing/
3•walterbell•1h ago•0 comments

Usability – Catch common usability problems before user testing

https://www.userium.com/
1•calmnordic•1h ago•0 comments

Anthropic's 'Claude Mythos' Leak

https://www.coindesk.com/markets/2026/03/27/anthropic-s-massive-claude-mythos-leak-reveals-a-new-...
2•geoffbp•1h ago•0 comments

Basic Physics Engine in about 100 lines of pure JavaScript

https://slicker.me/javascript/physics/physics_engine.htm
3•thunderbong•1h ago•1 comments

Show HN: Canary – Monitor any URL for changes, get AI-powered intelligence

2•iambel0ved•1h ago•1 comments