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.

Danish warship sunk by Nelson's British fleet discovered after 225 years

https://apnews.com/article/denmark-archaeologists-warship-nelson-copenhagen-dannebroge-lynettehol...
1•gmays•8s ago•0 comments

The Hormuz Blockade: The energy and supply chain crisis that's upon us

https://martinvol.pe/blog/2026/04/06/the-energy-and-supply-chain-crisis-thats-upon-us/
1•martinvol•11s ago•0 comments

Show HN: Give your AI coding assistant persistent, semantic memory

https://github.com/strvmarv/total-recall
1•strvmarv•27s ago•0 comments

The Value of Technological Progress

https://worksinprogress.co/issue/the-value-of-technological-progress/
1•sarimkx•1m ago•0 comments

Eat Your Oatmeal

1•dfedbeef•3m ago•0 comments

Mac Mini and Mac Studio facing extreme shipping delays amid RAM shortage

https://www.macrumors.com/2026/04/06/mac-mini-and-mac-studio-long-shipping-delays/
1•lysace•4m ago•0 comments

Generating Flashcards from PDF Underlines

https://danilafe.com/blog/pdf_flashcards_llm/
1•ibobev•5m ago•0 comments

Strait of Hormuz: A Citrini Field Trip

https://www.citriniresearch.com/p/strait-of-hormuz-a-citrini-field
1•Luc•5m ago•0 comments

The new-er-est "Blade Runner" (2012)

https://www.rogerebert.com/scanners/the-new-er-est-blade-runner
1•evo_9•5m ago•0 comments

Personal Software with the Help of LLMs

https://danilafe.com/blog/llm_personal_software/
1•ibobev•6m ago•0 comments

Book Review: When We Cease to Understand the World

https://www.stephendiehl.com/posts/cease_understand_world/
1•ibobev•6m ago•0 comments

What if your AI remembered and learned from its mistakes?

https://github.com/sliamh11/Deus
1•sliamh11•7m ago•0 comments

Show HN: Vulnchain – open-source SAST that synthesises attack chains

https://github.com/hamzamiladin/Vulnchain/tree/main
1•kenshi144•9m ago•0 comments

Dev.css: tiny, simple, classless CSS framework

https://tangled.org/devins.page/dev.css
1•nerdypepper•9m ago•0 comments

System Engineering an agent thanks to Golang

https://blog.mattsbit.co.uk/2026/04/06/systems-engineering-an-agent-thanks-to-the-world-of-golang/
1•mrmattyboy•10m ago•0 comments

When does human control become ineffective in advanced AI systems?

https://zenodo.org/records/19440592
1•EliasArden•10m ago•1 comments

The Economics of Generative AI: Two Years Later

https://apoorv03.com/p/the-economics-of-generative-ai-two
1•gmays•11m ago•0 comments

My Carpet Collection

https://itsiweinstock.substack.com/p/my-carpet-collection
1•ItsiW•11m ago•0 comments

Cider cross-platform Apple Music experience

https://cider.sh/
1•hmokiguess•13m ago•0 comments

Show HN: AI agent that edits videos from natural language commands

https://twitter.com/yonatanbd/status/2041156418183500091
2•yonatan06•15m ago•0 comments

Hershey's Promises to Use Only Real Chocolate After Backlash

https://www.nytimes.com/2026/04/02/business/hershey-reeses-recipe-real-chocolate.html
1•mikhael•17m ago•0 comments

Corporate Accountability – Some Retired Clown Challenging McDonald's CEO

https://eattheburgerchris.com/
1•drkica•17m ago•2 comments

Catherine Lacey

https://www.catherinelacey.com
1•bookofjoe•17m ago•0 comments

Garmin – Give My Data

https://github.com/nrvim/garmin-givemydata
1•ArlenBales•17m ago•0 comments

Stop Writing Guard Clauses Like It's 2010

https://dotnetfiddle.substack.com/p/stop-writing-guard-clauses-like-its
1•refactormonkey•19m ago•0 comments

Old economy software x New economy software

https://ctts.substack.com/p/old-economy-software-x-new-economy
1•threefiftyone96•19m ago•0 comments

Show HN: mva-melding-java – Java library for Norwegian VAT filing via Altinn 3

1•guanghu•19m ago•0 comments

Boxsh: A sandboxed POSIX shell with concurrent JSON-line RPC mode

https://github.com/xicilion/boxsh
1•progyoung•19m ago•0 comments

JSIR: A High-Level IR for JavaScript

https://discourse.llvm.org/t/rfc-jsir-a-high-level-ir-for-javascript/90456
2•michaelkrem•21m ago•0 comments

Sourcehut disrupted due to DDoS attack [Apr 6, 2026]

https://status.sr.ht/issues/2026-04-06-ddos-attack/
1•alakra•21m ago•0 comments