frontpage.
newsnewestaskshowjobs

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.

Show HN: Ragleap-RAG – RAG engine that documents what it doesn't do yet

https://github.com/antonyrag/ragleap-core
1•antonyragleap•3m ago•0 comments

Show HN: ArtDeck – An infinite canvas for collecting and studying references

https://getartdeck.com/
1•artdeck•4m ago•0 comments

He Wasn't Chasing Mountains

https://hasskhan.substack.com/p/he-wasnt-chasing-mountains
1•pradeepchhetri•5m ago•0 comments

Bloody Awful Work Creeps in When Standards Are Relaxed

https://dailysceptic.org/2026/08/02/bloody-awful-work-creeps-in-when-standards-are-relaxed/
2•Michelangelo11•6m ago•0 comments

Apple says MacBook Neo is pulling schools away from Windows and Chromebooks

https://9to5mac.com/2026/08/01/apple-says-macbook-neo-is-pulling-schools-away-from-windows-and-ch...
1•gslin•6m ago•0 comments

Nifty little macOS toolbar tool to track daily usage

https://github.com/shrivara/token-bar
1•Hasaber8•7m ago•0 comments

Show HN: Gainz.fast – Local Inference, Faster

https://gainz.fast/
1•carsenk•8m ago•0 comments

Show HN: Fruman – A 2D souls-like game demo running in web browser

https://fruman.baklab.app
1•Kholin•13m ago•0 comments

OpenAI has the right strategy, but is unlucky

2•Taikhoom2010•15m ago•0 comments

Trump's AI protectionism has come for robotics

https://www.technologyreview.com/2026/08/03/1141056/trumps-ai-protectionism-has-come-for-robotics/
1•joozio•15m ago•0 comments

Show HN: lde – LuaJIT written, all in one, package manager and runtime

https://lde.sh/blog/lde-0.10.0/
4•oncelock•16m ago•1 comments

Six Problems When You Try to Build an AI Therapist

https://emilylee293105.substack.com/p/six-problems-when-you-try-to-build
2•fhe•20m ago•0 comments

C64 Demo Effects Explained: Rodents in the Attic [video]

https://www.youtube.com/watch?v=uZ1atMUOUMU
1•nikanj•21m ago•0 comments

Halloween Documents

https://en.wikipedia.org/wiki/Halloween_documents
1•sundarurfriend•22m ago•0 comments

China gets better bang for its buck than America in AI

https://www.economist.com/finance-and-economics/2026/08/03/how-china-gets-better-bang-for-its-buc...
2•pingou•27m ago•0 comments

Genkan

https://en.wikipedia.org/wiki/Genkan
1•robalni•28m ago•0 comments

Three lions die at Tokyo's Tama Zoo amid heat wave

https://www.japantimes.co.jp/news/2026/08/04/japan/science-health/heat-lion-deaths-tokyo-zoo/
1•geox•31m ago•0 comments

Show HN: Visualize in 2D/3D map any GitHub user's followers

https://github.com/ThierryRakotomanana/GitCharta
1•ThierryRakt•32m ago•0 comments

ChatTJB advertises a human-powered LLM parody on an SF billboard

https://runtimewire.com/article/chattjb-human-powered-llm-san-francisco-billboard
1•ryanmerket•34m ago•0 comments

Hiring Senior SRE

https://www.axelerant.com/careers
1•adrijadas•36m ago•0 comments

I built an open-source admin panel builder for existing databases

https://github.com/Haimanot25/dboard
2•haimanot25•39m ago•0 comments

Walking to the Car Wash: The Salience Bias of LLMs in Commonsense Reasoning

https://arxiv.org/abs/2607.28478
1•theanonymousone•41m ago•0 comments

Show HN: Generate a plan visualization with dependencies to parallelize projects

https://reloadium.com/reloadium-plans/
1•julienreszka•41m ago•0 comments

Certified Program Synthesis with a Multi-Modal Verifier [pdf]

https://verse-lab.org/papers/leetproof-ase26.pdf
1•matt_d•57m ago•0 comments

Dev tools must be opensourced thats why I build this

1•Thomeras•57m ago•0 comments

Harness Engineering for Self-Improvement

https://lilianweng.github.io/posts/2026-07-04-harness/
2•tosh•59m ago•0 comments

Show HN: ePub Translator

https://epubtranslator.net/
2•xiaoape•1h ago•0 comments

Agentty ADE: preview of agentic orchestrator is out in v0.14.0

https://github.com/agentty-xyz/agentty
2•minev-dev•1h ago•1 comments

Bubo: AI code-reviewer that learns from review comments

https://github.com/mountainowl/bubo
1•mt_owl•1h ago•1 comments

An Internet for the Solar System [video]

https://media.ccc.de/v/emf2026-45-1-an-internet-for-the-solar-system
2•anticensor•1h ago•1 comments