frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Grasshopper – Algorithmic Modeling for Rhino

https://www.grasshopper3d.com/
1•gurjeet•1m ago•0 comments

Codeberg Is Down

https://social.anoxinon.de/@codebergstatus/116824443563698727
2•sscaryterry•5m ago•0 comments

DOE explains antimatter

https://www.energy.gov/science/doe-explainsantimatter
1•hhs•6m ago•0 comments

We almost had a smartphone in the 90s. Why did it fail?

https://www.npr.org/2026/06/26/nx-s1-5872785/general-magic-sony-magiclink-constraints
2•hackernj•7m ago•1 comments

New Work, new technologies, and the skill premium

https://www.nber.org/digest/202606/new-work-new-technologies-and-skill-premium
2•hhs•9m ago•0 comments

Teamwork: An unexpected strategy bacteria use to survive antibiotics

https://www.bcm.edu/news/teamwork-an-unexpected-strategy-bacteria-use-to-survive-antibiotics
2•hhs•12m ago•0 comments

The Weight-Loss Revolution People Didn't Want

https://greyenlightenment.com/2026/06/23/the-weight-loss-revolution-people-didnt-want/
2•paulpauper•18m ago•0 comments

Show HN: KV-psi, using Linux PSI to to trim an LLM KV cache

https://github.com/infiniteregrets/kv-psi
1•infiniteregrets•20m ago•0 comments

The psychology behind AI fueled delusions

https://www.wsj.com/tech/personal-tech/ai-chatbots-psychology-delusion-662a3663
1•Jimmc414•20m ago•1 comments

Show HN: Peek-CLI: let coding agents see your browser

https://github.com/puffinsoft/peek-cli
3•BeverlyHills001•20m ago•0 comments

Show HN: Sasso – pure-Rust SCSS compiler, byte-exact to current dart-sass

https://github.com/momiji-rs/sasso
1•linyiru•20m ago•0 comments

Walter S. Arnold–Sculptor/Stone Carver

https://stonecarver.com/
1•NaOH•21m ago•0 comments

Reserved THP Feature Proposed for Linux to Combine the Best of HugeTLB and THP

https://www.phoronix.com/news/Reserved-THP-Linux
1•Bender•30m ago•0 comments

How to Audit a Legacy Codebase in the First Week

https://piechowski.io/post/how-i-audit-a-legacy-rails-codebase/
1•howToTestFE•31m ago•0 comments

Graphify – Open-Source Knowledge Graph Skill for AI Coding Assistants

https://graphify.net/index.html#features
2•xy008areshsu•44m ago•0 comments

US Army used 35 drones and 100 lb of C4 to clear a breach

https://www.businessinsider.com/us-army-commander-used-drones-c4-packed-robots-clear-breach-2026-6
2•Lihh27•45m ago•0 comments

Set Up Your Own DoH Service

https://nochan.net/b/Internet-Crap/20260602-Set-Up-Your-Own-DoH-Service/
2•Bender•46m ago•0 comments

Show HN: I made a webcam motion detector, local/cloud storage, AI person detect

https://camera10.com/
1•pixeltwenty•46m ago•0 comments

Tesla settles lawsuit over fatal FSD pedestrian crash tied to 3.2M-vehicle probe

https://electrek.co/2026/06/26/tesla-fsd-pedestrian-death-settlement/
3•logickkk1•50m ago•0 comments

Old Computer Challenge

http://occ.sdf.org/
2•wrxd•52m ago•0 comments

Show HN: Vlt – A local vault for the API keys you use in every project

https://bwanaerp.medium.com/how-to-stop-losing-api-keys-forever-i-built-a-tool-for-this-8b2adfca782a
1•instarlaxy•54m ago•0 comments

The cost YAGNI was never about

https://newsletter.kentbeck.com/p/the-cost-yagni-was-never-about
2•mustaphah•56m ago•0 comments

Choosing a Public DNS Resolver

https://evilbit.de/dns-resolver-guide.html
1•pawal•59m ago•0 comments

Show HN: E3d-pod2vid – AI pipeline that turns podcasts into YouTube-ready videos

https://github.com/spacepacket1/e3d-pod2vid
2•spacepacket•1h ago•0 comments

Heimdall, a small open-source TUI to watch my homelab machines from one terminal

https://github.com/kinncj/Heimdall
3•kinncj•1h ago•0 comments

Why One of Tech's Biggest Gamblers Is Betting Against Elon Musk's AI Vision

https://www.wsj.com/tech/why-one-of-techs-biggest-gamblers-is-betting-against-elon-musks-ai-visio...
3•1vuio0pswjnm7•1h ago•4 comments

Reclaiming the Roads

https://worksinprogress.co/issue/reclaiming-the-roads/
2•JumpCrisscross•1h ago•0 comments

Silicon Valley has much to learn from the spreadsheet jockeys it despises

https://www.economist.com/business/2026/06/24/silicon-valley-has-much-to-learn-from-the-spreadshe...
2•1vuio0pswjnm7•1h ago•0 comments

GPS Satellites Tracker

https://www.gps-satellites.com/
1•ohjeez•1h ago•0 comments

HackMate – Automate the OpenCore Hackintosh USB Setup from Linux/Windows/macOS

https://github.com/riftaway7-code/hackmate
1•hackmateapp•1h ago•0 comments