frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Claude knows who you are

https://www.patrickstevens.co.uk/posts/2026-04-18-claude-knows-you/
1•Smaug123•4m ago•0 comments

Shared-memory persistent data structures for ClojureScript and Clojure

https://github.com/SeniorCareMarket/eve
1•PaulHoule•6m ago•0 comments

Show HN: Chlibc – A ptrace-based tool to hot-swap glibc and interp in user-space

https://github.com/gzm55/chlibc
1•gzm55•7m ago•0 comments

Metallic Microlattice

https://en.wikipedia.org/wiki/Metallic_microlattice
1•ZeljkoS•10m ago•0 comments

GNOME GitLab Git traffic caching

https://www.dragonsreach.it/2026/04/17/gnome-gitlab-git-pulls-caching-improvements/
11•JNRowe•13m ago•0 comments

The Quiet Coup: How AI Is Rewriting Power, Wealth, and Human Agency

https://neerajkarimpuzha.wordpress.com/2026/04/18/293/
2•neeraj_r•18m ago•0 comments

Fixing DNS tail latency with a 5-line config and a 50-line function

https://numa.rs/blog/posts/fixing-doh-tail-latency.html
2•fanf2•18m ago•0 comments

Biangbiang Noodles

https://en.wikipedia.org/wiki/Biangbiang_noodles
1•thunderbong•20m ago•0 comments

China humanoid robot half-marathon to showcase technical leaps

https://www.reuters.com/world/asia-pacific/china-humanoid-robot-half-marathon-showcase-technical-...
3•JumpCrisscross•24m ago•0 comments

A brief history of C/C++ programming languages

https://lemire.me/blog/2026/04/09/a-brief-history-of-c-c-programming-languages/
1•signa11•25m ago•0 comments

Cannabis may make you remember things that never happened

https://www.nationalgeographic.com/health/article/how-cannabis-affects-memory-thc-false-recall
2•johntfella•30m ago•0 comments

Anthropic decided to shut down our organization for an alleged violation

https://twitter.com/patomolina/status/2045281665363386504
1•isolli•30m ago•1 comments

Ask HN: How do small startups, solo/lean HR agencies manage hiring pipeline?

1•kathir05•34m ago•0 comments

Show HN: I can't write Python. It works anyway

https://github.com/Wewoc/Garmin_Local_Archive
1•Wewoc•35m ago•0 comments

Laimark – 8B LLM that self-improves. Consumer GPU

https://github.com/seetrex-ai/laimark
2•jesustabares•42m ago•0 comments

Peter Thiel Is Launching an "AI Ministry of Truth" Called Objection

https://old.reddit.com/r/antiai/comments/1sngw6f/peter_thiel_is_launching_an_ai_ministry_of_truth/
4•doener•49m ago•0 comments

Men caught competing in women's category of prestigious South African marathon

https://www.cnn.com/2026/04/17/sport/men-found-womens-category-sa-marathon-intl-scli
1•breve•50m ago•0 comments

Grok TTS and STT APIs

https://x.ai/news/grok-stt-and-tts-apis
2•chopete3•50m ago•1 comments

BibCrit – LLM grounded in ETCBC corpus data for Biblical textual criticism

https://github.com/Jossifresben/BibCrit
1•jossifresben•55m ago•0 comments

Long Covid Diagnostic Out of Stanford

https://join.muno.bio/
2•limalabs•59m ago•0 comments

Forsp: A Forth+Lisp hybrid lambda calculus language (2024)

https://xorvoid.com/forsp.html
1•HeliumHydride•1h ago•0 comments

The Art of the Fictional Pop Song

https://www.newyorker.com/culture/pop-music/the-art-of-the-fictional-pop-song
2•fortran77•1h ago•0 comments

America Lost the Mandate of Heaven

https://geohot.github.io//blog/jekyll/update/2026/04/18/america-mandate-of-heaven.html
3•mefengl•1h ago•1 comments

Claude Opus wrote a Chrome exploit for $2,283

https://www.theregister.com/2026/04/17/claude_opus_wrote_chrome_exploit/
5•Mohansrk•1h ago•0 comments

Purdue University CS240 Class over 50% of students 'caught' using AI on homework

https://old.reddit.com/r/Purdue/comments/1sogfb4/comment/ogsvymy/
1•twaldin•1h ago•2 comments

Unweight: Lossless MLP Weight Compression for LLM Inference

https://research.cloudflare.com/nikulin2026/
2•jgrahamc•1h ago•0 comments

Helpmate-Live, Social and AI Chat with Built-In CRM for WordPress

1•RhapsodyPlugins•1h ago•0 comments

Show HN: A delivery gate that automatically releases files when invoice is paid

1•pixelatedRudy•1h ago•1 comments

GloraMD Face Lift Serum

https://www.facebook.com/GloraMDFaceLiftSerumUS
1•bbangerr•1h ago•0 comments

Show HN: I made a self-employed expense keeper

https://bizlect.com
1•ispaceman•1h ago•0 comments