frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

I built a powerful tool for YouTube Creators

https://commentscope.co/
1•sanky369•10m ago•0 comments

Stratospheric Aerosol Injection may partially mitigate marine heatwaves

https://essopenarchive.org/doi/full/10.22541/essoar.176442822.27571009/v1
2•bikenaga•12m ago•1 comments

Is Claude opus 4.5 any good?

https://www.aithings.dev/blog/is-claude-opus-45-good
2•irere123•20m ago•1 comments

Made a simple pastebin – feedback appreciated

https://fourbin.onrender.com/
1•redemption•21m ago•0 comments

Virtual ARchaeological Computer EMulator – cycle exact old x86 PC emulator

https://github.com/VARCem/VARCem
2•peter_d_sherman•22m ago•0 comments

Show HN: AI agent that rotates your passwords (browser-use and zero-knowledge)

https://thepassword.app/
2•thepasswordapp•25m ago•0 comments

Reduce Cron Email Noise with Logdiff

https://chapati.systems/reduce-cron-email-noise-with-logdiff/
2•Wronnay•29m ago•0 comments

DeepSeekMath-V2: Towards Self-Verifiable Mathematical Reasoning

https://github.com/deepseek-ai/DeepSeek-Math-V2
3•maxloh•30m ago•0 comments

Anime event in Shanghai canceled after singer's show interrupted

https://www.japantimes.co.jp/news/2025/11/29/japan/shanghai-japan-anime-event-cancel/
1•DustinEchoes•33m ago•0 comments

Unconditional self-acceptance is impossible

https://www.technotheoria.org/p/unconditional-self-acceptance-is
1•paulpauper•33m ago•0 comments

A Different Type of Dementia Is Changing What's Known About Cognitive Decline

https://www.nytimes.com/2025/11/28/health/late-dementia-alzheimers.html
3•mitchbob•35m ago•1 comments

Why Doesn't Anyone Monitor AI Consciousness? [video]

https://youtu.be/S8ttAlUXbc0
1•indigodaddy•37m ago•0 comments

Colleges Are Preparing to Self-Lobotomize

https://www.theatlantic.com/ideas/2025/11/colleges-ai-education-students/685039/
3•JumpCrisscross•39m ago•2 comments

We're learning more about what Vitamin D does to our bodies

https://www.technologyreview.com/2025/11/21/1128206/vitamin-d-bodies-bone-health-immune/
13•Brajeshwar•41m ago•1 comments

A real-time stream of anonymous human thoughts

https://cloudly.cc
2•98469056•41m ago•0 comments

Inside the deal-making power of the F-35: A weapon, a network, strategic lock-in

https://timesofindia.indiatimes.com/world/us/inside-the-deal-making-power-of-the-f-35-a-weapon-a-...
1•rustoo•41m ago•0 comments

UN Convention Against Torture–Findings Re Albania, Argentina, Bahrain and Israel

https://tbinternet.ohchr.org/_layouts/15/treatybodyexternal/SessionDetails1.aspx?SessionID=2844&L...
2•treetalker•42m ago•1 comments

How and Why You Should Cut Your Social Media Usage

https://www.samstack.io/p/how-and-why-you-should-cut-your-social
1•paulpauper•42m ago•0 comments

N.Y. Law Could Set Stage for A.I. Regulation's Next 'Big Battleground'

https://www.nytimes.com/2025/11/29/nyregion/personalized-surveillance-pricing-ai-new-york.html
1•mitchbob•43m ago•1 comments

Experts warn of growing risk of 'ChatGPT psychosis' among AI chatbot users

https://techoreon.com/ai-chatgpt-psychosis-delusion-among-chatbot-users-experts/
8•ashishgupta2209•43m ago•0 comments

Auditing Large-Scale GCP Environments with InSpec/CINC

https://www.alexbilz.com/post/2025-11-18-auditing-gcp-with-inspec/
1•ninefinger•44m ago•1 comments

I measure my cortisol from sweat using a hacked-together device

https://www.reddit.com/r/Biohackers/s/lSS8Z42oWq
2•hentrep•44m ago•0 comments

His Very Variousness: Benjamin Franklin's Experiments

https://www.lrb.co.uk/the-paper/v47/n22/ferdinand-mount/his-very-variousness
1•jger15•45m ago•0 comments

I used an algorithm to see every inch of my neighborhood

https://youtu.be/hjuu-w2sK2g
2•dbsdev•46m ago•0 comments

How much do I owe in taxes?

https://taxquarterly.com/
1•vetraxneo_apps•47m ago•2 comments

FAA issues airspace warning for the SVZM FIR

https://safeairspace.net/venezuela/
1•ByteBlaster•48m ago•0 comments

Openreview Statement Regarding API Security Incident

https://openreview.net/forum/user%7Cstatement_regarding_api_security_incident
1•sieste•52m ago•0 comments

Individual and well-being factors associated w chatbot usage: A 6-country study

https://journals.sagepub.com/doi/10.1177/02654075251392956
1•DrierCycle•52m ago•0 comments

I'm a Concert Pianist. This Is Why I Seek Imperfection.

https://www.nytimes.com/2025/11/29/opinion/pianist-music-performance-perfection.html
1•mitchbob•53m ago•1 comments

Schubfach: The smallest double-to-string implementation

https://vitaut.net/posts/2025/smallest-dtoa/
2•todsacerdoti•54m ago•0 comments