frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Debugging: Google requests permission to release 32M mosquitoes

https://www.theguardian.com/technology/2026/jun/01/google-permission-release-mosquitoes-californi...
1•ranit•26s ago•0 comments

I built a free AEO/GEO audit tool HTTPS://freeaiwebsiteaudit.com/

1•snowbirdsong•58s ago•0 comments

Bringing Goodnotes to the Web with Swift and WebAssembly

https://www.swift.org/blog/bringing-goodnotes-to-web-with-swift/
1•CharlesW•1m ago•0 comments

University System Went All in on A.I. Now It's Tearing Itself Apart

https://www.nytimes.com/2026/06/01/magazine/ai-university-college-california.html
1•jbredeche•3m ago•0 comments

Show HN: Glq LLM quantization using E8 lattice

https://github.com/cnygaard/glq
1•acd•4m ago•0 comments

Cognitive Packets for Agent Orchestration

https://github.com/JeanHuguesRobert/cogentia/blob/main/research/cognitive_packet_switching.md
1•jhrobert•4m ago•0 comments

Tokens Are the New Transistors

https://www.anantjain.xyz/posts/tokens-are-the-new-transistors
1•anant90•6m ago•0 comments

Show HN: Knotch – a hub-and-spoke voice agent

https://github.com/akshatvasisht/knotch
1•akshatvasisht•7m ago•1 comments

The Pope isn't AGI-pilled

https://www.theverge.com/ai-artificial-intelligence/937933/pope-ai-encyclical-tech-industry-react...
2•1vuio0pswjnm7•8m ago•0 comments

RNG: Flat Datacenter Networks at Scale

https://arxiv.org/abs/2604.15261
2•bschne•10m ago•0 comments

Rise of the Forward Deployed Engineer

https://newsletter.gtmengineering.ai/p/forward-deployed-engineering-101
2•alexjl1226•11m ago•1 comments

Lix v0.6 – Embeddable version control system for AI agents

https://twitter.com/samuelstroschei/status/2061531328517111826
2•samuelstros•12m ago•0 comments

Walt Disney's MultiPlane Camera (1957) [video]

https://www.youtube.com/watch?v=YdHTlUGN1zw
3•imnot404•15m ago•0 comments

Alphabet to Raise $80B in Equity Capital for Al Spending

https://www.bloomberg.com/news/articles/2026-06-01/alphabet-to-raise-80-billion-in-equity-capital...
3•di•16m ago•0 comments

Bank Balance Is a Lie, and Fragmentation Is Why Nobody Can Prove It

https://heavenslive.com
2•bbenevolence•21m ago•0 comments

Information Theory for Complex Systems [pdf]

https://annas-archive.pk/md5/2f7824216767bc996173fcf234842cc2
2•gdss•21m ago•0 comments

The Filamentary Funnels That Form Stars

https://www.universetoday.com/articles/the-filamentary-funnels-that-form-stars
2•leephillips•21m ago•0 comments

NuCS vs. Choco: a pure-Python solver meets a JVM veteran

https://github.com/yangeorget/nucs/blob/main/articles/NuCS%20vs%20Choco/nucs-vs-choco.mdsummar
2•yan_georget•22m ago•0 comments

Updates to GitHub Copilot billing and plans

https://github.blog/changelog/2026-06-01-updates-to-github-copilot-billing-and-plans/
2•agluszak•22m ago•0 comments

De Maquinas Y Seres Vivos – Humberto Maturana, Francisco Varela

https://z-library.im/book/8WkmBWGpJj/de-maquinas-y-seres-vivos.html
2•gdss•23m ago•0 comments

Alphabet Announces $80B Equity Capital Raise to Expand AI Infra and Compute

https://abc.xyz/investor/news/news-details/2026/Alphabet-Announces-Proposed-80-Billion-Equity-Cap...
31•gregschlom•26m ago•15 comments

Going multi-cloud with an in-housed status page

https://blog.railway.com/p/multi-cloud-status-page
4•ndneighbor•27m ago•0 comments

What "Memory Compiler" Means: From Bitcells to GDS Tiling

https://thecloudlet.github.io/technical/compiler/memory-compiler/
3•sillysaurusx•30m ago•0 comments

A court just decided how much foot movement counts as 'pedaling' an e-bike

https://electrek.co/2026/05/31/a-court-just-decided-how-much-foot-movement-counts-as-pedaling-an-...
3•Bender•30m ago•0 comments

Verkada Publicly Disparages Major Competitors with Private Google VC Doc

https://ipvm.com/reports/verkada-nps-comp
5•jhonovich•30m ago•0 comments

Bill Maher becomes latest liberal to throw support behind Spencer Pratt

https://www.dailymail.com/media/article-15865069/bill-maher-spencer-pratt-club-random-la-los-ange...
3•Bender•30m ago•0 comments

FastStream 0.7: MQTT support – in-memory tests, AsyncAPI generation and more

https://github.com/ag2ai/faststream/releases/tag/0.7.0
3•Lancetnik•32m ago•0 comments

Red Hat packages backdoored through its offical NPM channel

https://arstechnica.com/security/2026/06/dozens-of-red-hat-packages-backdoored-through-its-offica...
3•jjbinx007•35m ago•0 comments

AI music is flooding streaming services, but who wants it?

https://www.theverge.com/column/921599/ai-music-is-flooding-streaming-services-but-who-wants-it
3•1vuio0pswjnm7•39m ago•0 comments

Show HN: Mqtt Broker for 10 Years

https://github.com/skittleson/mqtt_broker_esp
3•skittleson•39m ago•0 comments