frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Cerelog ESP-EEG is a new 8-channel biosensing board at a hobbyist-friendly price

https://www.autodidacts.io/cerelog-esp-eeg-affordable-openbci-like-board/
1•Curiositry•6m ago•0 comments

Designing Predictable and Maintainable Forms in React

https://jsdev.space/react-form-primitives/
1•javatuts•7m ago•0 comments

Construction to begin on Florida expressway that will charge EVs while driving

https://www.nbcmiami.com/news/construction-to-begin-on-florida-expressway-that-will-charge-evs-wh...
2•geox•11m ago•2 comments

How much gold is kept in the Bank of England?

https://www.bankofengland.co.uk/explainers/how-much-gold-is-kept-in-the-bank-of-england
2•thunderbong•11m ago•0 comments

Marmot – A distributed SQLite server with MySQL wire compatible interface

https://github.com/maxpert/marmot
3•zX41ZdbW•13m ago•0 comments

neTV: A new option for self hosted IPTV transcoding

https://github.com/jvdillon/netv
2•dfacto•14m ago•1 comments

Ruamel.yaml gave me the first incident of 2026

https://yaml.dev/blog/01/
2•pandemiclatte•17m ago•2 comments

Scalable Oral Exams with an ElevenLabs Voice AI Agent

https://www.behind-the-enemy-lines.com/2025/12/fighting-fire-with-fire-scalable-oral.html
2•owl_vision•24m ago•0 comments

Learnings from 100K Lines of Rust with AI

https://zfhuang99.github.io/rust/claude%20code/codex/contracts/spec-driven%20development/2025/12/...
2•dputtick•25m ago•0 comments

A Solid Load of Bull

https://loup-vaillant.fr/articles/solid-bull
1•signa11•26m ago•0 comments

2025 in Review at DemandSphere

https://www.demandsphere.com/blog/2025-in-review-at-demandsphere/
1•rgrieselhuber•26m ago•0 comments

Rav1d will never beat dav1d in performance

3•key_aavoja•28m ago•0 comments

Thoughts on the emergence of artificial general intelligence

https://jesse.id/blog/posts/thoughts-on-the-emergence-of-artificial-general-intelligence
1•jesse_dot_id•38m ago•0 comments

Show HN: Collage87.com – Free photo collage/grid maker (no login, no watermarks)

https://collage87.com/
2•brianchanwh•41m ago•1 comments

AI Futures Model: Dec 2025 Update

https://blog.ai-futures.org/p/ai-futures-model-dec-2025-update
2•amstam•44m ago•1 comments

PG&E to Lower Electric Prices on Jan. 1, Fourth Decrease in Two Years

https://www.pge.com/en/newsroom/press-release-details.html
1•bryan0•45m ago•1 comments

RyOS

https://github.com/ryokun6/ryos
1•handfuloflight•50m ago•0 comments

Happy Public Domain Day 2026

https://publicdomainreview.org/blog/2026/01/public-domain-day-2026/
23•apetresc•53m ago•0 comments

HP-UX hits end-of-life today, and I'm sad

https://www.osnews.com/story/144094/hp-ux-hits-end-of-life-today-and-im-sad/
3•notpushkin•55m ago•0 comments

Stolze-Smith Shorthand

https://stolze-smith.com/
2•crux•56m ago•0 comments

Humane Guidelines for Computer Usage and Design

https://horeszko.ca/blog/humane-guidelines-for-computer-usage-and-design.html
1•horeszko•58m ago•0 comments

Extensibility: The "100% Lisp" Fallacy

https://kyo.iroiro.party/en/posts/100-percent-lisp/
2•todsacerdoti•59m ago•0 comments

Pelican: Multi-step SVG generation with vLLMs

https://pelican.alexey.work/
2•xl0•1h ago•2 comments

Rust9x update: Rust 1.93.0-beta

https://seri.tools/blog/rust9x-1-93/
2•pornel•1h ago•0 comments

Saks Prepares for Bankruptcy After Missing Debt Payment

https://www.wsj.com/finance/saks-prepares-for-bankruptcy-after-missing-debt-payment-ff3df6d2
3•bookofjoe•1h ago•2 comments

Security breaks during partial failures – design notes from distributed systems

3•sandhyavinjam•1h ago•0 comments

Ask HN: Automated WebRTC Livestream Recording

3•PhilipWee•1h ago•0 comments

Everyone's Watching Stocks. The Real Bubble Is AI Debt

https://www.bloomberg.com/news/newsletters/2025-12-31/everyone-s-watching-stocks-the-real-bubble-...
3•zerosizedweasle•1h ago•1 comments

Norman Foster's steroidal new skyscraper is an affront to the New York skyline

https://www.theguardian.com/artanddesign/2025/dec/03/eco-obscenity-norman-foster-skyscraper-jp-mo...
5•PaulHoule•1h ago•0 comments

Why users cannot create Issues directly (Ghostty)

https://github.com/ghostty-org/ghostty/issues/3558
4•xpe•1h ago•1 comments