frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Gianfranco used autoresearch to fix Gumroad's flaky tests in a week

https://twitter.com/gianfrancopiana/status/2037199814694228187
1•amberj•2m ago•0 comments

Installing a Let's Encrypt TLS Certificate on a Brother Printer with Certbot

https://owltec.ca/Other/Installing+a+Let%27s+Encrypt+TLS+certificate+on+a+Brother+printer+automat...
2•8organicbits•3m ago•0 comments

Fibonacci's Composed Fractions

https://ztoz.blog/posts/fibonacci-fractions/
1•aebtebeten•3m ago•0 comments

What do I teach now?

https://prithvi081099.github.io/layerlab/2026/03/23/What-Do-I-Teach-Now.html
1•azhenley•3m ago•0 comments

What the CIA's "Queen of Torture" did next

https://www.newstatesman.com/world/americas/north-america/us/2026/03/what-the-cias-queen-of-tortu...
1•jbegley•4m ago•0 comments

Austria plans to ban social media use for under-14s, joining other countries

https://apnews.com/article/austria-government-social-media-under-14-ban-7516559412eecd9197df70ed9...
1•bookofjoe•5m ago•0 comments

Shield AI Just Raised $2B and Doubled Its Valuation in a Year

https://techlife.blog/posts/shield-ai/
1•tsenturk•6m ago•0 comments

Aria Gen 2 Glasses

https://www.projectaria.com/glasses/?_fb_noscript=1
2•haritha-j•6m ago•0 comments

Halving my game's netcode bandwidth and reworking a core mechanic

https://pablosjourneygame.com/blog/dev-log-1/
1•grgaln•6m ago•0 comments

Show HN: AIMarkdownPro – AI-powered Markdown editor for developers and writers

https://play.google.com/store/apps/details?id=com.aimarkdownpro.app&hl=en_US
1•getinfotoyou•7m ago•0 comments

Show HN: SpedFind – An open-source internet speed test (React/Python and CLI)

1•nishalk•9m ago•0 comments

New Anthropic model wrecking cybersecurity stocks

https://twitter.com/DenisGobo/status/2037524649374806059
1•minkeymaniac•9m ago•0 comments

Show HN: Built Loony for builders who want to spin up data infrastructure fast

https://loony.dev
1•maxmealing•9m ago•0 comments

Scientists watch sperm whales work as a team to assist a birth

https://www.npr.org/2026/03/27/nx-s1-5762978/sperm-whale-birth-cooperation-coordination-marine-ma...
1•Brajeshwar•10m ago•0 comments

Ask HN: What's the Deal with Forward Deployed Engineers?

1•rubyrenegade•11m ago•1 comments

Show HN: Built a reading experience for DTV by Michael Moritz

https://www.aditdaga.com/dtv
1•dagaadit•11m ago•0 comments

Nematophagous Fungus

https://en.wikipedia.org/wiki/Nematophagous_fungus
1•lordgilman•11m ago•0 comments

Football 'has eaten almost every sport' due to digital dominance

https://www.theguardian.com/football/2026/feb/25/football-digital-dominance-goalhanger-podcast-ch...
2•PaulHoule•11m ago•0 comments

MCP Authorization in 5 easy OAuth specs

https://workos.com/blog/mcp-authorization-in-5-easy-oauth-specs
1•fagnerbrack•12m ago•0 comments

The Cost of Concurrency Coordination with Jon Gjengset

https://www.youtube.com/watch?v=tND-wBBZ8RY
1•VorpalWay•13m ago•0 comments

A small experiment with multi-agent workflows for dev (forgeAI)

https://github.com/nelson820125/forgeai
3•teng0127•14m ago•2 comments

Apple Mac Pro Fades into Oblivion

https://www.cnet.com/tech/computing/apple-mac-pro-fades-into-oblivion/
1•thunderbong•15m ago•1 comments

Lebesgue Constants

https://www.johndcook.com/blog/2026/03/26/lebesgue-constants/
1•ibobev•15m ago•0 comments

So many em-dashes in Tim Cook's letter

https://www.apple.com/50-years-of-thinking-different/
1•frasermarlow•16m ago•1 comments

Handle CSV, JSON and XML with Java

https://xnacly.me/posts/2023/java-encoding/
2•ibobev•16m ago•0 comments

Git Guide for Absolute Beginners

https://xnacly.me/posts/2023/git-guide/
1•ibobev•16m ago•0 comments

Text-scale should be the default

https://www.lkhrs.com/mutex/text-scale-should-be-the-default/
1•8organicbits•16m ago•0 comments

Show HN: I have 5 PostHog projects and got tired of checking them one at a time

https://www.pockethog.com/
1•rspitzer•20m ago•0 comments

AgentID – open-source identity layer for AI agents

https://github.com/Pedroshakoor/Agent-ID
1•pedroshakoor•26m ago•1 comments

Show HN: The Economics of Builder Saturation in Digital Markets

1•armcat•26m ago•0 comments