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.

Liminality

https://geohot.github.io//blog/jekyll/update/2026/06/23/liminality.html
1•tosh•32s ago•0 comments

Naïve Art

https://en.wikipedia.org/wiki/Na%C3%AFve_art
1•o4c•8m ago•0 comments

Companies are struggling to hire competent software developer

https://twitter.com/GergelyOrosz/status/2071105819089973716
1•USTECH_WORKER•11m ago•0 comments

All Your Favorite Gadgets Are Getting More Expensive Again

https://www.wired.com/story/we-are-in-the-knockout-round-of-price-increases-for-consumer-electron...
1•tosh•12m ago•0 comments

Show HN: AI assistant for Google Chat to translate any file preserving layout

https://workspace.google.com/marketplace/app/anyfile_translator/960750725467
1•robgehring•12m ago•1 comments

Show HN: Parlan – a new programming language for fun

https://github.com/parlan-lang/parlan
1•Santiago-Lopez•14m ago•1 comments

Self-Hosting Umami with Cloudflare, Fly, and Supabase

https://hjerpbakk.com/blog/2026/07/11/self-hosting-umami
1•Sankra•14m ago•0 comments

Einride: Freight Platform Has Enough Early Traction

https://seekingalpha.com/article/4921116-einride-freight-platform-has-enough-early-traction
1•kamil55555•16m ago•0 comments

I built a free Wordle × GeoGuessr for the stock market

https://benginn.github.io/stockguesser/
1•obepozdemir•20m ago•0 comments

Show HN: Andurel Web Framework Releases v1.0.0

https://github.com/mbvlabs/andurel
1•mbvisti•24m ago•0 comments

ChatGPT Work

https://openai.com/chatgpt-work/
2•modinfo•26m ago•0 comments

Gene-edited babies are now closer. The ethical debate is far from settled

https://www.cnn.com/2026/07/08/science/human-embryo-gene-editing
1•giuliomagnifico•29m ago•0 comments

New dinosaur species as long as cricket pitch discovered in Thailand

https://www.bbc.com/news/articles/crely7g8xepo
1•zdkaster•29m ago•0 comments

Element 26.07.3, a Matrix client, adds content scanning infrastructure

https://github.com/element-hq/element-x-ios/releases/tag/release%2F26.07.3
1•nickslaughter02•29m ago•1 comments

Skill nostalgia - The radical reasons why you dream of making things by hand

https://aeon.co/essays/the-radical-reasons-why-you-dream-of-making-things-by-hand
3•pseudolus•30m ago•0 comments

Ofcom Wants American Police to Collect Its Speech Fine

https://reclaimthenet.org/ofcom-wants-american-police-to-collect-its-speech-fine
2•delichon•33m ago•1 comments

Show HN: Axletic – a local-first workout tracker for iPhone

https://www.axletic.app
1•bjar2•33m ago•0 comments

Larry Page: "I think we should look into acquiring (2005) [YouTube]

https://twitter.com/TechEmails/status/1433837480449613839
2•downbad_•34m ago•0 comments

China recovered its first reusable rocket and showed a new way to do it

https://arstechnica.com/space/2026/07/china-recovered-its-first-reusable-rocket-and-showed-a-new-...
5•pseudolus•34m ago•0 comments

fastcore v2

https://github.com/AnswerDotAI/fastcore
1•tosh•36m ago•0 comments

Show HN: Find introduction path between you and someone you want to reach

https://reloadium.com/reloadium-contacts/
1•julienreszka•38m ago•1 comments

AI Surveillance and Social Progress

https://www.schneier.com/blog/archives/2026/07/ai-surveillance-and-social-progress.html
2•ahamez•39m ago•0 comments

Jelly Trim ReviEwS (2026): We Tried It My Honest Review

https://gamma.app/embed/Jelly-Trim-ReviEwS-2026-We-Tried-It-My-Honest-Review-53eqnkqqiyo7hbs
2•nahqsakr•39m ago•0 comments

An Ex‑Apple Designer Built an EV with No Doors and a Top Speed of 40 MPH

https://www.inc.com/connor-jewiss/an-ex-apple-designer-built-an-ev-with-no-doors-and-a-top-speed-...
2•connorjewiss•43m ago•2 comments

Trident – macOS menu-bar utility that remaps trackpad gestures

https://github.com/cyanyux/trident
2•cyanyux•44m ago•0 comments

Show HN: Python SDK for signing digital content with revocable signatures

https://github.com/lyfeninja/lyfeninja_blkseal_python_sdk
1•lyfeninja•48m ago•0 comments

Karpathy LLM Wiki (Obsidian Plugin)

https://llmwiki.greenerai.top/
1•rahimnathwani•52m ago•0 comments

OptiAir Portable Cooler ReviEwS (July 2026): We Tried It My Honest Review

https://gamma.app/embed/OptiAir-Portable-Cooler-ReviEwS-July-2026-We-Tried-It-My-Honest-R-llbyvuc...
2•gaxyjaik•52m ago•0 comments

World-War-Ⅱ-era telephone line still in use in Upper Tanana Valley Alaska (2021)

https://www.sketchesofalaska.com/2021/03/world-war-ii-era-telephone-line-still.html
2•Lammy•53m ago•1 comments

Who cleans up after the vibe-coding party?

https://www.ft.com/content/cec8df9e-b43b-4cd1-8feb-c07e804e8d33
2•msephton•54m ago•1 comments