frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Why I don't like the "staff engineer archetypes"

https://www.seangoedecke.com/staff-engineer-archetypes/
1•swah•1m ago•0 comments

SpaceChem 2012 Tournament

https://lparchive.org/SpaceChem-2012-Tournament/
1•tmtvl•4m ago•0 comments

Father hiring bouncer to keep kids from beating the dog due to phone addiction

https://www.msn.com/en-in/news/India/in-gujarat-bouncers-hired-to-watch-screen-addicts/ar-AA22fBfW
1•vivzkestrel•6m ago•0 comments

Agentic Coding Is Burning Me Out

https://0xsid.com/blog/agentic-coding-fatigue
1•mpweiher•8m ago•0 comments

Richard Sutton – Father of RL thinks LLMs are a dead end [video]

https://www.youtube.com/watch?v=21EYKqUsPfg
1•mpweiher•9m ago•0 comments

Company says nuclear fusion could power the grid – and soon

https://www.cnn.com/2026/04/30/climate/nuclear-fusion-real-world-electricity-grid
1•mpweiher•10m ago•0 comments

A 360° panorama generator that turns one sentence into a world

https://imagesv2.ai/
1•taoji1994•10m ago•0 comments

Systemd-manager-TUI: A TUI application for managing systemd services

https://github.com/Matheus-git/systemd-manager-tui
3•thunderbong•13m ago•0 comments

Re-Imagining GitHub/GitLab Actions

https://almostintuitive.com/docs/technical/reimagining-github-actions/
1•legothief•14m ago•0 comments

The Problem with Open Hardware [video]

https://www.youtube.com/watch?v=YLn4vnfchaE
1•Udo_Schmitz•17m ago•0 comments

Shares in Japanese toilet maker Toto soar on AI-related pivot

https://www.ft.com/content/38969e97-92e4-4066-a554-27274d32d545
1•JumpCrisscross•18m ago•0 comments

Names Are the First Thing You Read and the Last Thing You Remember

https://fagnerbrack.com/names-are-the-first-thing-you-read-and-the-last-thing-you-remember-b2b3f0...
1•fagnerbrack•19m ago•0 comments

Dependencies Are Replacing Knowledge

https://www.youtube.com/watch?v=99RgPS3Vu5M
1•birdculture•19m ago•0 comments

Becoming Unblockable

https://www.seangoedecke.com/unblockable/
2•fagnerbrack•20m ago•0 comments

Migrating 6000 React tests using AI Agents and ASTs

https://eliocapella.com/blog/ai-library-migration-guide/
1•fagnerbrack•20m ago•0 comments

Three full-stack data platforms in a weekend: Fabric and Azure AI Foundry

https://cubiczan.substack.com/p/how-we-shipped-3-full-stack-data
1•cubiczan•22m ago•0 comments

If society had a scorecard, what would be on it?

1•baptou12•25m ago•1 comments

Edible Plants Wiki

https://edibleplants.wikioasis.org/wiki/Main_Page
1•altilunium•33m ago•0 comments

Mnemory – Persistent memory for AI agents

https://github.com/fpytloun/mnemory
2•genunix64•36m ago•1 comments

Show HN: WebAssembly Interpreter in a Header

https://github.com/lifthrasiir/wah
1•lifthrasiir•36m ago•0 comments

Codeonix: Python task automation for your desktop – with AI, 14 triggers

https://codeonix.app
2•hassananayi•37m ago•1 comments

A month after being laid off, I wrote the story I needed to make sense of it

https://anushkakarmakar.substack.com/p/1-why-did-i-choose-to-run-that-marathon
1•thinkingkite•38m ago•0 comments

Why CFOs Need a Consensus Hardening Protocol for AI Decisions

https://cubiczan.substack.com/p/why-cfos-need-a-consensus-hardening
1•cubiczan•40m ago•0 comments

As a Ukrainian journalist, I've covered the US for 20 years. I find it shocking

https://www.theguardian.com/world/2026/apr/30/as-a-ukrainian-journalist-ive-covered-the-us-for-20...
1•YeGoblynQueenne•42m ago•0 comments

Graphene is on track to deliver on its promises (2019)

https://www.nature.com/articles/s41565-019-0557-0
2•simonebrunozzi•43m ago•0 comments

Livecodes – A Code Playground That Just Works

https://github.com/live-codes/livecodes
1•modinfo•53m ago•0 comments

Fujian Tulou

https://en.wikipedia.org/wiki/Fujian_tulou
1•simonebrunozzi•54m ago•0 comments

KMRI: A chunk-based compression format for MRI-style 3D volumes

https://github.com/Kiamehr5/KMRI
1•kiamehr•56m ago•0 comments

Ripple – the elegant TypeScript UI framework

https://www.ripple-ts.com/
2•modinfo•58m ago•0 comments

SMS blaster rising in Switzerland (French)

https://www.rts.ch/info/suisse/2026/article/sms-blaster-l-arnaque-aux-fausses-amendes-se-repand-d...
2•kuon•1h ago•0 comments