frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Empirical, a language for time-series analysis

https://www.empirical-soft.com/
1•dmmalam•1m ago•0 comments

Thomas Hawkins and the Letters of Wanda Tinasky

https://en.wikipedia.org/wiki/Tom_Hawkins_(writer)
1•sans_souse•1m ago•1 comments

Dragon Ball Color Correction Process [pdf]

https://andrewvanner.github.io/som/SoM_CC_Process_Day.pdf
1•haunter•2m ago•0 comments

Best Cryptocurrency Recovery Company

https://www.autopsymainnetsolutions.com
1•SAMUELluck•2m ago•1 comments

Show HN: Updose – A boilerplate for AI coding tool configs

https://github.com/Alchemist85K/updose
1•beomjunkdev•3m ago•1 comments

Neurotechnology? For Cancer?

https://www.owlposting.com/p/neurotechnology-for-cancer-ben-woodington
1•crescit_eundo•5m ago•0 comments

Show HN: Zi2zi-JIT – Generate Chinese font in one hour

https://github.com/kaonashi-tyc/zi2zi-JiT
2•zi2zi-jit•8m ago•0 comments

Ablation – Short Fiction from an LLM POV

https://shenstories.com/ablation
2•jamesjyu•10m ago•0 comments

Trump Admin. Still Used Anthropic's Claude in Iran Strikes, Hours After It

https://sfist.com/2026/03/02/trump-administration-still-used-sfs-anthropic-in-iran-strikes-mere-h...
9•cdrnsf•11m ago•0 comments

Reframing Energy for the Age of Electricity [pdf]

https://ember-energy.org/app/uploads/2026/02/Reframing-Energy-for-the-Age-of-Electricity-1.pdf
2•barbazoo•11m ago•0 comments

AI Tool Sets New Standard in Diagnosing Rare Diseases

https://lifespan.io/news/ai-tool-sets-new-standard-in-diagnosing-rare-diseases/
2•RobotToaster•15m ago•0 comments

Show HN: DeadSwitch – Encrypted USB vault with dead man's switch

https://dead-switch.com/
2•chuckgreve•16m ago•0 comments

Japan Radio Begins 24-Hour Shortwave Transmission in Response to Iran Attacks [pdf]

https://www3.nhk.or.jp/nhkworld/upld/thumbnails/en/information/nhk_world-japan_radio_begins_tempo...
9•TigerUniversity•16m ago•0 comments

How to Count AIs: Individuation and Liability for AI Agents

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6273198
3•droidjj•16m ago•0 comments

Werner Herzog Between Fact and Fiction

https://unherd.com/2026/02/why-scientists-should-read-more-poetry/?edition=us
2•tintinnabula•16m ago•1 comments

The workers behind Meta's smart glasses can see everything

https://www.svd.se/a/K8nrV4/metas-ai-smart-glasses-and-data-privacy-concerns-workers-say-we-see-e...
54•sandbach•19m ago•6 comments

Anthropic and Alignment

https://stratechery.com/2026/anthropic-and-alignment/
2•stochastician•21m ago•0 comments

I used Claude Code's agent teams on a production incident (field report)

https://magarcia.io/using-claude-code-agent-teams-for-incident-investigation/
1•martinprins•22m ago•0 comments

Show HN: HNWatch – Keyword monitoring for Hacker News with AI-scored digests

https://hnwatcher.watch
1•moehj•22m ago•0 comments

Watershed Moment for AI–Human Collaboration in Math

https://spectrum.ieee.org/ai-proof-verification
1•sohkamyung•26m ago•0 comments

OpenAI's 'Red Lines' Speak the NSA's Language

https://www.techdirt.com/2026/03/02/openais-red-lines-are-written-in-the-nsas-dictionary-where-wo...
1•cdrnsf•26m ago•0 comments

A Review of Uncle Bob's Functional Design: Principles, Patterns, and Practices

https://coding-mischief.com/?p=43
1•luca_takahashi•29m ago•0 comments

Apple Changes Trade-In Values for iPhones, Macs, and More

https://www.macrumors.com/2026/03/02/apple-trade-in-values-changed/
2•mgh2•30m ago•0 comments

Tampons in the men's room helped derail the Netflix-Warner Bros. deal

https://nypost.com/2026/03/01/business/how-tampons-in-the-mens-room-helped-derail-the-netflix-war...
2•greenchair•31m ago•0 comments

Show HN: Set of Braille Unicode spinners as React decorator components

https://github.com/agilek/cli-loaders
2•agilek•31m ago•0 comments

Iranians face third day of internet blackout as connectivity hits 1%

https://www.techradar.com/vpn/vpn-privacy-security/near-complete-shutdown-iranians-face-third-day...
2•geox•31m ago•0 comments

Show HN: I simulated 1200 Iranian missiles attacking air defences in a browser

https://airdefense.dev/iran
3•possiblelion•32m ago•0 comments

A simplified guide to TRS MIDI

https://minimidi.world/
3•jmole•32m ago•0 comments

How to Kill the Code Review

https://www.latent.space/p/reviews-dead
1•swyx•34m ago•1 comments

Yahoo Scout Beta

https://scout.yahoo.com
5•broabprobe•34m ago•0 comments