frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

HTML to PDF

https://github.com/Timh1970/wkgtk-html2pdf
1•timh1970•58s ago•1 comments

Tbps of capacity: Cloudflare scaling its global network for 16 years

https://blog.cloudflare.com/500-tbps-of-capacity/
1•emot•2m ago•0 comments

Shibboleth 67

https://aimeru.com/blog/shibboleth67.html
1•dirk94018•5m ago•0 comments

Claude AI Assistant for Microsoft Office

https://pivot.claude.ai
1•skogstokig•5m ago•0 comments

Anthropic Will Use CoreWeave's AI Capacity to Power Claude

https://www.bloomberg.com/news/articles/2026-04-10/anthropic-agrees-to-rent-coreweave-ai-capacity...
2•Anon84•7m ago•0 comments

Microsoft lets Windows 11 testers unlock experimental features

https://www.theverge.com/news/909659/microsoft-windows-insider-changes-unlock-experimental-featur...
2•cebert•8m ago•0 comments

Show HN: The API directory for AI agents and developers

https://saxi.ai/
1•Adanos•8m ago•0 comments

OpenAI's new $100 tier targets developers hitting Codex limits

https://thenewstack.io/openais-new-100-tier-targets-developers-hitting-codex-and-claude-code-limits/
1•Brajeshwar•10m ago•0 comments

Iranian hackers are targeting US energy and water sectors, federal agencies warn

https://www.politico.com/news/2026/04/07/iranian-hackers-energy-water-cybersecurity-00862018
2•rbanffy•11m ago•0 comments

Open Website Omegle

https://blog.cloudflare.com/welcome-to-connectivity-cloud/
1•Eknathpawar•11m ago•0 comments

ParrotOS and Usenet: A Complete VPS and Home Server Setup

https://linuxserverguides.com/parrot-usenet-guide.html
1•steelsmiley•12m ago•0 comments

A Ruby program that generates a Rust program that generates a Scala program...

https://github.com/mame/quine-relay
2•bundie•12m ago•0 comments

Show HN: Pyre Agents – An Elixir-Orchestrated Runtime for Python Agents

https://akashyap.ai/pyre-agents-an-elixir-orchestrated-runtime-for-python-agents/
1•KashyapArjun•13m ago•0 comments

Artemis, Greatness, and Risk

https://1517.substack.com/p/1-in-30-artemis-greatness-and-risk
2•lawrenceyan•13m ago•0 comments

Suspect Arrested for Throwing Molotov Cocktail at Sam Altman's Home

https://www.wired.com/story/sam-altman-home-attack-openai-san-franisco-office-threat/
2•coloneltcb•14m ago•1 comments

Harvard Economist Ludwig Straub Wins 2026 John Bates Clark Medal

https://www.thecrimson.com/article/2026/4/9/ludwig-straub-clark-medal/
1•paulpauper•14m ago•0 comments

Roots of Progress Blog-Building Intensive

https://newsletter.rootsofprogress.org/p/announcing-the-2026-roots-of-progress
1•paulpauper•15m ago•0 comments

Postsecondary and Labor Outcomes for Ohio Students Earning Industry Credentials

https://fordhaminstitute.org/ohio/research/college-or-career-readiness-postsecondary-and-labor-ma...
1•paulpauper•15m ago•0 comments

1,200 years of peak cherry blossom dates in Kyoto

https://twitter.com/antea04/status/2042541639793627533
2•tzury•15m ago•1 comments

Show HN: Expose your agents via MCP as a tool

http://mantyx.io/mcp
1•mantyx•16m ago•0 comments

The Weather Channel RetroCast Now Behind the Scenes and Technical / Design

2•leemartin•18m ago•0 comments

Bring Back Buddy – A Consolidated Plea from the [Claude Code] Community

https://github.com/anthropics/claude-code/issues/45596
1•rickcarlino•18m ago•0 comments

CoreWeave Announces Multi-Year Agreement with Anthropic

https://www.coreweave.com/news/coreweave-announces-multi-year-agreement-with-anthropic
1•skogstokig•18m ago•0 comments

Former NASA Astronaut Charles Camarda on the Artemis II Heatshield Decision

https://docs.google.com/document/d/1ddi792xdfNXcBwF8qpDUxmZzIksrs0jy/edit
1•nickvec•18m ago•1 comments

V1.21 Update for Gpumkat

1•Okerew•21m ago•0 comments

Valence and HYVE, RT Physics Attention and a "Synthetic Organism"

1•PaperScarecrow•21m ago•0 comments

YouTube Premium's US pricing is going up

https://www.engadget.com/social-media/youtube-premiums-us-pricing-is-going-up-132121683.html
3•bookofjoe•22m ago•0 comments

My Picture of the Present in AI

https://www.greaterwrong.com/posts/WjaGAA4xCAXeFpyWm/my-picture-of-the-present-in-ai
1•gmays•23m ago•0 comments

Ask HN: Its either I or Agent code. Both of us on same codebase is a disaster

1•zane__chen•24m ago•0 comments

Artemis 2 LIVE: NASA's Artemis 2 astronauts prepare for splashdown today

https://www.space.com/news/live/artemis-2-nasa-moon-mission-updates-april-10-2026
2•thinkingemote•25m ago•0 comments