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.

Developing iOS Shortcuts and Apps

https://mythofechelon.co.uk/blog/2026/08/17/developing-ios-shortcuts-and-apps
1•tentacleuno•1m ago•0 comments

Show HN: A Pi extension to monitor your quota for OpenCode GO et CommandCode

https://github.com/Anhydrite/pi-quota-monitoring
1•anhydrite•2m ago•0 comments

The Naked Truth (2021)

https://pudding.cool/2021/03/foundation-names/
1•Tomte•2m ago•0 comments

Stolpersteine

https://www.stolpersteine.eu/en/the-art-memorial/stolpersteine
1•Tomte•3m ago•0 comments

Ghps: A Minimal GitHub Pages Simulator for Local Development

https://pypi.org/project/ghps/
1•rankorn•3m ago•0 comments

Running Modern LLMs on a 2GB GPU

https://medium.com/@therealcomtom/running-modern-llms-on-a-2gb-gpu-517512813bb0
1•comtom•4m ago•0 comments

Fully Automated Luxury Communism(2015)

https://www.theguardian.com/sustainable-business/2015/mar/18/fully-automated-luxury-communism-rob...
1•o4c•4m ago•0 comments

Show HN: Catch AI code hallucinations without asking a model

https://github.com/lovettsendit/hedgemony
1•lovettsendit•5m ago•0 comments

Connect Your AI Agent to a Remote MCP Server

https://quickchat.ai/post/connect-ai-agent-to-mcp-server
1•piotrgrudzien•5m ago•0 comments

Deployah, Spec-to-Release for Kubernetes

https://deployah.dev/
2•gopherly•7m ago•0 comments

Run multiple Linux kernels without hypervisor

https://lore.kernel.org/lkml/ao34RJ7aZ2BLd67S@pop-os.localdomain/
3•hasley•8m ago•0 comments

Show HN: No Rush – Ambient places that run on the real local time and weather

https://no-rush.app/
1•bearjb•8m ago•0 comments

GitHub Copilot reviews cost creep

https://github.blog/changelog/2026-08-28-upcoming-changes-to-github-copilot-policies-and-billing/
1•whirlwin•14m ago•1 comments

Innovation Doesn't Happen on Schedule

https://www.thesignalist.io/s/innovation-doesnt-happen-on-schedule/
1•cirio•14m ago•0 comments

The Age of Average

https://www.alexmurrell.co.uk/articles/the-age-of-average
1•dwrodri•19m ago•0 comments

Show HN: Schist – the open source photo editor that feels nice to use

https://schist.app/
1•astrid__•19m ago•0 comments

Study: Blue light impairs the eye's ability to distinguish fine detail most

https://research.uga.edu/news/blue-light-has-a-surprising-effect-on-your-eyes-study-finds/
1•giuliomagnifico•20m ago•0 comments

Advanced AI threatens global financial stability, says Bank of England boss

https://www.theguardian.com/business/2026/aug/31/advanced-frontier-ai-financial-stability-andrew-...
2•theanonymousone•24m ago•0 comments

How to Build Open Source for AI Agents

https://agentledco.substack.com/p/how-to-build-open-source-for-ai-agents
1•hsantana8•25m ago•0 comments

How an MIT research project became a global programming language

https://news.mit.edu/2026/how-mit-research-project-became-global-programming-language-0831
2•theanonymousone•26m ago•0 comments

The Hedgehog and the Fox

https://en.wikipedia.org/wiki/The_Hedgehog_and_the_Fox
1•dcminter•28m ago•0 comments

Sampling hard [quantum] circuits with verifiably high fidelity

https://arxiv.org/abs/2607.25941
1•thebeardisred•28m ago•0 comments

Peekl: An Alternative to Puppet and Ansible

https://peekl.dev/
1•redat00•29m ago•1 comments

If you work in software company, then you must be making Windows, Mouse Keyboard

https://medium.com/@gurvinder372/if-you-work-in-a-software-company-then-you-must-be-making-window...
1•gps372•31m ago•0 comments

Show HN: Sonde, a local code graph for AI agents that refuses to guess

https://github.com/anishmoncivarghese/sonde
1•anishvarghese•31m ago•0 comments

Soundwise.ai: Free AI Transcription That Respects Your Privacy

https://soundwise.ai
1•ShawnaWang•33m ago•0 comments

Ask HN: Are Prompt Injections "Malware"?

1•razorbeamz•36m ago•5 comments

KDE for Digital Sovereignty

https://kde.org/for/digital-sovereignty/
1•ognarb•37m ago•0 comments

ReactOS 0.4.16

https://reactos.org/project-news/reactos-0416-released/
1•marttt•39m ago•0 comments

IST as common time reference across the country

https://www.pib.gov.in/PressReleasePage.aspx?PRID=2304613&reg=48&lang=2
1•cyb0rg0•41m ago•0 comments