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.

Why Colonize Space: The Need for Frontier

https://medium.com/@darkft/why-colonize-space-the-need-for-frontier-dd03c443a2f3
1•d_silin•33s ago•0 comments

Hydra Joins Supabase

https://supabase.com/blog/hydra-joins-supabase
1•rottencupcakes•1m ago•0 comments

CBP Signs Clearview AI Deal to Use Face Recognition for 'Tactical Targeting'

https://www.wired.com/story/cbp-signs-clearview-ai-deal-to-use-face-recognition-for-tactical-targ...
1•laurex•2m ago•0 comments

A piece of code that causes LLVM Flang to generate NaN/Inf randomly

https://github.com/llvm/llvm-project/issues/180957
1•zaikunzhang•2m ago•0 comments

Airspace closure followed spat over drone-related tests and balloon shoot-down

https://www.cbsnews.com/news/airspace-closure-followed-spat-over-drone-related-tests-and-party-ba...
1•mhb•3m ago•0 comments

NetNewsWire Turns 23

https://netnewswire.blog/2026/02/11/netnewswire-turns.html
2•robin_reala•3m ago•0 comments

MolmoSpaces: A large-scale, open platform and benchmark for embodied AI research

https://allenai.org/blog/molmospaces
1•maxloh•3m ago•1 comments

Show HN: Stop Getting Rejected by ATS – I Built a Fix

https://arzunocv.site
1•common_creator•4m ago•1 comments

Spotify-fs Store any file inside Spotify tracks

https://github.com/Xelckis/spotify-fs
2•delduca•7m ago•0 comments

Show HN: Gottp – A Postman/Insomnia-Like TUI API Client Built in Go

https://github.com/sadopc/gottp
2•sadopc•8m ago•0 comments

Migrating from Slurm to Kubernetes

https://blog.skypilot.co/slurm-to-k8s-migration/
4•rombr•8m ago•0 comments

Evolving Git for the next decade (FOSDEM 2026)

https://lwn.net/SubscriberLink/1057561/040a5b0283517773/
2•chmaynard•9m ago•0 comments

Lightweight daemon to remap the Copilot keyboard key in Linux using libevdev

https://github.com/m-bartlett/remap-copilot
1•evah•10m ago•0 comments

We built a museum exhibit about a 1990s game hint line, with a physical binder

https://yarnspinner.dev/blog/hint-line-93/
1•PaulHoule•11m ago•0 comments

EU commission eyes turning 5G antennas into drone detectors

https://www.euractiv.com/news/commission-eyes-turning-5g-antennas-into-drone-detectors/
1•giuliomagnifico•13m ago•0 comments

Maxis Software Toys

https://arbesman.substack.com/p/maxis-software-toys
1•arbesman•13m ago•0 comments

SEO Score for Your Docs

https://docsalot.dev/tools/docs-seo
1•fazkan•13m ago•0 comments

Interactive guide to Bitcoin's proof of work

https://bennet.org/learn/proof-of-work-what-bitcoin-mining-really-does/
2•tombennet•14m ago•1 comments

Show HN: TagLib-WASM – Read/write audio metadata with all JavaScript runtimes

1•CharlesW•14m ago•0 comments

Talk to Proteins

https://www.codyliu.com/chatmol
1•codyyyyliu•14m ago•0 comments

Opus 4.6, Codex 5.3, and the post-benchmark era

https://www.interconnects.ai/p/opus-46-vs-codex-53
1•gmays•15m ago•0 comments

Andreessen Horowitz's Rising Influence over Trump-Era AI Policy

https://www.bloomberg.com/news/features/2026-02-10/trump-s-ai-policy-shaped-by-vc-tech-giant-andr...
1•atlasunshrugged•16m ago•0 comments

Challenger Center announces Space Coding Challenges with Hack Club

https://challenger.org/news-insights/new-partnership-with-hack-club-launches-space-coding-challen...
1•Charmunk•16m ago•0 comments

The hunt for zero-CVE container images

https://thenewstack.io/chainguard-and-the-hunt-for-truly-zero-cve-container-images/
2•CrankyBear•17m ago•0 comments

Old Reddit Broken

https://old.reddit.com/r/help/comments/1r1fde4/is_old_reddit_super_super_broken_right_now_for/
2•amai•18m ago•0 comments

Turning YouTube into Cloud Storage [video]

https://www.youtube.com/watch?v=l03Os5uwWmk
1•f311a•18m ago•0 comments

Gallup will no longer measure presidential approval after 88 years

https://thehill.com/homenews/media/5733236-gallup-stops-presidential-approval-ratings-polls/
16•hypeatei•19m ago•3 comments

Show HN: Simulate Anybody's Gmail Inbox

https://unread.ooo/
1•theroo•19m ago•0 comments

Show HN: Steam and Autism, a book by Opus 4.6

https://github.com/cloudstreet-dev/STEAM-and-Autism
1•DavidCanHelp•20m ago•1 comments

Are we losing our sense of "Quality" in the age of AI agents

https://mcradcliffe.substack.com/p/zen-and-the-art-of-hand-written-code
1•bigpapikite•20m ago•0 comments