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.

My journey from foreign correspondent to Uber driver in Trump's America

https://stevescherer.substack.com/p/my-journey-from-foreign-correspondent
1•only_in_america•1m ago•0 comments

Show HN: Shadowscan – see what an AI agent can access on your machine

https://github.com/LakshmiSravyaVedantham/shadowscan
1•sravyavedantham•1m ago•1 comments

Claude Code building 100 mini games with one prompt (5.3M tokens)

https://twitter.com/amgauge/status/2031809325375897931
1•august-•1m ago•0 comments

Sensational news. Fintech has published banking secrets for public access

https://sfg.media/en/a/monobank-accuses-client-collaboration-slovenian-flag-mistake/
1•Kizert•2m ago•0 comments

Vitalina: Export Your Apple Health Data as CSV/JSON

https://apps.apple.com/us/app/health-data-exporter-vitalina/id6759179139
2•MegaMaddin•7m ago•1 comments

Digital Democracy

https://calmatters.digitaldemocracy.org/
2•jruohonen•7m ago•0 comments

Kishida Prize crowns wordsmiths in the theater world

https://www.japantimes.co.jp/culture/2026/02/26/stage/kishida-prize-theater-japan/
3•PaulHoule•10m ago•0 comments

Zoox starts mapping Dallas and Phoenix for its robotaxis

https://techcrunch.com/2026/03/09/zoox-starts-mapping-dallas-and-phoenix-for-its-robotaxis/
2•gmays•11m ago•0 comments

Surgical Repair of Collapsed Attention Heads in ALiBi Transformers

https://arxiv.org/abs/2603.09616
3•palmerschallon•12m ago•1 comments

Why Escalation Favors Iran

https://www.foreignaffairs.com/iran/why-escalation-favors-iran
6•decimalenough•12m ago•1 comments

Thanks, ChatGPT

https://www.robpanico.com/articles/display/?entry_short=thanks-chatgpt
2•retrocog•12m ago•1 comments

Wired headphone sales are exploding. What's with the Bluetooth backlash?

https://www.bbc.com/future/article/20260310-wired-headphones-are-better-than-bluetooth
14•billybuckwheat•14m ago•2 comments

Feature Unrequest

https://kudmitry.com/articles/feature-unrequest/
3•skwee357•16m ago•0 comments

OrthoScience – Hybrid search engine for 500K+ orthopedic translational research

https://orthoarchives.com/en/orthoscience/search
2•DrMeric•17m ago•1 comments

Don't post generated/AI-edited comments. HN is for conversation between humans.

https://news.ycombinator.com/newsguidelines.html#generated
129•usefulposter•23m ago•29 comments

An Update from First Board Chair Laurie Leshin

https://www.firstinspires.org/about/press-room/an-update-from-first-board-chair-laurie-leshin
2•ndrake•23m ago•0 comments

Show HN: R2 Desk Pro – a vault-locked desktop client for CF R2 (Tauri/Rust)

https://r2desk.greeff.dev
2•pio_greeff•23m ago•0 comments

A record share of U.S. workers now have access to paid leave

https://19thnews.org/2026/03/paid-leave-policies-united-states/
3•mooreds•23m ago•0 comments

I'm glad the Anthropic fight is happening now

https://www.dwarkesh.com/p/dow-anthropic
3•emschwartz•23m ago•0 comments

Over puppy yoga? Try it with snakes

https://text.npr.org/nx-s1-5743865
2•mooreds•24m ago•0 comments

Do You Need to Wash New Clothes Before Wearing Them?

https://www.nytimes.com/2026/03/10/well/wash-new-clothes-before-wearing.html
3•mooreds•24m ago•0 comments

Buying a Laptop Online Is a Broken Experience (2018)

https://blog.raed.dev/posts/buying_laptop_online_broken_experience/
2•Raed667•25m ago•0 comments

We Built a Linux Kernel Mailing List Front End

https://nexus-kb.com/blog/nexus-kb-announcement/
3•tansanrao•27m ago•0 comments

Ask HN: Developers still enjoying development after AI?

3•aavci•27m ago•3 comments

The Internet Has 100M Shops and No Front Door

https://askucp.com/blog
2•possiblelion•28m ago•0 comments

OmniCode: A Benchmark for Evaluating Software Development Agents

https://arxiv.org/abs/2602.02262
2•foma-roje•29m ago•0 comments

From IDEs to AI Agents with Steve Yegge [video]

https://www.youtube.com/watch?v=aFsAOu2bgFk
2•claudiug•29m ago•1 comments

Show HN: Daub – A rendering spec for AI-generated UIs (two files, no build step)

https://daub.dev
2•kulesh•32m ago•0 comments

FBI warns Iran wanted to attack California with drones

https://www.sfchronicle.com/california/article/iran-drones-west-coast-attack-22071032.php
4•babelfish•33m ago•0 comments

The Passion of Will Self

https://www.newstatesman.com/culture/books/2026/03/the-passion-of-will-self
3•apollinaire•35m ago•0 comments