frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Playing with Jupyter style playbooks that work with Claude Code

https://old.reddit.com/r/ClaudeCode/comments/1tgdvex/playing_with_jupyter_style_playbooks_that_work/
1•bgnm2000•1m ago•0 comments

Building a Micro-Drilling Machine [video]

https://www.youtube.com/watch?v=KrStsdNG5go
1•pillars•4m ago•0 comments

Better CSS fluid sizing with round()

https://ishadeed.com/article/css-round/
1•BaudouinVH•5m ago•0 comments

Void Room – an offline writing app that does nothing on purpose

https://noirsonance.com/product/void-room/
1•Rimedag•5m ago•0 comments

Your Evals Will Break and You Won't See It Coming

https://wanglun1996.github.io/blog/your-evals-will-break.html
1•muggermuch•5m ago•0 comments

DeerFlow 2.0 – Deep Exploration and Efficient Research Flow

https://github.com/bytedance/deer-flow
1•epaga•7m ago•0 comments

More than 100 UK datacentres plan to burn gas to generate electricity

https://www.theguardian.com/business/2026/may/18/uk-datacentres-plan-to-burn-gas-to-generate-elec...
2•pera•10m ago•0 comments

SFC vs. Vizio Ruling on General Public License Compliance

https://www.dlapiper.com/en-us/insights/publications/2026/01/sfc-v-vizio-ruling-on-general-public...
1•beckford•12m ago•0 comments

Animated 3D map of Chongqing metro [video]

https://www.bilibili.com/video/BV16Z421W7o3/?spm_id_from=333.1387.homepage.video_card.click
1•decimalenough•13m ago•1 comments

2ality Blog: Temporarily Offline because of AI

https://2ality.com
1•tbassetto•15m ago•0 comments

Old Kindle e-readers will stop working on May 20th

https://goodereader.com/blog/kindle/old-kindle-e-readers-will-stop-working-on-may-20th
1•kozmonaut•15m ago•0 comments

Ask HN: Which AI harness comes close to Claude Code?

1•shivang2607•16m ago•0 comments

Code Orange: Fail Small is complete. The result is a stronger Cloudflare network

https://blog.cloudflare.com/code-orange-fail-small-complete/
2•thewisenerd•20m ago•0 comments

`Never-ending' AI slop strains corporate hacking reward schemes

https://www.ft.com/content/dbec4441-02dc-4053-8500-85677973d324
2•1vuio0pswjnm7•24m ago•0 comments

Spring Physics in my Word Game? A technical deep dive into SvelteJS animations

https://colechamberlin.substack.com/p/spring-physics-in-my-word-game
2•jessecoleman•27m ago•0 comments

Ask HN: Favorite In-Depth Deep Dives

4•vsupalov•27m ago•1 comments

Defence Tech Jobs for the week of 2026-05-18

https://defencetechjobs.substack.com/p/defence-tech-jobs-for-the-week-of-561
4•hnsa•30m ago•0 comments

Hacking a weird TV censoring device [video]

https://www.youtube.com/watch?v=a6EWIh2D1NQ
3•goodmythical•43m ago•0 comments

AI Eats the World (2026)

https://www.ben-evans.com/presentations
3•andrewjb•43m ago•0 comments

RAG vs. Fine-Tuning – The Question Every AI Builder Gets Wrong

https://thingswithai.org/posts/rag-vs-fine-tuning
2•UtkarshPatel13•44m ago•0 comments

Wayland Compositor in Minecraft [video]

https://www.youtube.com/watch?v=cTkEM7b0IQw
1•bigyabai•45m ago•0 comments

Stanford AA228V: Validation of Safety Critical Systems (2025)

https://www.youtube.com/playlist?list=PLoROMvodv4rOq1LMLI8U7djzDb8--xpaC
2•imakwana•46m ago•1 comments

Donald E. Knuth: The letter S [pdf]

https://gwern.net/doc/design/typography/1980-knuth.pdf
3•tzury•47m ago•0 comments

TestTest

2•kollittle•47m ago•0 comments

Lost at sea: the man who vanished for 14 months (2015)

https://www.theguardian.com/world/2015/nov/07/fisherman-lost-at-sea-436-days-book-extract
3•downbad_•47m ago•1 comments

Pope creates AI study group as Vatican prepares release of first encyclical

https://apnews.com/article/vatican-artificial-intelligence-pope-musk-nvidia-trump-889c0066f0d5ce7...
2•1vuio0pswjnm7•49m ago•0 comments

SPF Softfail vs. Hardfail: A Data-Backed Decision Guide

https://dmarcguard.io/blog/spf-softfail-vs-hardfail/
1•meysamazad•49m ago•0 comments

LLM Tracing with MLflow AI Gateway

https://karnwong.me/posts/2026/04/llm-tracing-with-mlflow-ai-gateway/
1•meysamazad•50m ago•0 comments

Letters from a Stoic by Seneca

https://lars-christian.com/posts/2026-04-04-letters-from-a-stoic-by-seneca/
1•meysamazad•50m ago•0 comments

Classical physics can explain quantum weirdness, study shows

https://www.msn.com/en-us/news/technology/classical-physics-can-explain-quantum-weirdness-study-s...
1•galaxyLogic•56m ago•0 comments