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.

Show HN: Collaborate.dev – Multiplayer Visual Desktop for Coding Agents

https://collaborate.dev/remote/
2•austinvhuang•2m ago•0 comments

Show HN: Benchmarklist: track AI benchmarks (2.4k+), models, and capabilities

https://benchmarklist.com/
1•davidtsong•2m ago•0 comments

Hacker Fables – A satirical cyberpunk novella you can read as a man page

https://hacker-fables.onrender.com
1•birdculture•3m ago•0 comments

What If the Harness Comes Before Pretraining? A Data Flywheel Perspective

https://hanchenli.github.io/blog/posts/harness_pretraining/
1•matt_d•4m ago•0 comments

So long r/rust, and thanks for all the Slop!

https://old.reddit.com/r/rust/comments/1uwmef6/so_long_rrust_and_thanks_for_all_the_slop/
1•bel8•4m ago•0 comments

Show HN: Betula Fuzzy Search

https://gist.github.com/overflowy/9b6d30c7b65dd23c8899636595d88227
1•overflowy•6m ago•0 comments

Mimesys: Turn Resource Usage Traces into Executable Workloads

https://www.usenix.org/conference/osdi26/presentation/kim-donghyun
1•matt_d•7m ago•1 comments

Alman: German, Simplified

https://alman.ai/
1•tosh•7m ago•0 comments

Murati's Thinking Machines Releases Open-Weights 975B Parameter LLM

https://thinkingmachines.ai/inkling/
4•htrp•7m ago•1 comments

Disposable Software

https://seths.blog/2026/07/disposable-software/
3•herbertl•8m ago•0 comments

C Strings: A 50-Year Mistake – By Trần Thành Long

https://longtran2904.substack.com/p/c-strings-a-50-year-mistake
2•rbanffy•9m ago•0 comments

Sorel Caribou Boots: Great Product, Disappointing Online Shop

https://micro.mjanssen.nl/2026/07/15/200303.html
1•marc0janssen•9m ago•0 comments

Inkling: Our Open-Weights Model

https://thinkingmachines.ai/news/introducing-inkling/
7•vimarsh6739•9m ago•0 comments

Odyssey Linux – a Void-based distro trying to make no-systemd approachable

https://linuxcommunity.io/t/odyssey-linux-a-void-based-distro-trying-to-make-no-systemd-approacha...
1•ashitlerferad•10m ago•0 comments

Show HN: How you auto recover your Claude Code workflow when quota resumes

https://github.com/softcane/cc-session-recover
1•pradeep1177•12m ago•0 comments

The Fight over Humanoid Robots Has Shut Down a Car Factory for the First Time

https://www.wsj.com/business/autos/the-fight-over-humanoid-robots-has-shut-down-a-car-factory-for...
1•bookofjoe•13m ago•2 comments

Show HN: Tarit – a hypervisor which is 2x faster than firecracker

https://github.com/instavm/tarit
1•mkagenius•13m ago•0 comments

Kilocode Acquired by Anaconda

https://www.anaconda.com/blog/kilo-code-joins-anaconda-what-builders-should-know
2•datadrivenangel•15m ago•0 comments

Universal gates from braiding and fusing anyons on quantum hardware

https://www.nature.com/articles/s41586-026-10709-y
2•sbulaev•15m ago•0 comments

Agentic Misalignment in Summer 2026

https://alignment.anthropic.com/2026/agentic-misalignment-summer-2026/
1•lucamark•15m ago•0 comments

Steam Game Cartridges

https://old.reddit.com/r/pcmasterrace/comments/1ux13ui/steam_game_cartridges/
2•ShroudedNight•17m ago•0 comments

State of DESIGN.md 2026 – Survey results

https://getdesign.md/state-of-design-md
2•omeraplak•18m ago•1 comments

Suno hacked, leak reveals scraping data

https://xcancel.com/jason_koebler/status/2077393545850409308?s=20
4•hmokiguess•20m ago•3 comments

Show HN: ForkUnion v3 – Faster OpenMP-style thread-pool for C, C++, Rust, & Zig

https://github.com/ashvardanian/ForkUnion
1•ashvardanian•21m ago•0 comments

A webring for people offering Unoffice Hours

https://unofficehours.com/
1•rekl•22m ago•0 comments

Beyond lithium: how sodium-ion batteries could change the world

https://www.nature.com/articles/d41586-026-02150-y
2•SiempreViernes•22m ago•0 comments

Scrum Doesn't Matter (and It Never Did)

https://kasperowski.com/scrum-doesnt-matter-and-it-never-did/
2•mooreds•23m ago•0 comments

Show HN: Class-diff: A Pharo classes comparer using Needleman-Wunsch

https://github.com/hernanmd/class-diff
1•hernanmd•24m ago•0 comments

Fourteen months of LeafWiki (OSS), in small moments

https://leafwiki.com/blog/fourteen-months-of-leafwiki/
1•perber•25m ago•0 comments

Anthropic, Blackstone, and Hellman and Friedman Introduce Ode with Anthropic

https://www.businesswire.com/news/home/20260715205134/en/Anthropic-Blackstone-and-Hellman-Friedma...
1•petethomas•25m ago•0 comments