frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Data from a phone becomes a death sentence

https://www.jpost.com/defense-and-tech/article-895697
1•iugtmkbdfil834•1m ago•0 comments

One Agent Sandbox Is Not Enough

https://multikernel.io/2026/03/25/sandlock-mcp-per-tool-sandboxing/
1•kjok•2m ago•0 comments

Researchers tune Casimir force using magnetic fields

https://phys.org/news/2024-06-tune-casimir-magnetic-fields.html
1•bilsbie•2m ago•0 comments

DMA: A Community Hackerspace in Seattle

https://dma.space/
1•bleeperblooper•6m ago•0 comments

YA3 – Yet Another 303 clone, that runs in the browser and as a DAW plugin

https://ya3.surge.sh/
1•stagas•6m ago•0 comments

Synthetic Exercise World – Fictional dataset for cyber exercises and standards

https://github.com/MISP/Synthetic-Exercise-World-Format
1•adulau•8m ago•0 comments

Humanoid Robots Are the Next Phase of the AI Hype Cycle

https://www.bloomberg.com/news/articles/2026-05-08/humanoid-robots-aren-t-as-advanced-as-the-ai-h...
2•wslh•10m ago•1 comments

Lies, damned lies, and Elastic's benchmarks

https://www.gouthamve.dev/lies-damned-lies-and-elastics-benchmarks/
1•gsky•13m ago•0 comments

Stoa – open substrate for agent-readable SaaS (spec, runtime, registry)

https://stoa.tryvext.com/
1•alayton•15m ago•0 comments

Ask HN: Would you use a verified social platform?

2•jacobwiseberg•18m ago•2 comments

Bottleneck – The Strait of Hormuz Crisis Game

https://bottleneck.jakubgornicki.com/en
1•smartmic•20m ago•0 comments

MIT researchers revive 40 year old triangular zipper concept

https://www.tomshardware.com/3d-printing/mit-researchers-revive-40-year-old-triangular-zipper-con...
2•MassPikeMike•22m ago•0 comments

Goblin Hunt

https://blog.leloew.com/goblin-hunt/
1•lysecret•28m ago•0 comments

Court Awards Aylo $4.2M, Not $84M, in Pornhits Piracy Case

https://torrentfreak.com/court-awards-aylo-4-2-million-not-84-million-in-pornhits-piracy-case/
2•Brajeshwar•28m ago•0 comments

Voyager FDS Emulator

https://zanehambly.com/voyager
1•vsrinivas•31m ago•1 comments

COBOL Is the Asbestos of Programming Languages

https://www.wired.com/story/cobol-is-the-asbestos-of-programming-languages/
3•birdculture•31m ago•0 comments

Show HN: Mosaic – sort iOS icons by color using an evolutionary algorithm

https://github.com/RTiK/mosaic
1•artkh•32m ago•0 comments

Hacking Tesla, so the doors open mechanically in an emergency

https://www.thestar.com/news/canada/i-couldnt-live-with-the-idea-of-my-kids-trapped-in-my-tesla-h...
2•ranit•35m ago•1 comments

Show HN: SimplyBudget, AI Finance Tracker

https://simplybudget.framer.ai/
1•zaintechsavvy•35m ago•0 comments

Show HN: KI im Mittelstand oder KI-Frustration? inkl. Demo

https://christopher-helm.com/einfuehrung-von-ki-im-mittelstand/
1•chelm•37m ago•1 comments

Adventures in Science Fiction Cover Art: Inside the Skull

https://sciencefictionruminations.com/2012/08/31/adventures-in-science-fiction-cover-art-inside-t...
1•jruohonen•38m ago•0 comments

There are only two paths left for software

https://www.a16z.news/p/there-are-only-two-paths-left-for
2•noashavit•39m ago•1 comments

Generative Recommendation for Large-Scale Advertising

https://arxiv.org/abs/2602.22732
1•mfiguiere•40m ago•0 comments

How FAANG Became General Electric

https://danunparsed.com/p/how-faang-became-general-electric
1•sambellll•46m ago•1 comments

ESP-IDF 6 Setup in VS Code: Blink and Debug an ESP32-S3 [video]

https://www.youtube.com/watch?v=y2pIqbZa-iA
1•iamflimflam1•46m ago•0 comments

Crack and Crab

https://languagelog.ldc.upenn.edu/nll/?p=73489
1•jruohonen•47m ago•0 comments

Beat – open-source screenwriting app for Mac/iOS

https://www.beat-app.fi/
1•tekkk•52m ago•0 comments

What happens when we lose a language?

https://www.theguardian.com/science/2026/may/10/what-happens-when-we-lose-a-language
2•saikatsg•56m ago•1 comments

Ask HN

1•Sxouterred•58m ago•2 comments

Ubiquitous, but in Which Language?

https://docs.eventsourcingdb.io/blog/2026/05/11/ubiquitous-but-in-which-language/
1•goloroden•58m ago•0 comments