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.

More Than 30M Years Ago, Monkeys Rafted Across the Atlantic to South America

https://www.smithsonianmag.com/science-nature/monkeys-raft-across-atlantic-twice-180974637/
1•thunderbong•31s ago•0 comments

The Specter of Neuralese

https://www.astralcodexten.com/p/the-specter-of-neuralese
1•gmays•1m ago•0 comments

Show HN: I built an AI code editor that for teams

https://www.rxcode.dev/
1•sirily11•1m ago•0 comments

Digital Presenteeism as a Stratified Signalling System [pdf]

https://www.productivity.ac.uk/wp-content/uploads/2026/11/WP083-Digital-Presenteeism-November-202...
1•rohanm93•4m ago•0 comments

AI labs are paying $100K–$2M for internal company workflow data

https://calendly.com/d/d3ss-w9w-7vs
2•dkggwl1990•10m ago•1 comments

Joe Rogan Experience #2578 – Calley Means [video]

https://www.youtube.com/watch?v=dMBBOawFy_4
1•jakeogh•12m ago•0 comments

70% of Brits say please or thank you when speaking to AI, some even apologize

https://www.techradar.com/pro/70-percent-of-brits-say-please-or-thank-you-when-speaking-to-ai-and...
1•giuliomagnifico•15m ago•0 comments

DeepSeek Harness – Creator Mode

https://www.reddit.com/r/DeepSeek/s/sxg7aAM6rS
2•sexy_seedbox•20m ago•0 comments

We're All Idiots

https://samparr.com/were-all-idiots/
1•Olshansky•21m ago•0 comments

Have a 401(k)? Help ProPublica Investigate What's Happening to Your Money

https://www.propublica.org/getinvolved/share-401k-fees-retirement-investment
1•toomuchtodo•21m ago•0 comments

Show HN: Codend – Build and publish web apps with AI agents

https://codend.dev
1•anythic•24m ago•0 comments

British Rail Double Arrow

https://en.wikipedia.org/wiki/British_Rail_Double_Arrow
1•ggm•24m ago•0 comments

Reddit User Detects Decrease in Thinking Blocks for Opus 5.5

https://www.reddit.com/r/claude/comments/1xql02s/is_opus_55_nerfed/
1•htrp•26m ago•0 comments

Maths Genealogy Project Graph

https://mgpgraph.com/
1•Tazerenix•28m ago•1 comments

Interleaf: An experimental tool for LLM assisted knowledge exploration

https://interleaf.tulv.in/
1•atulvi•30m ago•0 comments

AI Live Speech Rewriting Patent

https://patentlyze.com/patent/microsoft-live-ai-video-rewrites-speech/
1•sweetdingoes•32m ago•0 comments

Chimera Vocal Synth

https://twitter.com/jamesonhodge/status/2118733265372860826
1•ad8e•32m ago•0 comments

Real-Time Data Streaming for Live Video

https://www.red5.net/blog/real-time-data-streaming-for-live-video/
1•mondainx•32m ago•0 comments

Modern LLMs have tiny GPTs hidden inside them

https://invertedpassion.substack.com/p/modern-llms-have-tiny-gpts-hidden
2•gmays•46m ago•1 comments

My parents chose to die together and I was left to grieve alone

https://www.bbc.com/news/articles/c81dwllzv113o
8•onemoresoop•47m ago•0 comments

Here Be Monsters

https://www.nybooks.com/online/2026/10/30/here-be-monsters/
1•petethomas•47m ago•0 comments

VindexLLM – Windows Vulkan GPU GGUF LLM inference engine written in Delphi 12

https://github.com/tinyBigGAMES/VindexLLM
1•peter_d_sherman•48m ago•0 comments

PullDash – Review pull requests in a high-performance UI, driven by keybinds

https://pulldash.com/
1•anewhnaccount2•48m ago•0 comments

Self-promotion and the Altmetric score (2021)

https://yoursay.plos.org/2021/09/self-promotion-and-the-altmetric-score/
1•enjoyyourlife•49m ago•1 comments

What It Costs to Run 4 Apps on My Own Servers

https://akashrajpurohit.com/blog/what-it-costs-to-run-4-apps-on-my-own-servers/
1•ghostfoxgod•52m ago•0 comments

Pinot Noir: inside the criminal gangs making fake wine

https://www.economist.com/interactive/2026/11/06/pinot-noir-inside-the-criminal-gangs-making-fake...
1•axiomdata316•1h ago•0 comments

How do photonic logic gates work?

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

The internet has a human slop problem, and AI is exposing it

https://www.businessinsider.com/ai-human-slop-software-security-vulnerabilities-internet-2026-10
2•ghernando•1h ago•0 comments

Indirect Change

https://herbertlui.net/indirect-change/
2•herbertl•1h ago•0 comments

Sequoia shutting down its Scouts program marks the end of an era

https://techcrunch.com/2026/11/06/sequoia-shutting-down-its-scouts-program-marks-the-end-of-an-era/
2•nlpnerd•1h ago•0 comments