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.

Show HN: Lelu – gate OpenAI agent actions on confidence and prompt injection

https://github.com/Lelu-ai/lelu
1•abeni1990•23s ago•0 comments

The State of GEO Readiness 2026: 100 B2B brands across AI search engines

https://getgeoscan.ai/en/blog/state-of-geo-readiness-2026
1•jrame•40s ago•0 comments

Musk loses trillionaire status as global tech rout hits SpaceX

https://www.bbc.co.uk/news/articles/c8j2m2p8dgmo
1•zh3•2m ago•0 comments

Google set to lose two more AI researchers to Anthropic

https://www.bloomberg.com/news/articles/2026-06-24/google-poised-to-lose-two-more-high-profile-ai...
2•isomorphic_duck•3m ago•1 comments

Developers can overcome AI FOMO

https://old.reddit.com/r/LLMDevs/comments/1ueloyp/i_am_an_indie_dev_and_i_published_the_article_ai/
2•gamescodedogs•3m ago•0 comments

Show HN: Why AI Agents Fail at API Calls in Production (and How to Fix It)

https://twitter.com/Chaitralikakde/status/2064365706679144842
1•chaitralikakde•4m ago•0 comments

Calculating History

https://sites.google.com/site/calculatinghistory/home
1•bryanrasmussen•4m ago•0 comments

Megalomania

https://en.wikipedia.org/wiki/Megalomania
1•rolph•7m ago•0 comments

Who is the Rockefeller of AI and which industry will they transform?

https://constantlyrandom.substack.com/p/history-rhymes
1•go_kool•7m ago•0 comments

Harry Edwood

https://www.lambiek.net/artists/e/edwood_harry.htm
1•thunderbong•8m ago•0 comments

Why should we teach kids to learn how to code in the age of AI [pdf]

https://static.raspberrypi.org/files/about/Why-kids-still-need-to-learn-to-code-in-the-age-of-AI-...
1•Muhammad523•9m ago•0 comments

Amazon Seller Reveals Rare Glimpse of Shadow Bribery Market

https://www.bloomberg.com/news/articles/2026-06-24/inside-the-shadow-market-selling-access-to-ama...
2•1vuio0pswjnm7•9m ago•0 comments

Why Current AI Guardrails Train Models to Fake Alignment

https://kellyasay.substack.com/p/the-prisoners-paradox-how-adversarial
2•kellya•11m ago•0 comments

Wikipedia Workers to Seek Union Recognition

https://www.cwu.org/press_release/wikipedia-workers-to-seek-union-recognition/
3•addshore•12m ago•0 comments

GPT-Image 2 in Codex Workflows

https://twitter.com/RandyHaddad6/status/2069842784106971341
2•WhySee1468•13m ago•2 comments

Expanded billing choice and lower fees on Google Play

https://android-developers.googleblog.com/2026/06/play-expanded-billing.html
1•ganlaw•13m ago•0 comments

How to Get Your First 10 Customers

https://www.ycombinator.com/library/SF-how-to-get-your-first-10-customers
4•aurenvale•15m ago•1 comments

Mega-funds grab 72% all capital raised, VC's haves, have-nots gap widens

https://fortune.com/2026/06/24/the-spacex-ipo-marks-a-lopsided-win-for-venture/
4•1vuio0pswjnm7•15m ago•1 comments

Filaxy Herald – Turn GitHub activity into build-in-public posts

https://github.com/othmarodev/filaxy-herald
1•othmarodev•16m ago•0 comments

GitHub joins coalition advocating for fixes to California AI Transparency Act

https://github.blog/news-insights/policy-news-and-insights/github-joins-coalition-advocating-for-...
1•geogirl1997•17m ago•0 comments

Requesting wildcard SSL certificates via DNS with Sympl

https://dracos.co.uk/wrote/dehydrated/
1•speckx•17m ago•0 comments

Linkerd 2.20: Rate-limit-aware load balancing, reduced memory usage and more

https://www.buoyant.io/blog/bel-2-20-automated-trust-anchor-rotation-windows-vm-support-rate-limi...
1•darksoul•17m ago•0 comments

Show HN: Accent Roulette – guess a stranger's country from their voice

https://accent-roulette.com
1•townload•18m ago•0 comments

What are peoples' biggest issues with Crypto?

1•arifromjelly•19m ago•3 comments

ESA's Euclid captures the Milky Way's crowded heart

https://www.esa.int/Science_Exploration/Space_Science/Euclid/ESA_s_Euclid_captures_the_Milky_Way_...
3•robin_reala•20m ago•0 comments

The Fastest Python Struct?

https://www.crumpledpaper.tech/2026-06-21-python-struct-profiling/
1•lumpa•21m ago•0 comments

Head to Head: Anthropic: Claude Opus 4.8 vs. Google: Gemini 3.5 Flash

https://runtimewire.com/article/head-to-head-anthropic-claude-opus-4-8-vs-google-gemini-3-5-flash
2•ryanmerket•23m ago•0 comments

The Modular Monolith in Rails: Engines, Packwerk and Boundaries

https://davidslv.uk/modular-monolith-rails/
1•davidslv•24m ago•0 comments

Github Copilot: Changes to model selection for Free and Student plans

https://github.blog/changelog/2026-06-24-changes-to-model-selection-for-free-and-student-plans/
1•zorrn•25m ago•0 comments

How to Tell If Your Kafka Self-Service Is Working?

https://medium.com/conduktor/how-to-tell-if-your-kafka-self-service-is-actually-working-361cef450b81
1•chtefi•25m ago•0 comments