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.

American Diner Gothic

https://www.thenewatlantis.com/publications/american-diner-gothic
2•rienbdj•4m ago•0 comments

The Download: reawakening frozen brains, and the AI Hype Index returns

https://www.technologyreview.com/2026/03/25/1134636/the-download-reawakening-frozen-brains-and-th...
1•joozio•7m ago•0 comments

Granola raises $125M at $1.5B valuation

https://techcrunch.com/2026/03/25/granola-raises-125m-hits-1-5b-valuation-as-it-expands-from-meet...
3•doppp•10m ago•0 comments

Show HN: Find Customer Service Phone Numbers

https://dialindex.com
1•yosid•13m ago•0 comments

Two year old Android Bluetooth bug fixed, no more root required for librepods

https://issuetracker.google.com/issues/371713238
1•Lunar5227•13m ago•1 comments

FreeBSD remote DoS vulnerability via TCP – FreeBSD-SA-26:06.tcp

https://www.freebsd.org/security/advisories/FreeBSD-SA-26:06.tcp.asc
1•ggeorg•14m ago•1 comments

Spectre GCR Mac emulator for Atari ST manual (1989)

https://archive.org/details/Spectre_GCR_Manual_Macintosh_emulator_1989
1•TMWNN•16m ago•1 comments

Gerard of Cremona

https://en.wikipedia.org/wiki/Gerard_of_Cremona
1•teleforce•19m ago•0 comments

Rich Content AI Chat

https://99helpers.com/tools/ai-chat
1•nickk81•21m ago•1 comments

Accidental $70k Google Pixel Lock Screen Bypass

https://bugs.xdavidhu.me/google/2022/11/10/accidental-70k-google-pixel-lock-screen-bypass/
1•Jacobinski•23m ago•0 comments

Porting Go's io package to C

https://antonz.org/porting-go-io/
2•ingve•27m ago•1 comments

Skeleton of Three Musketeers' d'Artagnan may have been found under Dutch church

https://news.sky.com/story/skeleton-of-three-musketeers-dartagnan-may-have-been-found-under-dutch...
2•austinallegro•31m ago•0 comments

Wittgenstein's Contribution to Philosophy [video]

https://www.youtube.com/watch?v=sVKWlEq09tQ
1•daverol•33m ago•0 comments

Honda and Sony Scrap EVs

https://www.ft.com/content/b85869e1-0f69-4ab3-aa30-1d63d60ee3c9
1•mnming•35m ago•0 comments

macOS 26.4 Introduces New Security Feature for Terminal Commands

https://www.macrumors.com/2026/03/25/macos-26-4-terminal-security-feature/
1•tosh•36m ago•0 comments

70% of new software engineering papers on ArXiv are LLM related

https://shape-of-code.com/2026/03/22/70-of-new-software-engineering-papers-on-arxiv-are-llm-related/
1•jruohonen•37m ago•1 comments

The Sensor Debate: Vision, Lidar, and the Path to Real Autonomy

https://www.nuro.ai/blog/the-sensor-debate-vision-lidar
1•Jadrago•41m ago•0 comments

Crypto regime detection API: 10 weighted signals, 9 data sources

https://getregime.com
1•GunnarThor•42m ago•0 comments

Swift 6.3 Released

https://www.swift.org/blog/swift-6.3-released/
4•ingve•44m ago•0 comments

Solved Game

https://en.wikipedia.org/wiki/Solved_game
2•thunderbong•44m ago•0 comments

Is X down?

3•avnsv•48m ago•6 comments

Building apps after a 9-to-5 without burning out

https://bacist.com/avoid-burnout/
2•baCist•50m ago•0 comments

Polymarket affiliates are spreading misinformation on X

https://www.cjr.org/tow_center/polymarket-affiliates-are-spreading-misinformation-on-x.php
1•giuliomagnifico•51m ago•0 comments

Show HN: I built a free alternative to Screen Studio and Focusee

https://rookieclip.com/
1•this-is-shreya•51m ago•1 comments

Architecture Is Not a Blueprint. It's a Set of Decisions

https://scottmillett.medium.com/architecture-is-not-a-blueprint-its-a-set-of-decisions-61c4692f5222
1•kiyanwang•51m ago•0 comments

Show HN: Book Grounded AI Learning

https://teach-me.replit.app/
1•techypicky•54m ago•1 comments

Duso

https://duso.rocks/
2•kentbrew•54m ago•1 comments

Show HN: I built a selfhosted Wanderlog alternative with real-time collaboration

https://github.com/mauriceboe/NOMAD
1•mauriceboe•55m ago•0 comments

When should a manager step in? – dein.fr

https://www.dein.fr/posts/2026-03-17-when-a-manager-should-step-in
1•kiyanwang•56m ago•0 comments

Declining to review for NeurIPS due to policy on sanctioned institutions

https://pastebin.com/LEmwfMZT
2•limoce•56m ago•0 comments