frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Dusklight is a reverse-engineered reimplementation of Twilight Princess

https://github.com/TwilitRealm/dusklight
1•Plasmoid•5m ago•0 comments

TP-7 Field Recorder

https://teenage.engineering/products/tp-7
2•nirkalimi•6m ago•1 comments

Clippy Config Should Be Stricter

https://emschwartz.me/your-clippy-config-should-be-stricter/
1•birdculture•11m ago•0 comments

Tokenization Is the Bottleneck You're Not Measuring

https://ranvier.systems/2026/05/25/tokenization-is-the-bottleneck-youre-not-measuring.html
1•mindsaspire•13m ago•0 comments

ContextVault – Local-First AI Conversation Recorder for ChatGPT, Claude, Gemini

https://context-vault-two.vercel.app/
1•aliabdm•17m ago•0 comments

I built a new app and I don't know how I feel about it

https://philna.sh/blog/2026/05/25/i-built-a-new-app-and-i-dont-know-how-i-feel-about-it/
1•philnash•18m ago•0 comments

What is 'pink-slime' journalism and has it infiltrated Australian media?

https://www.abc.net.au/news/2026-05-22/pink-slime-journalism-regional-australia-ai/106639600
1•MarxOk•24m ago•0 comments

The UX Cost of Swipe Culture

1•BrittanyHale•24m ago•0 comments

Radvd 2.21 – Linux IPv6 Router Advertisement Daemon

https://radvd.litech.org/
1•neustradamus•25m ago•0 comments

AlphaProof Nexus is out, but Hassabis said solving Erdős isn't real invention

https://www.youtube.com/watch?v=bgBfobN2A7A
1•muditsrivastava•25m ago•0 comments

Creating a More Human Online Dating Experience

https://nataliyajr.blogspot.com/
2•NicoleCarterhj•25m ago•0 comments

Presentation and promulgation of the Encyclical Letter "Magnifica humanitas"

https://www.vatican.va/content/leo-xiv/en/events/event.dir.html/content/vaticanevents/en/2026/5/2...
1•KnuthIsGod•27m ago•2 comments

Sweden now considered a smoke free country

https://www.sverigesradio.se/artikel/sweden-now-considered-a-smoke-free-country
1•aleda145•27m ago•0 comments

Wyoming Company Uses High-Tech AI Sprinklers to Save Homes from Wildfire

https://cowboystatedaily.com/2026/05/25/wyoming-company-uses-high-tech-ai-sprinklers-to-save-home...
1•Bender•28m ago•1 comments

Ask HN: How do you get Internships with no work experience but cool projects?

1•dragonsenseiguy•30m ago•0 comments

Stack Overflow Sold to Tech Giant Prosus for $1.8B (2021)

https://www.wsj.com/business/deals/software-developer-community-stack-overflow-sold-to-tech-giant...
3•poly2it•32m ago•1 comments

Leadwerks Game Engine 5.1 Beta Releases with Support for "Potato PCs"

https://www.leadwerks.com/community/blogs/entry/2896-leadwerks-51-beta-is-now-available-with-supp...
3•Josh_Klint•45m ago•0 comments

Promising One and done heart disease genetic therapy

https://www.nytimes.com/2026/05/25/health/cholesterol-ldl-gene-therapy.html
2•anjel•52m ago•0 comments

CVE-2026-28952: Apple macOS 26.5 Kernel Vuln found by Claude

https://support.apple.com/en-us/127115
29•dragonsenseiguy•53m ago•1 comments

Google is its own worst enemy

https://disconnect.blog/google-is-its-own-worst-enemy/
6•cdrnsf•53m ago•0 comments

The Vatican-Anthropic relationship that's reshaping the AI ethics debate

https://religionnews.com/2026/05/22/why-anthropic-is-helping-unveil-the-popes-new-encyclical-on-ai/
4•cdrnsf•54m ago•0 comments

Performance of Rust Language [pdf]

https://github.com/yugr/rust-slides/
2•tanelpoder•56m ago•0 comments

Vim Classic

https://vim-classic.org/
3•GalaxyNova•1h ago•0 comments

SkiFreedle, a daily game version of SkiFree (1991)

https://skifreedle.com/
2•gfysfm•1h ago•1 comments

LLMs require curated context for reliable political fact-checking

https://arxiv.org/abs/2511.18749
2•teleforce•1h ago•0 comments

I Found the Lost Amiga Unix 2.02 [video]

https://www.youtube.com/watch?v=sDei4Tyo6yA
1•zdw•1h ago•0 comments

Secure TUI to Monitor Docker

https://toricli.sh
2•imbobbytables•1h ago•0 comments

Nobody cracks open a programming book anymore

https://unix.foo/posts/nobody-cracks-open-a-programming-book/
62•zdw•1h ago•70 comments

Distributing LLM Inference in DwarfStar

https://antirez.com/news/167
2•liveranga•1h ago•0 comments

Contextual web personalization and inspection engine on Fastly's edge stack

https://cheaply-joint-goshawk.edgecompute.app/
1•Fcuervo•1h ago•0 comments