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.

Chronophase: A fantastic UX to experiment with music and sound

https://chronophase.vercel.app/
1•sachaa•18s ago•0 comments

The Challenge of US Rural Development

https://conversableeconomist.com/2026/10/06/the-challenge-of-us-rural-development/
1•colinprince•1m ago•0 comments

PD's New Flock Policy Just Shows How Little It Was Doing Before the Blowback

https://www.techdirt.com/2026/10/07/pds-new-policy-for-flock-cameras-just-shows-how-little-it-was...
1•hn_acker•3m ago•1 comments

AI to Fight World Hunger

https://features.csis.org/ai-to-fight-world-hunger/
2•gmays•6m ago•0 comments

IBM Finds 400 Vulnerabilities

https://newsroom.ibm.com/2026-10-06-ibm-and-red-hat-remediate-more-than-400-previously-unknown-op...
2•____tom____•7m ago•1 comments

I'm in love with a German film star

https://heatherburns.tech/2026/10/01/im-in-love-with-a-german-film-star/
4•hn_acker•10m ago•0 comments

Apple Watch Series 12 / Ultra 4 Owners Report Skin Irritation

https://www.macrumors.com/2026/10/07/apple-watch-series-12-skin-irritation/
1•dabinat•11m ago•0 comments

Neural Network Diagram Compiler

https://lens-compiler.dk.workers.dev
1•measurablefunc•11m ago•0 comments

Meta's Copyright System Is Being Weaponized Against Albanian Protesters

https://www.wired.com/story/metas-copyright-system-is-being-weaponized-against-albanian-protesters/
2•hn_acker•16m ago•0 comments

Your Brain Might Be a Quantum Machine [video]

https://www.youtube.com/watch?v=VjlX7w_CjqQ
2•jonbaer•19m ago•0 comments

Claude Max and Teams plans now forced to API (Oct 7th update)

1•mattstarfield•21m ago•0 comments

Intelligence Is Not Expertise

https://www.sebastianmellen.com/post/2026/intelligence-is-not-expertise/
1•sebmellen•21m ago•0 comments

Festival: A Vibe Engineering Framework

https://github.com/Obedience-Corp/festival
1•lancekrogers•24m ago•1 comments

The Strange Power of Coincidence

https://www.theatlantic.com/newsletters/2026/10/strange-power-of-coincidence/688880/
2•bookofjoe•25m ago•1 comments

Show HN: Reflex – a cold-start GGUF engine that runs Aleph Alpha's Kolibri-1

https://github.com/lateos-ai/reflex
1•leochong•27m ago•0 comments

IngeTrazo – Open-Source SketchUp Clone

https://github.com/ingelibre/ingetrazo
1•dmacedo•28m ago•0 comments

How I use Claude Code subagents to make my Claude Pro limits last longer

https://www.andreagrandi.it/posts/claude-code-subagents-to-save-usage/
1•andreagrandi•32m ago•0 comments

Building Harvey's MCP Policy Engine

https://www.harvey.ai/blog/building-harveys-mcp-policy-engine
1•suhacker256•35m ago•0 comments

Workers: Simulation Environments for Verifying Mission Critical Software

https://workers.io/
1•handfuloflight•39m ago•0 comments

The Oktoberfest Cartel

https://www.mondayeconomist.com/p/oktoberfest-cartel
1•NomNew•39m ago•0 comments

I Benchmarked Databricks Against My iPhone

https://www.fivetran.com/blog/i-benchmarked-databricks-against-my-iphone
1•georgewfraser•39m ago•0 comments

Top Forecasters on the Plague

https://www.astralcodexten.com/p/top-forecasters-on-the-plague
2•toomuchtodo•40m ago•0 comments

Show HN: Railcode – Secure cloud for internal software

https://railcode.dev/
2•yakkomajuri•41m ago•1 comments

You can just fix everything with a Nix overlay

https://ghuntley.com/nix/
1•domenkozar•41m ago•0 comments

Everything we learned from dropping 80 apps

https://dangertestingpress.substack.com/p/everything-we-learned-from-dropping
1•structuredPizza•42m ago•0 comments

How to Avoid Disaster in the Next Iran War

https://coinmarketcap.com/community/post/379927415/
2•joeymabia1•43m ago•0 comments

Millions may face prescription drug price surge on Oct. 22

https://www.usatoday.com/story/money/2026/10/07/millions-may-lose-access-to-cheap-foreign-medicin...
4•elo2000•45m ago•2 comments

Stress and agents? Paired my Garmin with agent history and transcripts

https://oscar-labs.vercel.app/my-watch-thinks-i-was-resting
1•Oscalemor•46m ago•1 comments

From Graphical Interface to Agentic Interface

https://agentrow.io/blog/agentic-interface-the-new-interface-of-software
1•nidheesh1994•46m ago•0 comments

Declass is a coding agent that keeps sensitive context on your machine

https://github.com/maximpri/declass
1•maximpr•49m ago•0 comments