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.

My falling-out with the rationalist community

https://lcamtuf.substack.com/p/my-falling-out-with-the-rationalist
1•surprisetalk•3m ago•0 comments

Show HN: Velprium Workspace

https://www.velprium.com/
1•mjkl7896•3m ago•0 comments

Mysterious little red dots at the beginning time may have an explanation

https://www.livescience.com/space/mysterious-little-red-dots-at-the-beginning-time-may-finally-ha...
1•arto•5m ago•0 comments

Ramp AI Router

https://router-website-ramp.vercel.app/router
1•robbiet480•6m ago•1 comments

Poverty of attention. Text became free. Attention didn't. (German)

https://christopher-helm.com/aufmerksamkeit-ueberfischung-ki-journalismus/
1•chelm•7m ago•0 comments

Repos in 29 days with an AI pipeline – what broke wasn't code

https://lkforge.com/blog/26-repos-in-29-days/
1•lucianBKR•7m ago•0 comments

US judge approves Anthropic's $1.5B settlement of copyright lawsuit

https://www.reuters.com/world/us-judge-approves-anthropics-15-billion-settlement-copyright-lawsui...
1•Anon84•9m ago•0 comments

Free LLM balancer combines multiple local inference machines with cloud fallback

https://github.com/Gysho/LLMrPro
1•ThomasWink1981•9m ago•1 comments

DeskPad – Virtual Keyboard and Mouse for Android Desktop Mode

https://github.com/auxiliaryutils/deskpad-app
1•Nemeses1•10m ago•1 comments

Confront Reality (2012)

http://www.aaronsw.com/weblog/anders
2•waaldev•10m ago•0 comments

An Ode to the Old Ways

https://agniva.me/personal/2026/07/19/ode-to-old-ways.html
1•sangeeth96•11m ago•0 comments

Richard Hamming: how one frustrated mathematician made data repair itself

https://nirmalutwani.substack.com/p/when-bits-flip-and-everything-breaks
1•nutwani91•11m ago•1 comments

Did California's DMV Director lie to the legislature?

https://papersplease.org/wp/2026/07/01/did-californias-dmv-director-lie-to-the-legislature/
1•Jimmc414•11m ago•0 comments

New Orleans Cops Published Policy Document Allowing Weaponized Drones

https://www.404media.co/new-orleans-cops-published-policy-document-allowing-weaponized-drones/
2•Jimmc414•12m ago•0 comments

The depth problem with agentic research

https://moekhalil.substack.com/p/the-depth-problem-with-agentic-research
1•mfkhalil•13m ago•0 comments

Trump imposes 50% tariff on Canadian goods

https://apnews.com/article/trump-canada-50-tariff-autos-alcohol-cheese-ca268ef24992b16695954c4c2e...
1•bradleyankrom•14m ago•1 comments

AI Voice Phishing Performs on Par with Human Scammers at a Fraction of the Cost

https://fredheiding.substack.com/p/ai-voice-phishing-performs-on-par
1•DalasNoin•15m ago•0 comments

Stealth Crawlers Are Not a Threat to the Open Web. Bills Targeting Them Would Be

https://www.eff.org/deeplinks/2026/07/stealth-crawlers-are-not-threat-open-web-bills-targeting-th...
2•hn_acker•15m ago•0 comments

Inference as Stereotype

https://hollisrobbinsanecdotal.substack.com/p/stereotype-threat
1•HR01•16m ago•0 comments

Does "rtk" skill cut agent tokens by 60–90%? We tested it

https://blog.jetbrains.com/ai/2026/07/rtk-claude-code-token-savings/
1•kindaAnIdiot•17m ago•0 comments

D-FINE-seg – detection, instance and semantic segmentation in one model

https://github.com/ArgoHA/D-FINE-seg
1•argo_cve•18m ago•0 comments

Cognition acquires TierZero to extend Devin into production operations

https://runtimewire.com/article/cognition-acquires-tierzero-devin-production-operations
1•ryanmerket•19m ago•0 comments

Logical Foundations of Prolog

https://www.youtube.com/playlist?list=PLEvH6T-1oh76mgfZm5QbLQoRsyr87BhiR
2•b-man•19m ago•0 comments

Aipac spends millions in Michigan Senate race – AP News

https://apnews.com/article/aipac-michigan-senate-stevens-elsayed-588bb869eb557643950bf68d6a0d7a10
4•abdelhousni•22m ago•0 comments

Show HN: Forall – OpenSpec but with actual machine-checkable verification

https://github.com/astrio-labs/forall
1•Nolan_Lwin•22m ago•0 comments

Trump to hit Canada with new 50% tariff

https://www.cbc.ca/news/world/trump-canada-us-trade-tariffs-cusma-usmca-9.7276973
3•llm_nerd•23m ago•0 comments

Ramp opens AI model router, says it cut internal LLM costs 30%

https://runtimewire.com/article/ramp-router-ai-model-gateway-llm-costs
1•ryanmerket•25m ago•0 comments

Esoterica: Prototype Game Engine

https://github.com/BobbyAnguelov/Esoterica
2•wertyk•26m ago•0 comments

The Artisanal RPG Culture of Classic Fallout Modding

https://tripharrison.substack.com/p/the-artisanal-rpg-culture-of-classic
3•droptablemain•27m ago•0 comments

Own Your Weights

https://twitter.com/jaminball/status/2075602206099919159
2•gmays•27m ago•0 comments