frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Lies Screenwriters Tell Themselves About AI

https://elliotgrove.substack.com/p/7-lies-screenwriters-tell-themselves
1•matthewsinclair•4m ago•0 comments

Show HN: JavaScript interpreter that pauses and explains code for learners

https://www.codesteps.dev/learn-javascript/editor?s=p6klVe
1•maujood•11m ago•0 comments

Spec-Driven Development with AI

https://github.blog/ai-and-ml/generative-ai/spec-driven-development-with-ai-get-started-with-a-ne...
1•ekosz•15m ago•0 comments

AI Is Intensifying a 'Collapse' of Trust Online, Experts Say

https://www.nbcnews.com/tech/tech-news/experts-warn-collapse-trust-online-ai-deepfakes-venezuela-...
3•m463•20m ago•0 comments

Public Money Public Code

https://publiccode.eu/en/
2•pabs3•20m ago•0 comments

Autofocusing Smart Glasses with Eye Tracking Tech Could Make Bifocals Obsolete

https://hothardware.com/news/autofocusing-smart-glasses-with-eye-tracking-tech-could-make-bifocal...
2•WaitWaitWha•22m ago•0 comments

2. Title: Show HN: First Modern Nvidia Driver for IBM POWER8 via OCuLink

https://github.com/Scottcjn/nvidia-power8-patches
1•AutoJanitor•26m ago•0 comments

Show HN: A Base44-style builder for data workflows (DuckDB-powered)

https://www.gptbeyond.com/try
1•vibecoding101•29m ago•0 comments

An mRNA Shot Turned Back the Clock in Mice

https://singularityhub.com/2026/01/08/aging-weakens-immunity-an-mrna-shot-turned-back-the-clock-i...
1•WaitWaitWha•29m ago•1 comments

It's Your Job to Keep Your Secrets

https://www.overcomingbias.com/p/its-your-job-to-keep-your-secrets
1•paulpauper•31m ago•0 comments

The Permanent Emergency

https://www.astralcodexten.com/p/the-permanent-emergency
1•paulpauper•32m ago•0 comments

How I Learned to Love Reading

https://www.honest-broker.com/p/how-i-learned-to-love-reading
1•paulpauper•33m ago•0 comments

MIT Whirlwind I: A High-Speed Electronic Digital Computer (1951)

https://dome.mit.edu/bitstream/handle/1721.3/40245/MC665_r12_R-209.pdf?sequence=1&isAllowed=y
1•stmw•38m ago•1 comments

Freelance Contract Management Software

https://www.plotform.cc/
1•abdullah9•40m ago•0 comments

Gut bacteria may drive bipolar depression by influencing brain connectivity

https://medicalxpress.com/news/2025-12-gut-bacteria-play-role-bipolar.html
1•PaulHoule•41m ago•0 comments

Using AI, Mathematicians Find Hidden Glitches in Fluid Equations

https://www.quantamagazine.org/using-ai-mathematicians-find-hidden-glitches-in-fluid-equations-20...
1•pseudolus•42m ago•0 comments

Bytelocker Emacs Port

https://github.com/abaj8494/bytelocker.el
1•br1ttle•42m ago•1 comments

X Sues Music Publishers over "Weaponized" DMCA Takedown Conspiracy

https://torrentfreak.com/x-sues-music-publishers-over-weaponized-dmca-takedown-conspiracy/
6•gslin•43m ago•0 comments

The ethics of wanting someone who is taken

https://docs.google.com/forms/d/e/1FAIpQLSc2V2YQc0RssBA_4j2Gh0Pb4AT8cdHtPP3sOatGg9Ide0uvjg/viewfo...
1•tavro•47m ago•2 comments

Maine's black market for baby eels is spawning a crime-thriller subgenre

https://www.pressherald.com/2025/09/09/maines-black-market-for-baby-eels-is-spawning-a-crime-thri...
2•noleary•48m ago•1 comments

USDA suspends federal financial awards to Minnesota and Minneapolis

https://turnto10.com/news/nation-world/enough-is-enough-usda-suspends-federal-financial-awards-to...
5•blurbleblurble•55m ago•4 comments

Show HN: I built a Postgres GUI in Swift because existing tools felt bloated

https://postgresgui.com
1•fikrigha•59m ago•0 comments

Scaffold – Add AI features to any site, no API keys or back end

https://www.scaffoldtool.com/
1•niqhtcrawler•1h ago•1 comments

Rust Is Perfectly Imperfect

http://0x80.pl/notesen/2026-01-08-imperfect-rust.html
1•orlp•1h ago•1 comments

AI Won't Kill Open Source – It Will Amplify It

https://petabridge.com/blog/ai-wont-kill-open-source/
1•Aaronontheweb•1h ago•0 comments

Show HN: Build your own Atlas/Comet AI-browser (open source)

https://github.com/tomkit/chromium-my-assistant
1•tomkit•1h ago•0 comments

Americans Do Not Need a Left or Right Revolution

https://www.grumpychineseguy.com/p/americans-do-not-need-a-left-or-right
3•metadope•1h ago•3 comments

AI as the Engine of Application State

https://jonwoodlief.com/ai-app-state.html
1•jonfw•1h ago•0 comments

Show HN: A Constitutional Framework for Ethical AI Decision-Making

https://github.com/SebastFock/Sovereign-Engagement
1•StrategicEthos•1h ago•0 comments

Show HN: Ollie – Glass-box AI code editor with local models and no subscription

https://costa-and-associates.com/ollie
1•lcmeyer•1h ago•1 comments