frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•8mo 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•8mo 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•8mo 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.

Myth of the Monolithic ERP: Why They Keep Failing [video]

https://www.youtube.com/watch?v=o6d94HNGV1s
1•rossdavidh•2m ago•0 comments

An A.I. Startup Says It Wants to Empower Workers, Not Replace Them

https://www.nytimes.com/2026/01/20/technology/humans-ai-anthropic-xai.html
1•bookofjoe•4m ago•1 comments

Testosterone went from prostate cancer villain to potential ally

https://theconversation.com/how-testosterone-went-from-prostate-cancer-villain-to-potential-ally-...
1•PaulHoule•6m ago•0 comments

Flashlabs releases the world’s first open-source voice cloning model

https://twitter.com/flashlabsdotai/status/2013993446047158550
1•sangwen•8m ago•0 comments

Show HN: iMessage-data-foundry – Synthetic iMessage Data Generator

https://github.com/johnlarkin1/imessage-data-foundry
2•jlarks32•10m ago•0 comments

Open4D – Open-Source 4D Geometry Processing, Compression and Streaming Library

https://github.com/SINRG-Lab/Open4D
1•hex823•12m ago•1 comments

Palantir CEO: With AI, economies won't need immigration

https://www.theregister.com/2026/01/21/palantir_ceo_karp_claims_ai/
1•abdelhousni•12m ago•0 comments

GPTZero finds 100 new hallucinations in NeurIPS 2025 accepted papers

https://gptzero.me/news/neurips/
1•dnw•13m ago•0 comments

MsgBored, Screaming into the Abyss

https://johntrager.net/projects/msg-bored/
2•jtrager•13m ago•0 comments

AI recruiters: faster, cheaper, and still clueless

https://pksunkara.com/thoughts/ai-recruiters-faster-cheaper-and-still-clueless/
1•pksunkara•14m ago•0 comments

Explore the Mandelbrot Set

https://math.hws.edu/eck/js/mandelbrot/MB.html
1•mooreds•14m ago•0 comments

Summary paper on the STAR-Vote system [pdf]

https://www.cs.rice.edu/~dwallach/pub/star-summative-2018.pdf
1•thechao•23m ago•0 comments

FCC: Late-night and daytime talk shows must offer equal time for candidates

https://www.nbcnews.com/politics/elections/fcc-late-night-daytime-talk-shows-equal-time-candidate...
1•ceejayoz•23m ago•0 comments

The divergence of centralized systems and individual agency

3•Kiplomat-SouCmp•25m ago•3 comments

Ukraine Holds Off on New Helsing Drone Orders After Setbacks

https://www.bloomberg.com/news/articles/2026-01-19/ukraine-holds-off-on-new-helsing-drone-orders-...
4•doener•26m ago•1 comments

Basic TTS – fast, free, and easy-to-use online text-to-speech tool

https://basictts.com/
2•sea-gold•26m ago•1 comments

Cyclic Subgroup Sum

https://m-slee.netlify.app/posts/cyclic-subgroup-sum
1•richard_chase•30m ago•1 comments

Being a tourist, can I film police in the US?

https://travel.stackexchange.com/questions/58566/being-a-tourist-can-i-film-police-in-the-us
2•beatthatflight•30m ago•2 comments

Show HN: HinkyPunk VPN

https://github.com/canaanmckenzie/HinkyPunk
2•prince_nez•31m ago•0 comments

List of Sales Closing Techniques

https://talnet.co/social/3KqvEuvb4p
1•bouia•33m ago•0 comments

Easy Delegation with C# Source Generators Library

https://davidvedvick.info/notes/2026/01/21/source-generators-easy-delegation-c-sharp
1•whoisthemachine•34m ago•0 comments

Tried Implementing DeepSeek's MHC

https://github.com/enochyearn/mhc-vs-resnet-mlx
1•enochyearn•35m ago•1 comments

Self-Hosting Discourse Just Got a Whole Lot Easier

https://meta.discourse.org/t/self-hosting-discourse-just-got-a-whole-lot-easier/393915
2•Curiositry•36m ago•0 comments

Cipher Copy – a fast, privacy-first text encoding tool (runs client-side)

https://cipher-06d59a47.base44.app/Encode
1•jasoncbuckley•37m ago•1 comments

Show HN: A fast, ad-free wiki for Where Winds Meet (built with Astro)

https://wwm-db.com/en/
1•causalzap•40m ago•0 comments

Bluementhals letter about ICE memo justifying entry into homes without warrant [pdf]

https://www.hsgac.senate.gov/wp-content/uploads/2026-01-21-Letter-from-Blumenthal-to-DHS-ICE.pdf
19•rawgabbit•42m ago•4 comments

Ask HN: How do you audit autonomous AI agent decisions?

1•credentum•44m ago•1 comments

AI Design Field Guide

https://www.aidesignfieldguide.com/
1•samuel246•44m ago•0 comments

Ask HN: Anyone seeing copy/paste reliability issues in ChatGPT Web on macOS?

1•sallyrideauto•47m ago•0 comments

What I Mean by "Dream Team"

https://theproductmindedqa.com/on-dream-teams/
1•sabdelrahman•48m ago•0 comments