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.

Elon Musk Misled Twitter Investors Before 2022 Buyout, Jury Says

https://www.bloomberg.com/news/articles/2026-03-20/elon-musk-misled-twitter-investors-before-2022...
1•toomanyrichies•28s ago•0 comments

Neugebauer Lutnick Confrontation hints at trouble with data center project

https://www.politico.com/news/2026/03/20/confrontation-ceo-and-lutnick-00838496
1•defrost•34s ago•0 comments

One Battle After Another: PTA and the Death of Revolutionary Cinema

https://old.reddit.com/r/TrueFilm/comments/1nv83g1/one_battle_after_another_paul_thomas_anderson_...
1•kaycebasques•2m ago•0 comments

How 30 agent frameworks handle context rot, memory and tools

https://github.com/vasilyevdm/ai-agent-handbook
1•rocketrider•2m ago•0 comments

User Interface Hall of Fame (1999)

http://hallofshame.gp.co.at/mfame.htm
1•12_throw_away•3m ago•0 comments

Stash: Fast and easy local-first file sync for agents

https://github.com/telepath-computer/stash
1•stlhood•3m ago•0 comments

The Anti-Portfolio

https://www.bvp.com/anti-portfolio
1•gone35•5m ago•0 comments

After nearly $1M in donations,78‑year‑old DoorDash driver says he's not retiring

https://www.nbcdfw.com/news/national-international/doordash-driver-not-retiring-1m-donations/3998...
1•teleforce•7m ago•0 comments

AI Fatigue

https://thethinkingbuilder.substack.com/p/on-ai-fatigue
2•kondov•9m ago•0 comments

What Happened to Gem?

https://dfarq.homeip.net/whatever-happened-to-gem/
2•naves•10m ago•0 comments

GrapheneOS adds experimental Pixel 10a support

https://grapheneos.org/releases
1•cf100clunk•10m ago•0 comments

Fisher Traction – at-home spinal decompression devices for neck and back pain

https://www.fishertraction.com/products/fisher-traction-neck-traction
1•denseroll•11m ago•0 comments

Wine Registry Seeks Co-Founder

1•Chelper•11m ago•0 comments

Notre Dame – families with incomes under $150k will pay zero tuition

https://news.nd.edu/news/notre-dame-announces-that-families-with-incomes-under-150-000-will-pay-z...
2•b_mc2•11m ago•0 comments

When Do We Become Adults, Really?

https://www.newyorker.com/culture/annals-of-inquiry/when-do-we-become-adults-really
1•benbreen•13m ago•0 comments

Skills Are Grown, Not Written

https://medium.com/@derwiki/skills-are-grown-not-written-7839e4a2bb36
1•derwiki•14m ago•0 comments

Claude Tried to Hack 30 Companies.Nobody Asked It To

https://trufflesecurity.com/blog/claude-tried-to-hack-30-companies-nobody-asked-it-to
2•ki4jgt•16m ago•0 comments

CEOs Don't Steer

https://www.ribbonfarm.com/2017/11/09/ceos-dont-steer/
2•vinhnx•18m ago•0 comments

Pull requests are dead, long live pull requests

https://gieseanw.wordpress.com/2026/03/20/pull-requests-are-dead-long-live-pull-requests/
2•andyg_blog•18m ago•0 comments

SlopFilter – uBlock Origin for AI-generated content

https://github.com/dilipShaachi/slopfilter
1•dilipa•21m ago•0 comments

Wine 11.5 – Run Windows Applications on Linux, BSD, Solaris and macOS

https://www.winehq.org/announce/11.5
3•neustradamus•22m ago•1 comments

The First Quantum Internet Protocol

https://qi.quantumcloud.one
1•wakanda-island•22m ago•0 comments

Stop Leading with the Affirmative Condition

https://idealchorus.com/posts/stop-leading-with-the-affirmative-condition
2•rubyrules•24m ago•0 comments

Technological Speed Limit

https://metastable.org/speed-limit/
1•pbw•24m ago•0 comments

Why AI Analysis Gives You the Wrong Specific Answers

https://opinionatedintelligence.substack.com/p/why-ai-analysis-gives-you-generic
2•nano81•26m ago•0 comments

Glasses Cleaning Simulator

https://vole.wtf/glasses-cleaning/
3•gaws•27m ago•1 comments

Data transfer dock for Apple Watch development

https://mastodon.tz.is/@khaost/116253127280820238
3•Austin_Conlon•28m ago•0 comments

Show HN: Red Grid Link – peer-to-peer team tracking over Bluetooth, no servers

https://github.com/RedGridTactical/RedGridLink
1•redgridtactical•30m ago•0 comments

A History of Local LLMs

https://av.codes/blog/local-llms-history/
3•everlier•32m ago•0 comments

The Mystery of Rennes-Le-Château, Part 2: Secret Codes and Hidden Messages

https://www.filfre.net/2026/03/the-mystery-of-rennes-le-chateau-part-2-secret-codes-and-hidden-me...
2•doppp•33m ago•0 comments