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.

Design Notes: Local Lifetimes for Kotlin

https://github.com/Kotlin/KEEP/blob/main/notes/0007-local-lifetimes.md
1•LelouBil•8m ago•0 comments

Eyes on the Solar System

https://eyes.nasa.gov/apps/solar-system/
1•md224•9m ago•0 comments

Demystifying phone unlocking tools: A technical overview

https://osservatorionessuno.org/blog/2026/05/demystifying-phone-unlocking-tools-a-technical-overv...
1•Cider9986•10m ago•0 comments

Staybl, the browser that adjusts for tremors in real time (2022)

https://www.marketingbrew.com/stories/2022/04/26/the-story-behind-havas-new-app-for-people-with-t...
1•bobbiechen•11m ago•0 comments

Show HN: Free Fonts – a collection of 400+ original, open-source typefaces

https://www.mixfont.com/fonts
1•justswim•14m ago•0 comments

Lam Research focused on adding AI to chipmaking tools as it eyes US expansion

https://www.reuters.com/business/lam-research-focused-adding-ai-chipmaking-tools-it-eyes-us-expan...
1•tartoran•20m ago•0 comments

CVE-2026-28910: Breaking macOS App Sandbox Data Containers and Hijacking Apps

https://mysk.blog/2026/05/19/cve-2026-28910/
1•rzk•22m ago•0 comments

New Getmonero.org Website Design

https://beta.monerodevs.org/
2•Cider9986•24m ago•1 comments

Donald Trump abruptly postpones AI order after White House infighting

https://www.ft.com/content/14213cb0-8d11-4118-bac0-12a403696185
1•petethomas•25m ago•0 comments

Ask HN: What happens when you intercept and modify Claude Code's system prompt?

1•lbrauer•25m ago•0 comments

XMRChat – Tip your favorite streamer in cryptocurrency

https://xmrchat.com
4•Cider9986•27m ago•2 comments

Software Engineering at the Tipping Point

https://www.youtube.com/watch?v=2n41YjR5QfU
1•rahu_•27m ago•0 comments

Show HN: Traditional Card Game Website

https://throw-cards.org/
1•Kyselica•31m ago•0 comments

Replacing Static BI with a Deterministic NLP-to-SQL Engine over Delta Lake

https://hunnykathuria.substack.com/p/nlp-architecting-zero-intervention
1•AI_Force•32m ago•0 comments

Tell HN: I'm tired of AI-generated answers

40•theorchid•35m ago•16 comments

Thoughts on People and Blogs

https://manuelmoreale.com/thoughts/thoughts-on-people-and-blogs
1•sebg•35m ago•0 comments

Codex got better, codex might be built with Claude Opus

1•Robelkidin•35m ago•1 comments

How Node.js and V8 keep each other working

https://joyeecheung.github.io/blog/2026/05/18/how-nodejs-and-v8-keep-each-other-working/
1•aragonite•37m ago•0 comments

Google Is Shattering Under Its Own Weight (The IBM-Ification of Google?)

https://zeroshot.bearblog.dev/google-is-shattering-under-its-own-weight-the-ibm-ification-of-google/
2•sabatonfan•42m ago•0 comments

Starship's Twelfth Flight Test Livestream

https://x.com/i/broadcasts/1YxNrZwwomNxw
5•janpot•44m ago•0 comments

Nascar champion Kyle Busch dies, 41 after hospitalization with 'severe illness'

https://www.npr.org/2026/05/21/g-s1-123547/nascar-champ-kyle-busch-dies
2•RickJWagner•46m ago•0 comments

Why metaphors make AI animations come out better

https://frigade.com/blog/describe-the-world-not-the-widget
1•pancomplex•46m ago•0 comments

Genkit Middleware: Intercept, extend, and harden your agentic apps

https://developers.googleblog.com/announcing-genkit-middleware-intercept-extend-and-harden-your-a...
1•gmays•49m ago•0 comments

Proof of storage protocol support for abstract data types

https://pinionengineering.github.io/storage-proofs/
1•coryschwartz•50m ago•1 comments

Anthropic's "Profitability" Swindle

https://www.wheresyoured.at/anthropics-profitability-swindle/
1•misswaterfairy•50m ago•1 comments

Fork this repo to organize your GitHub Gists automatically

https://github.com/adrenak/gists
1•adrenak•50m ago•0 comments

I made a free Safari extension that shows when your YouTube video ends

https://github.com/Fluphalicious/youtube-end-time-extension
1•fluphies•53m ago•0 comments

Show HN: Baby's First Cards – real photo flash cards for toddlers

https://apps.apple.com/us/app/baby-photo-flash-cards/id6740240314
1•ailocalrecorder•1h ago•0 comments

Google API keys can remain usable for up to 23 minutes after deletion

https://www.theregister.com/devops/2026/05/21/threat-hunters-find-google-api-keys-still-usable-23...
9•logickkk1•1h ago•0 comments

Noncompetes and firm heterogeneity

https://www.aeaweb.org/articles?id=10.1257/pandp.20261076
1•hhs•1h ago•0 comments