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.

I wish Xcode was more like Visual Studio when coding C++

https://www.lasselaursen.com/post/i-wish-xcode-was-more-like-visual-studio-when-coding-c/
2•Gazoo101•4m ago•0 comments

Formal Verification in Any Language for Everybody (lean 4)

https://www.dev-log.me/formal_verification_in_any_language_for_everybody/
1•wazHFsRy•4m ago•1 comments

Can LLMs accelerate science? An experiment

https://pavpanchekha.com/blog/llk.html
1•pavpanchekha•8m ago•0 comments

Federal Court Denies Anthropic's Motion to Lift 'Supply Chain Risk' Label

https://www.nytimes.com/2026/04/08/technology/anthropic-pentagon-risk-circuit-court.html
1•DeathArrow•9m ago•0 comments

Flatpak: Complete Sandbox Escape

https://github.com/flatpak/flatpak/security/advisories/GHSA-cc2q-qc34-jprg
1•eyberg•10m ago•0 comments

AI #163: Mythos Quest

https://thezvi.substack.com/p/ai-163-mythos-quest
1•paulpauper•15m ago•0 comments

US adults are having fewer kids – and it's forcing schools to close

https://www.theguardian.com/us-news/2026/mar/16/birthrate-schools-closing
1•PaulHoule•18m ago•0 comments

'The Egg' by Andy Weir (2009)

https://www.galactanet.com/oneoff/theegg_mod.html
2•goekjclo•19m ago•0 comments

When AI Day of Reckoning?

https://www.overcomingbias.com/p/when-ai-day-of-reckoning
1•paulpauper•19m ago•0 comments

Keychron has open sourced its hardware

https://github.com/Keychron/Keychron-Keyboards-Hardware-Design/tree/main
3•azhenley•20m ago•0 comments

I rebuilt Claude Code's removed /buddy companion as a permanent MCP app

https://github.com/1270011/claude-buddy
2•1270011•22m ago•0 comments

Violating Copyright, Not the Planet

https://mumumelon.co/
1•Tomte•37m ago•0 comments

Bryson DeChambeau to use a 5-iron he made with 3D printer at Masters

https://www.espn.com/golf/story/_/id/48431238/bryson-dechambeau-using-iron-made-3d-printer-masters
1•1659447091•37m ago•0 comments

You've Been Writing Matplotlib Abstractions Wrong

https://www.dontusethiscode.com/blog/2026-04-08_matplotlib_abstractions.html
1•Tomte•38m ago•0 comments

How can I get your attention?

1•ChalresWT•40m ago•0 comments

On Partners, Mario, & Pi

https://www.foggynotions.day/#on-partners-mario-pi
1•doppp•41m ago•0 comments

The Waymo Rule for AI-Generated Code

https://rng.md/posts/the-waymo-rule-for-ai-generated-code/
2•handfuloflight•44m ago•0 comments

Know the Owners – Explore the ownership structures behind name brand products

https://knowtheowners.com/
2•pkutro•44m ago•1 comments

Pizza Tycoon simulated traffic on a 25 MHz CPU

https://pizzalegacy.nl/blog/traffic-system.html
1•vinhnx•45m ago•0 comments

Alignment Risk Update for Claude Mythos [pdf]

https://www-cdn.anthropic.com/79c2d46d997783b9d2fb3241de43218158e5f25c.pdf
1•jablongo•46m ago•0 comments

Bash Owns the Loop

https://www.nibzard.com/wrappers/
1•peterdemin•46m ago•0 comments

Under the hood of MDN's new front end

https://developer.mozilla.org/en-US/blog/mdn-front-end-deep-dive/
3•vinhnx•47m ago•0 comments

Open Source Security at Astral

https://astral.sh/blog/open-source-security-at-astral
22•vinhnx•48m ago•0 comments

Show HN: Jsonl Viewer – An offline JSON Lines viewer in a single HTML file

2•Cassandra99•49m ago•1 comments

Esquire Singapore responds to criticism over use of AI for Mackenyu story

https://cnalifestyle.channelnewsasia.com/entertainment/esquire-singapore-ai-mackenyu-interview-ba...
1•hboon•53m ago•0 comments

GPU solver that matches Fujitsu's $1M Digital Annealer on cubic optimization

https://github.com/MysticCodingCat/CUDA-Native-HUBO
1•bobchen1322•54m ago•1 comments

The Slow Startup Movement

https://tarikhkorula.com/wisdom/the-slow-startup-movement
1•subdane•54m ago•1 comments

Pope Leo Reportedly Unlikely to Visit US After Trainwreck Trump Admin Meeting

https://www.mediaite.com/media/news/pope-leo-reportedly-unlikely-to-visit-us-after-trainwreck-tru...
6•rawgabbit•55m ago•0 comments

Apple Will Release iPhone Air 2 No Matter How Badly It May Sell

https://www.macrumors.com/2026/04/08/iphone-air-2-to-launch-no-matter-what/
1•mgh2•56m ago•0 comments

Show HN: We Evaluates Medical Research Agent Skills

https://github.com/aipoch/medical-research-skills
2•The_resa•1h ago•0 comments