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.

Ask HN: Where is my UX after all those billions spent on LLM codegen?

1•tzury•3m ago•0 comments

The Psychology of Human Misjudgment (2005) [pdf]

http://web.archive.org/web/20151004200748/http://law.indiana.edu/instruction/profession/doc/16_1.pdf
1•downbad_•3m ago•1 comments

The open standard for agent readiness

https://www.agentready.org/
1•geueuudyhdwj•3m ago•0 comments

ICANN opens applications for new generic top-level domains first time since 2012

https://www.theregister.com/2026/05/01/icann_new_gtld_applications/
2•Bender•4m ago•0 comments

The Persian Polycrisis

https://thehonestsorcerer.substack.com/p/the-persian-polycrisis
1•OgsyedIE•4m ago•0 comments

Women sue the men who used their Instagram feeds to create AI porn influencers

https://www.wired.com/story/ai-porn-lawsuit-arizona/
2•Bender•5m ago•0 comments

Minnesota passes ban on fake AI nudes; app makers risk $500K fines

https://arstechnica.com/tech-policy/2026/05/minnesota-set-to-be-first-state-to-ban-nudification-a...
2•Bender•6m ago•0 comments

WolfCOSE vs. the Field: The Smallest, Most Complete COSE Implementation

https://aidangarske.github.io/wolfCOSE/blog/wolfcose-vs-the-field/
1•aidangarske•6m ago•1 comments

Fredis: A simple in-memory Redis compatible server in Rust

https://gitlab.com/foscogab/fredis
1•gfosco•10m ago•1 comments

AI as Infrastructure

https://cunderwood.dev/2026/05/01/ai-as-infrastructure/
2•speckx•10m ago•0 comments

How to speed up clinical trials (Works in Progress Podcast)

https://podcasts.apple.com/us/podcast/how-to-speed-up-clinical-trials/id1819488714?i=1000763007270
1•marojejian•11m ago•1 comments

Top AI companies agree to work with Pentagon on secret data

https://www.washingtonpost.com/technology/2026/05/01/pentagon-ai-deals-microsoft-amazon-google-cl...
1•geox•12m ago•0 comments

Musk Warns of Killer AI, While He and Silicon Valley Cash in on AI That Kills

https://theintercept.com/2026/05/01/elon-musk-openai-lawsuit-trial/
2•cdrnsf•12m ago•0 comments

The California Government Is Coming for Your E-Bikes

https://sfstandard.com/2026/04/30/e-bike-regulation-license-plates-laws-dmv/
2•m463•12m ago•0 comments

Using "underdrawings" for accurate text and numbers

https://samcollins.blog/underdrawings/
3•samcollins•14m ago•1 comments

Claude Code can now /perceive

https://github.com/machinepulse-ai/world2agent
2•WayLonWen•14m ago•0 comments

An Interview with Josh Fisher – Inventing VLIW, Multiflow, Itanium [video]

https://www.youtube.com/watch?v=ZF8ohzWmuzI
1•dataviz1000•16m ago•0 comments

Show HN: Blotter, a live map of police radio activity

https://blotter.fm
1•s_e__a___n•18m ago•1 comments

Andrej Karpathy's Sequoia talk, I agree with most but not this

https://twitter.com/xing101/status/2050271353983598630
3•xingwu•19m ago•2 comments

Show HN: BeautyShot – easy macOS promo screenshots

https://bendansby.com/apps/beautyshot/
1•webwielder2•19m ago•0 comments

Farrell vs. LinkedIn Corporation 4:26-CV-02953-KAW (ND Cal Apr 6, 2026) [pdf]

https://ia601503.us.archive.org/33/items/gov.uscourts.cand.467271/gov.uscourts.cand.467271.1.0.pdf
1•1vuio0pswjnm7•20m ago•0 comments

LLM-eval-kit: Distributed LLM evaluation framework (v0.3.0)

https://github.com/benmeryem-tech/llm-eval-kit
1•benmeryem_ai•22m ago•0 comments

Achi RPC: An RPC library for workers on the web and node JavaScript

https://www.npmjs.com/package/achi-rpc
1•stiles11•23m ago•0 comments

"I suck" -theprimeagen [video]

https://www.youtube.com/watch?v=V-ZvAw_VNk4
1•eamag•23m ago•0 comments

Ganglion: Hostile-network reachability for robot fleets, built on libp2p

https://github.com/TafyLabs/ganglion
1•karma0•25m ago•1 comments

Y Combinator alum Skio sells for $105M cash, only raised $8M, founder says

https://techcrunch.com/2026/04/30/y-combinator-alum-skio-sells-for-105m-cash-only-raised-8m-found...
3•Brajeshwar•28m ago•0 comments

TypeKro: Write TypeScript. Deploy Kubernetes

https://typekro.run
3•ManWith2Plans•29m ago•0 comments

Pentagon reaches agreements with top AI companies, but not Anthropic

https://www.reuters.com/business/retail-consumer/pentagon-reaches-agreements-with-leading-ai-comp...
2•mikhael•33m ago•0 comments

It's Possible to Learn in Our Sleep. Should We?

https://www.newyorker.com/culture/annals-of-inquiry/its-possible-to-learn-in-our-sleep-should-we
2•XzetaU8•34m ago•1 comments

Decoding a decade of rising global light pollution

https://www.darkskyconsulting.com/blog/decoding-a-decade-of-rising-global-light-pollution
2•DamonHD•36m ago•0 comments