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.

Matchmakers Are Busy, but Worried

https://www.tabletmag.com/sections/community/articles/matchmaker-business-booming
1•Michelangelo11•21s ago•0 comments

Humans prefer to walk anticlockwise, scientists find – but reason is unclear

https://www.theguardian.com/science/2026/jun/10/humans-prefer-to-walk-anticlockwise-scientists-fi...
2•helsinkiandrew•5m ago•0 comments

SSL Certificate Expiry Explained

https://urlwatch.io/blog/ssl-certificate-expiry.php
1•rajsuper123•9m ago•0 comments

AUR Report Thread

https://lists.archlinux.org/archives/list/aur-general@lists.archlinux.org/thread/FGXPCB3ZVCJIV7FX...
1•xiaoyu2006•9m ago•0 comments

AUR Packages Compromised with Infostealer and Rootkit

https://discourse.ifin.network/t/400-aur-packages-compromised-with-infostealer-and-rootkit/577
2•keyle•12m ago•0 comments

Zepto's published search architecture only applies to logged-in users

https://medium.com/@abhij89/zeptos-llm-search-works-great-unless-you-re-a-new-user-c13e138de331
1•abhij89•14m ago•0 comments

Countdown calendar for Fable 5 leaving Claude Code

https://fable-farewell-calendar.pages.dev/
2•meowokIknewit•15m ago•1 comments

Guardian Runtime – Track AI agents token usage and enforce API budgets

https://github.com/ashp15205/guardian-runtime
4•ashp15205•16m ago•0 comments

LLM for the ESP32-S3

https://github.com/harmansingh4163-ai/ESP-32-s3-Story-maker-LLM
1•Harman-Singh123•16m ago•0 comments

Digital Sovereignty Becomes an Imperative as the US Reads Dutch Emails

https://www.korte.co/2026/06/11/digital-sovereignty-becomes-an-imparative-as-the-us-reads-dutch-e...
1•dotcoma•18m ago•0 comments

Remote Magic Trackpad – Control Mac's Trackpad and Keyboard from Android Device

https://play.google.com/store/apps/details?id=com.magic.trackpad.free&hl=en_US
1•sks147•21m ago•1 comments

There Is Life Before Main in Rust

https://grack.com/blog/2026/06/11/life-before-main/
1•y1n0•27m ago•0 comments

Stimulants Are the Killer of Generalists

2•anonymous-bear•32m ago•2 comments

Scientists unlock the secret behind the Venus flytrap's snap

https://www.reuters.com/science/scientists-unlock-secret-behind-venus-flytraps-snap-2026-06-11/
2•1659447091•34m ago•0 comments

Does Music Help You Focus? (2022)

https://rdegges.com/2022/does-music-help-you-focus/
2•svenfaw•35m ago•0 comments

Show HN: Landcheck – is coordinate is on land?

https://jaakla.github.io/trifold/landcheck.html
1•jaakl•35m ago•0 comments

Senate wants to force US to share sensitive Intel with Israel

https://responsiblestatecraft.org/us-intelligence-israel/
1•WarOnPrivacy•36m ago•1 comments

Peekaboo Timer – Free Boxing and Sports Timer

https://apps.apple.com/ua/app/peekaboo-timer/id6768093897
1•Romachamp•38m ago•0 comments

Why CrabNebula Cloud is now free: Tauri, the CRA

https://crabnebula.dev/blog/why-cloud-is-free/
1•DanielKehoe•42m ago•0 comments

How to (Inadvertently) Sabotage Your Organization (2019)

https://sloanreview.mit.edu/article/how-to-inadvertently-sabotage-your-organization/
2•thelastgallon•45m ago•0 comments

Infineon to open fab in Germany as part of sovereignty push

https://www.bloomberg.com/news/articles/2026-06-12/infineon-to-open-german-chip-fab-as-part-of-eu...
2•SanjayMehta•46m ago•1 comments

Midnight8Ball, a fast browser-based casino game experiment

https://midnight8ball.com
1•Midnight8ball•48m ago•1 comments

Ask HN: How many $SPCX shares did you request and how many did you get?

1•GMoromisato•50m ago•0 comments

The MilkV Jupiter 2/SpacemiT K3

https://taoofmac.com/space/reviews/2026/06/11/1830
2•snvzz•57m ago•0 comments

A digital reckoning against smartphones in schools has spread to Sweden

https://apnews.com/article/sweden-school-mobile-phones-ban-screen-time-20a32f9bd9e2849a5ba76ef3fd...
1•1vuio0pswjnm7•1h ago•0 comments

SpaceX demolishes IPO records, visualized

https://www.reuters.com/graphics/SPACEX-IPO/byprdokrkpe/
2•giuliomagnifico•1h ago•1 comments

Show HN: Manob: A social media plaform without algorithm, ads, or data-tracking

1•Iamahmedsalman•1h ago•0 comments

Show HN: Dont lose your friends, use a Kadoodle to plan your next event

https://kalerum.com/en/tools/kadoodle
2•mailforge•1h ago•1 comments

OpenAI Considers Drastic Price Cuts, Anticipating War for Users With Anthropic

https://www.wsj.com/tech/ai/openai-considers-drastic-price-cuts-anticipating-war-for-users-with-a...
5•jbk•1h ago•1 comments

The end is not nigh

https://ckarchive.com/b/0vuwh9h96eg98i7mggrmzhv7orr2eunhx8g7n
3•bigfudge•1h ago•0 comments