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.

The Problem Is Culture

https://deadsimpletech.com/blog/the_problem_is_culture
1•logicprog•38s ago•1 comments

Suicides Were Frequent at the Golden Gate Bridge. Not Anymore

https://www.nytimes.com/2026/01/20/us/golden-gate-bridge-suicides.html
1•mhb•1m ago•0 comments

Show HN: We built Power Apply at night to survive the 9 to 5

https://powerapply.ai/
1•inesbarros1•1m ago•0 comments

Ask HN: At what point does adding AI slow a product down?

1•kajolshah_bt•3m ago•1 comments

Show HN: XFolder – a fast macOS file manager with multi-pane views

https://github.com/zebrapixel/XFolder/releases
1•dreampixel•4m ago•0 comments

Show HN: Dev visibility for non-technical founders

1•inferno22•4m ago•1 comments

Structural Plasticity in AI Agents: What AI systems can learn from neurobiology

https://augmentedperspectives.substack.com/p/structural-plasticity-in-agents
1•ap_aditipriya•5m ago•1 comments

Ireland wants to give its cops spyware, ability to crack encrypted messages

https://www.theregister.com/2026/01/21/ireland_wants_to_give_police/
1•jjgreen•7m ago•0 comments

From Rock to Tech, Talent Flees Taxes

https://chroniclesmagazine.org/web/from-rock-to-tech-talent-flees-taxes/
1•RickJWagner•10m ago•0 comments

2 years building a kids audio app as a solo dev – lessons learned

2•oliverjanssen•11m ago•0 comments

Rewilding Software Engineering: Myths We Tell Ourselves

https://medium.com/feenk/rewilding-software-engineering-ca3ad1e612d8
1•flail•11m ago•0 comments

Why did medieval people invent so many collective nouns? (2023)

https://weirdmedievalguys.substack.com/p/why-did-medieval-people-invent-so
2•crescit_eundo•12m ago•0 comments

Good climate news: thawing permafrost turns out to be a methane sink

https://www.nature.com/articles/s43247-025-02765-5
2•bilsbie•13m ago•0 comments

Jeffrey Snover, creator of PowerShell, retires

https://twitter.com/jsnover/status/2013932110394904586
1•emot•13m ago•0 comments

Denmark is 'irrelevant,' says US Treasury Secretary

https://www.politico.eu/article/denmark-irrelevant-us-donald-trump-treasury-chief-scott-bessent/
4•jmsflknr•13m ago•0 comments

Show HN: GenAI Prompts as "Native" Programs

https://promptcmd.sh/
1•tgalal•13m ago•0 comments

Weight-Loss Drugs Could Save U.S. Airlines $580M per Year

https://www.nytimes.com/2026/01/19/travel/airlines-weight-loss-drugs.html
1•ilamont•13m ago•0 comments

Why medieval artists drew ancient Romans in medieval clothes (2023)

https://weirdmedievalguys.substack.com/p/why-medieval-artists-drew-ancient
1•crescit_eundo•13m ago•0 comments

AI Destroys Institutions

https://cyberlaw.stanford.edu/publications/how-ai-destroys-institutions/
10•JeanKage•17m ago•4 comments

Show HN: Threadyx – BYOK multi-agent AI coding platform

https://code-agent-frontend-production.up.railway.app/
1•threadyx•17m ago•1 comments

I made AI earphones remember everything (auto-sync to Obsidian)

1•Paddyz•18m ago•0 comments

Ask HN: What are the metrics for "AI-generated technical debt"?

1•willj•18m ago•0 comments

Show HN: I built a narrative game about running a thrift shop

https://store.steampowered.com/app/2961120/Shop_Crush/
3•hollowlimb•19m ago•0 comments

Meta's new AI team delivered first key models internally this month

https://www.reuters.com/technology/metas-new-ai-team-has-delivered-first-key-models-internally-th...
1•geox•22m ago•0 comments

Why AI Agents Increase External AI Reliance

https://www.aivojournal.org/why-ai-agents-increase-external-ai-reliance/
1•businessmate•22m ago•1 comments

Veronika the cow astounds science with first consistent case of tool use

https://english.elpais.com/science-tech/2026-01-20/veronika-the-cow-astounds-science-with-first-c...
1•t-3•23m ago•0 comments

New H-1B Rules Hurt Tech Companies 3x More Than the Staffing Firms [OC]

https://theh1brecords.substack.com/p/analysis-of-517874-petitions-reveals
3•codebyaditya•24m ago•1 comments

Weaponizing Calendar Invites: A Semantic Attack on Google Gemini

https://www.miggo.io/post/weaponizing-calendar-invites-a-semantic-attack-on-google-gemini
1•thunderbong•24m ago•0 comments

Show HN: Vib-OS, vibecoded a operating system

https://github.com/viralcode/vib-OS
4•viralcode•24m ago•2 comments

The Power of the Powerless [pdf]

https://www.nonviolent-conflict.org/wp-content/uploads/1979/01/the-power-of-the-powerless.pdf
1•andsoitis•24m ago•0 comments