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.

Why Submit to AI in Production: Speaking as a Tool for Better Work

https://www.r-bloggers.com/2026/01/why-submit-to-ai-in-production-speaking-as-a-tool-for-better-w...
1•7777777phil•1m ago•0 comments

AT&T Archives: The Unix Operating System (1972) [video]

https://www.youtube.com/watch?v=tc4ROCJYbm0
1•vismit2000•4m ago•0 comments

Agentic RAG for Dummies

https://github.com/GiovanniPasq/agentic-rag-for-dummies
1•thunderbong•5m ago•0 comments

Mnemonic BTC Slots

https://coinables.github.io/mnemonic-slots/#
1•nicholasbraker•5m ago•0 comments

Welcome to Niji V7

https://nijijourney.com/blog/niji-7
1•ankitg12•7m ago•0 comments

Show HN: A Spectrum Album – Structuring AI-Generated Music with Suno

https://karbeyazalbum.replit.app/
2•ersinesen•7m ago•0 comments

Accidentally making $1000 for finding Security Bugs as a Back end Developer

https://not-afraid.medium.com/accidentally-making-1000-for-finding-security-bugs-as-a-backend-dev...
1•birdculture•8m ago•0 comments

Show HN: BSS Blue Hive Guide

https://www.bluehiveguide.com/index.html
1•andy846851797•9m ago•0 comments

Git Show

https://tonystr.net/blog/git
1•TonyStr•10m ago•0 comments

Show HN: LLM fine-tuning without infra or ML expertise

https://www.tinytune.xyz/
1•Jacques2Marais•10m ago•0 comments

Ask HN: How do you manage your morning catch-up routine?

2•Peterz_shu•14m ago•1 comments

Show HN: I built an enterprise weather intelligence platform with Lovable

https://preview--chrono-strata.lovable.app/shop
2•lavandar-admin•17m ago•0 comments

From 75% to 99.6%: The Math of LLM Ensembles

https://www.shibaprasadb.com/2026/01/20/llm-ensemble.html
2•bluebirdfirewin•17m ago•0 comments

Remote Authentication By-Pass in Telnetd (2026)

https://seclists.org/oss-sec/2026/q1/89
2•faebi•18m ago•0 comments

Community-driven peer review: platform to review, rate, access research freely

https://test.scicommons.org/
3•DrSAR•19m ago•0 comments

Show HN: Encrypter.site – Browser-based E2E encryption for text and files

https://www.encrypter.site/
2•zealer•19m ago•0 comments

The UK government is backing AI that can run its own lab experiments

https://www.technologyreview.com/2026/01/20/1131462/the-uk-government-is-backing-ai-scientists-th...
2•Brajeshwar•20m ago•0 comments

Can you slim macOS down?

https://eclecticlight.co/2026/01/21/can-you-slim-macos-down/
3•ingve•20m ago•0 comments

Anthropic's CEO stuns Davos with Nvidia criticism

https://techcrunch.com/2026/01/20/anthropics-ceo-stuns-davos-with-nvidia-criticism/
3•taubek•25m ago•1 comments

Show HN: Kerns – Research that compounds instead of resetting

https://www.kerns.ai/
2•kanodiaayush•25m ago•0 comments

Rakoon – privacy first, offline first workout tracker

https://apps.apple.com/us/app/rakoon-gym-workout-tracker/id6757954467
2•palooka•28m ago•0 comments

Exploring the role Delaware plays as a domestic tax haven (2013)

https://www.sciencedirect.com/science/article/abs/pii/S0304405X13000056
2•walterbell•31m ago•0 comments

EU Parliament freezes US trade deal ratification

https://www.lemonde.fr/en/international/article/2026/01/20/eu-parliament-freezes-us-trade-deal-af...
6•pjmlp•35m ago•0 comments

Hyve – Parallel isolated workspaces for AI coding agents and multi-repo dev

1•eladkishon•36m ago•0 comments

In Bangladesh, volunteers are battling climate-fueled disease at its source

https://grist.org/health/bangladesh-clean-volunteers-dhaka-mosquitoes/
1•Brajeshwar•41m ago•0 comments

Scottrade Is Back – The 80s Legend Revived with AI Power, 100% Free (For Now)

https://scottrade.net/
1•Veryyy•44m ago•0 comments

Show HN: Replacing ComfyUI node graphs with YAML-defined DAGs

https://github.com/jaskirat05/OpenHiggs
2•jaskirat05•44m ago•0 comments

Show HN: CLI tool to clean up dependency directories across all your projects

https://github.com/Kuneosu/dep-clean
1•kuneosu•44m ago•0 comments

Graphics APIs: Yesterday, Today, and Tomorrow

https://asawicki.info/articles/graphics_apis_yesterday_today_tomorrow_en.php
2•ingve•45m ago•1 comments

AI Writes Python Code, but Maintaining It Is Still Your Job

https://www.kdnuggets.com/ai-writes-python-code-but-maintaining-it-is-still-your-job
1•eigen_code•46m ago•0 comments