frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Generation Alpha

https://en.wikipedia.org/wiki/Generation_Alpha
1•keepamovin•5m ago•0 comments

Musk Says He 'Was a Fool' to Provide OpenAI's Early Funding

https://www.nytimes.com/2026/04/29/technology/musk-openai-trial-altman.html
2•1vuio0pswjnm7•5m ago•0 comments

Musk casts himself as AI's good guy in testimony vs. OpenAI

https://www.axios.com/2026/04/30/musk-openai-safety-grok
2•1vuio0pswjnm7•6m ago•0 comments

7-Zip 26.01 (7zip) – A free file archiver for high compression

https://sourceforge.net/p/sevenzip/discussion/45797/thread/555e132ba4/
1•neustradamus•7m ago•0 comments

FDA alleges 'manipulated' data supported approval of Amgen's autoimmune drug

https://www.biospace.com/fda/fda-alleges-manipulated-data-supported-approval-of-amgens-autoimmune...
2•randycupertino•12m ago•1 comments

Zulip 12.0 Released

https://blog.zulip.com/2026/04/27/zulip-12-0-released/
1•tabbott•13m ago•0 comments

Wanman: Open-source agent matrix network with JSON-RPC communications

https://github.com/chekusu/wanman/
2•imWildCat•20m ago•0 comments

Open-source briefing packets and citizen-action toolkits

https://github.com/ClosedNetwork/closed-network-flock-resources
2•pkaeding•20m ago•0 comments

Copilot Student GPT-5.3-Codex removal from model picker

https://github.blog/changelog/2026-04-27-copilot-student-gpt-5-3-codex-removal-from-model-picker/
1•aaronsung•22m ago•1 comments

AInvest

https://www.ainvest.com
2•Yang_Ruichen•23m ago•0 comments

Show HN: Agent that refuses to run commands without human approval

https://github.com/few-sh/fewshell
2•hexer303•24m ago•0 comments

Microsoft lifts 2026 AI spend by $25B to cover component price rises

https://www.theregister.com/2026/04/30/microsoft_q3_2026/
4•omer_k•25m ago•0 comments

A Grounded Conceptual Model for Ownership Types in Rust

https://cacm.acm.org/research-highlights/a-grounded-conceptual-model-for-ownership-types-in-rust/
4•tkhattra•26m ago•0 comments

Have You Seen the New Excel?

https://idiallo.com/blog/have-you-seen-the-new-xl-ai-parody
2•jnord•27m ago•0 comments

Neural similarity predicts whether strangers become friends

https://www.nature.com/articles/s41562-025-02266-7#Sec2
2•E-Reverance•28m ago•0 comments

Craig Venter has died

https://www.jcvi.org/media-center/j-craig-venter-genomics-pioneer-and-founder-jcvi-and-diploid-ge...
26•rdl•29m ago•4 comments

On the stand, Elon Musk can't escape his own tweets

https://techcrunch.com/2026/04/29/on-the-stand-elon-musk-cant-escape-his-own-tweets/
3•jnord•29m ago•0 comments

The feed doesn't know you, and YouTube refuses to let you browse

https://evilgeniuslabs.ca/blog/the-feed-doesnt-know-you
2•paulpauper•32m ago•0 comments

We Don't Know How A.I. Works. That's a Problem

https://www.nytimes.com/2026/04/15/magazine/ai-black-box-interpretability-research.html
2•lxm•33m ago•0 comments

When a tornado hits after US Government mass-deploy auto kill-switch

https://twitter.com/gatlin_didier/status/2049617318112534743
2•egberts1•41m ago•0 comments

Failed AI tractor company lays off all employees, abandons Bay Area headquarters

https://www.sfgate.com/tech/article/monarch-ai-tractor-failure-22183476.php
5•randycupertino•42m ago•0 comments

Show HN: WorkProof – JSON schema for skill evidence graphs

https://github.com/TalentProof/workproof-schema
2•parth4•46m ago•0 comments

Botfiles: Dotfiles-esque setup for Managing Agents

https://twitter.com/curious_queue/status/2049660997993152855
2•sourya4•47m ago•1 comments

Zwift buys Rouvy in shake-up of indoor cycling

https://www.bikeradar.com/news/zwift-buys-rouvy-in-massive-shake-up-of-indoor-cycling
3•obilgic•52m ago•0 comments

Claude.ai and API Unavailable

https://status.claude.com/incidents/2gf1jpyty350
85•rob•53m ago•74 comments

Claude.ai Down Again?

16•zh_code•53m ago•14 comments

45% of Hostile Bot Traffic Passes Your WAF. Here's Why

https://botconduct.org/waf-baypass-45-percent/
2•botconduct•54m ago•0 comments

AirDrop is coming to more Android phones, and I'm here for it

https://9to5mac.com/2026/04/29/airdrop-is-now-widely-available-on-android-phones-and-im-here-for-it/
2•omer_k•1h ago•0 comments

Show HN: Serra – A Magic: The Gathering life counter using DRM/KMS

https://git.sr.ht/~cmt/serra
2•nan60•1h ago•0 comments

Qualcomm, Apple, and Nuvia alumni form CPU startup – Nuvacore

https://www.tomshardware.com/pc-components/cpus/legendary-qualcomm-apple-and-nuvia-alumni-form-ne...
2•gnabgib•1h ago•0 comments