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.

M John Harrison becomes first science fiction author to win the Booker prize

https://www.theguardian.com/books/2026/nov/09/m-john-harrison-the-end-of-everything-wins-2026-boo...
1•sorokod•3m ago•0 comments

Ask HN: Exploring non-Euclidean static-resonance and zero-latency hardware (v=0)

1•masterofroot•5m ago•0 comments

FlashStore: A Ceph OSD Rewritten in Rust for NVMe

https://github.com/croit/ceph-rust
1•jojodicus•7m ago•0 comments

Self-hosting email the hard way from your own routable IPv4 block up

https://anil.recoil.org/notes/recoil-self-hosting-2026
1•cromka•8m ago•0 comments

Two Apps One Process

https://andersmurphy.com/2026/11/09/two-apps-one-process.html
1•andersmurphy•9m ago•1 comments

Fortune Telling and Speculative Design

https://maggieappleton.com/fortune-telling
2•saikatsg•15m ago•0 comments

Terraform governance: using incident evidence and plan output to define "fixed"

https://leanpub.com/terraform-on-azure
1•newbmiao•21m ago•0 comments

How to control unit tests in Agentic Pipelines

3•n4pw01f•23m ago•0 comments

My notes app has no database, just a folder of Markdown and an MCP server

https://sayjot.xyz/blog/no-database
1•mohakbajaj•24m ago•0 comments

Max Hardberger: Repo man of the seas (2010)

https://www.theguardian.com/world/2010/nov/14/max-hardberger-sea-captain-pirates-seized
1•js2•24m ago•0 comments

Fireworks AI security breach impacts Notion and their customers

https://www.notion.com/help/security-incident-involving-a-third-party-ai-vendor
1•werrett•24m ago•0 comments

MeterMesh: Count SaaS usage in a small Go mesh before Postgres

https://github.com/vinayrawat058/metermesh
1•vinayrawat058•24m ago•0 comments

RippleMap – Map dependencies across GitHub repositories

https://github.com/Zintrivious/ripplemap
1•Zintrivious•32m ago•0 comments

New Details Emerge in Amazon Ads Auction-Rigging Case

https://www.ftc.gov/news-events/news/press-releases/2026/11/new-details-emerge-amazon-ads-auction...
2•toomuchtodo•32m ago•1 comments

Mailgun SMTP is moving to TLS 1.2: What you need to do before the March 1, 2027

https://www.mailgun.com/blog/product/mailgun-smtp-is-moving-to-tls-1-2-what-you-need-to-do-before...
1•asplake•38m ago•0 comments

S.t.a.l.k.e.r. Call of Pripyat in WASM and WebGPU

https://stalker-online.xyz/
1•roman01la•40m ago•0 comments

Microsoft are rejecting your mail

https://www.mythic-beasts.com/blog/2026/11/09/microsoft-are-rejecting-your-mail/
1•birdculture•41m ago•0 comments

JPMorganChase expands Claude Code to more employees across the firm

https://claude.com/resources/articles/jpmorganchase-expands-claude-code
2•geoffbp•41m ago•1 comments

Gostman, the open source API client

https://gostman.com/
1•jicea•44m ago•0 comments

Camoufox for Web Scraping: Setup, Proxies and Servers

https://scrapfly.io/blog/posts/camoufox-for-web-scraping
8•AIWithLucas•46m ago•0 comments

Ask HN: Have you seen any outstanding GitHub repo?

2•skwasimin•47m ago•0 comments

Web Scraping in 2026: Six Insights from Pierluigi of the Web Scraping Club

https://scrapeops.io/blog/web-scraping-insights-from-pierluigi-of-the-web-scraping-club/
12•WesleyGrant782•47m ago•0 comments

Can You Explain a Zipper? Why AI Makes You Feel Smarter Than You Are [video]

https://www.youtube.com/watch?v=tqAangiY0FE
1•ykonstant•50m ago•0 comments

From Text Scripts to Animated Memes: A Guide to Creating GIFs with GPT Image 2.6

https://cloud.tencent.com/developer/article/2779922
1•yeungjipei•55m ago•0 comments

The Economics of Distrust: How the Market of Authenticity Consumed Itself

https://medium.com/@smoking2man/the-economics-of-distrust-how-the-market-of-authenticity-consumed...
1•Aldus551•55m ago•0 comments

Hire AI Agents and Pay in Monero (XMR)

https://agents.monero.jobs
1•CR1337•59m ago•0 comments

Show HN: Willder, an open source context layer that syncs apps, agents and teams

https://github.com/willderai/willder-core
2•NMTri1110•59m ago•0 comments

We're launching an in-game AI agent for WoW

https://willocompanion.com/
1•PatrickMoumiet•59m ago•1 comments

I was wrong about dark mode

https://dbushell.com/2026/11/09/dark-mode/
1•BrunoBernardino•1h ago•0 comments

BasicOboe: An easy to use but powerful Google Oboe library

https://github.com/tpkarras/BasicOboe
2•tpkarras•1h ago•1 comments