frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Simon Willison on Technical Blogging

https://writethatblog.substack.com/p/simon-willison-on-technical-blogging
1•cyndunlop•1m ago•0 comments

Nvidia makes AI weather forecasting more accessible, no supercomputer needed

https://thenewstack.io/nvidia-makes-ai-weather-forecasting-more-accessible-no-supercomputer-needed/
1•nick217•2m ago•0 comments

pi

https://buildwithpi.ai/
1•tosh•2m ago•0 comments

EU, India wrap up talks for landmark trade deal amid strained US ties

https://www.reuters.com/world/china/india-eu-wrap-up-talks-landmark-trade-deal-amid-strained-us-t...
1•alephnerd•3m ago•0 comments

Did they just nuke Opus 4.5 into the ground?

https://old.reddit.com/r/ClaudeCode/comments/1qmsfyo/did_they_just_nuke_opus_45_into_the_ground/
1•tamnd•3m ago•0 comments

Maia 200: The AI accelerator built for inference

https://blogs.microsoft.com/blog/2026/01/26/maia-200-the-ai-accelerator-built-for-inference/
1•Handy-Man•4m ago•0 comments

Women Game Designers

https://daily.jstor.org/the-hidden-history-of-women-game-designers/
1•ohjeez•4m ago•0 comments

Show HN: a habit tracker that only lets you track one habit

https://ahabit.app
1•davvie•5m ago•0 comments

Sometimes Your Job Is to Stay the Hell Out of the Way

https://randsinrepose.com/archives/sometimes-your-job-is-to-stay-the-hell-out-of-the-way/
2•Tomte•5m ago•1 comments

Ask HN: How do you prevent children from accessing your products?

3•eastoeast•6m ago•0 comments

Show HN: EhAye Engine – Give your AI a voice

https://ehaye.io
1•avidcoder•8m ago•0 comments

Ask HN: Open Source PM Work

2•conner_h5•8m ago•0 comments

Return Void (Hacking a Vespera II Telescope)

https://thomasloupe.com/project/a-telescope-for-the-world/
3•alnwlsn•8m ago•0 comments

Former astronaut on lunar spacesuits: "I don't think they're great "

https://arstechnica.com/space/2026/01/former-astronaut-on-lunar-spacesuits-i-dont-think-theyre-gr...
2•CharlesW•9m ago•0 comments

Show HN: Chord: Clawdbot alternative with a security layer

https://github.com/tvytlx/chord-releases
1•arctanx•9m ago•0 comments

Show HN: OffLingua on Device AI Translator

https://offlingua.rdcopilot.com/
1•mvpasarel•9m ago•0 comments

Literature Clock

https://literature-clock.jenevoldsen.com/
1•grajmanu•9m ago•0 comments

Temperature and the Sackur–Tetrode Equation [video]

https://www.youtube.com/watch?v=gRPv4rd_6O4
1•surprisetalk•11m ago•0 comments

The mountain that weighed the Earth

https://signoregalilei.com/2026/01/18/the-mountain-that-weighed-the-earth/
2•surprisetalk•11m ago•0 comments

What Drives Retention (2019)

https://www.raphkoster.com/2019/01/30/what-drives-retention/
1•surprisetalk•11m ago•0 comments

Bop It Playing Robot [video]

https://www.youtube.com/watch?v=i9Kmm2tILVo
1•surprisetalk•11m ago•0 comments

Study: More market freedom may mean fewer homicides

https://news.uga.edu/more-market-freedom-fewer-homicides/
1•giuliomagnifico•11m ago•0 comments

Show HN: Dhi – 520x faster data validation for Python, 77x faster for TypeScript

https://github.com/justrach/dhi
1•rachpradhan•11m ago•0 comments

Manage AI Agent skills easily with one CLI command

https://ai-devkit.com/docs/7-skills/
1•hoangnnguyen•13m ago•0 comments

Curl Project Drops Bug Bounties Due to AI Slop Blog – By Maya Posch

https://hackaday.com/2026/01/26/the-curl-project-drops-bug-bounties-due-to-ai-slop/
1•grajmanu•13m ago•1 comments

MCP and Skills: Why Not Both?

https://kvg.dev/posts/20260125-skills-and-mcp/
2•tanelpoder•14m ago•0 comments

The Death of Software 2.0 (A Better Analogy)

https://www.fabricatedknowledge.com/p/the-death-of-software-20-a-better
1•sasvari•14m ago•0 comments

Show HN: Recal – Turn meetings and Slack threads into actionable tasks

https://tryrecal.com
1•markbuilds•16m ago•0 comments

Challenging the dance of bailout and austerity (2025)

https://www.cambridge.org/core/journals/finance-and-society/article/challenging-the-dance-of-bail...
1•robtherobber•17m ago•0 comments

RTX 5090 pricing has risen by 55% since July

https://overclock3d.net/news/gpu-displays/rtx-5090-pricing-spikes-55-increase/
1•akyuu•17m ago•0 comments