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.

New Rocket League is using Unreal Engine 6

https://twitter.com/QNDZYcom/status/2058586983656726640
2•astlouis44•1m ago•0 comments

Can't they get the small science stuff right?

https://www.theguardian.com/commentisfree/2026/may/24/hill-i-will-die-on-hollywood-blockbusters-s...
2•pjbk•3m ago•0 comments

What Gets Kept

https://www.newyorker.com/culture/the-weekend-essay/what-jack-kerouac-left-behind
2•lermontov•4m ago•0 comments

Oss.zone, a Pubnix Running on NixOS

https://oss.zone
1•f1nniboy•5m ago•1 comments

Apple Preparing New 'Gen AI' Website Ahead of WWDC

https://www.macrumors.com/2026/05/23/apple-gen-ai-subdomain/
1•Brajeshwar•5m ago•0 comments

FieldStation42 – Turn your computer into a vintage TV cable system

https://fieldstation42.com/
2•indigodaddy•9m ago•0 comments

Beating C with Dyalog APL: wc

https://ummaycoc.github.io/wc.apl/
1•tosh•10m ago•0 comments

Dynamical Governed Topology Engine with 3D Viewer

https://pub-6f95fcb400b34d7290952c160671872b.r2.dev/Screenshot%202026-05-24%20093053.png
1•verhash•12m ago•1 comments

FreeBSD Foundation Executive Director Tries Daily Driving FreeBSD on Laptop

https://www.phoronix.com/news/FreeBSD-On-Laptop-Driver
3•Bender•14m ago•0 comments

First ever Cray T3D Supercomputer goes up for auction with $81,000 reserve

https://www.tomshardware.com/tech-industry/supercomputers/first-ever-cray-t3d-supercomputer-goes-...
3•LorenDB•14m ago•0 comments

AT&T sues to ditch Cali copper phone lines to save billions

https://www.theregister.com/networks/2026/05/22/att-sues-to-ditch-cali-copper-phone-lines-to-save...
1•Bender•14m ago•0 comments

HP investigating BIOS updates that leave premium laptop users in boot loop limbo

https://www.theregister.com/personal-tech/2026/05/24/hp-investigating-bios-updates-that-leave-pre...
1•Bender•15m ago•0 comments

Hello TinyTree

https://tinytree.dev/blog/hello-tinytree/
2•oftenwrong•16m ago•0 comments

Memory has grown to nearly two-thirds of AI chip component costs

https://epoch.ai/data-insights/ai-chip-component-cost-shares
2•intelkishan•19m ago•0 comments

The Drugging of the American Boy (2014)

https://www.esquire.com/news-politics/a32858/drugging-of-the-american-boy-0414/
2•downbad_•19m ago•1 comments

Ask HN: Why do people seem to generally hate AI?

1•Austin_Conlon•24m ago•5 comments

Urgent Evaluation Needed

1•Wessch•24m ago•0 comments

Show HN: Script to determine when government intervention is legitimate

https://julienreszka.github.io/economic-simulator/armey-curve.html
1•julienreszka•28m ago•1 comments

Resonance, randomness, and negotiated meaning for AI-assisted tarot divination

https://dl.acm.org/doi/10.1145/3772318.3791571
2•lkellar•29m ago•0 comments

Show HN: Rendering Chinese text on a Commodore 64

https://github.com/lionello/han64
2•lionello•35m ago•0 comments

'Corpse Point' in the Arctic Is Melting, Disturbing Centuries-Old Bodies

https://www.404media.co/corpse-point-in-the-arctic-is-melting-disturbing-centuries-old-bodies/
4•Brajeshwar•36m ago•0 comments

Intel: Advanced Vector Extensions 10.2 [pdf]

https://docs.alexrp.com/x86/x86_intel_avx_10_2.pdf
1•tosh•37m ago•0 comments

AI MOD Music Remastering at 32-bit 96 kHz

https://github.com/Kind-Computers/quinlight-audio
3•fechols•39m ago•0 comments

Irrational philistine "education" has won

https://pietersz.co.uk/2026/05/irrational-philistine-education-has-won
2•graemep•42m ago•0 comments

GoPro Q1 2026: gross margin went from 32% to 4.5% in one year, exploring a sale

https://finance.yahoo.com/quote/GPRO/earnings/GPRO-Q1-2026-earnings_call-552481.html
1•Lwrless•46m ago•0 comments

For Residents of Jurisdictions with Age Verification Laws

https://dianne.skoll.ca/projects/california.php
2•vslira•47m ago•0 comments

Venera 7

https://en.wikipedia.org/wiki/Venera_7
2•thunderbong•52m ago•0 comments

We open sourced another feature of our commercial EDR Show us love

1•SilverPlate3•53m ago•0 comments

Liverpool and Manchester Railway

https://en.wikipedia.org/wiki/Liverpool_and_Manchester_Railway
1•daverol•54m ago•0 comments

Free US expat tax calculators (no signup, static HTML)

https://www.clearedexpat.com/
2•advsign•55m ago•0 comments