frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: Bridging the gap between SIP infrastructure and real-time AI

https://leilani.dev
1•kfeeney•2m ago•0 comments

Peter Thiel: Capitalism Isn't Working for Young People

https://www.thefp.com/p/peter-thiel-capitalism-isnt-working-for-young-people
1•ctoth•2m ago•0 comments

Ask HN: How do you know if a job is likely to be a ghost job?

1•AznHisoka•2m ago•0 comments

Snippyst – Share and discover Typst snippets with the community

https://snippyst.com
1•schu•2m ago•0 comments

Keep on fishing, while my app logs your catches

https://apps.apple.com/de/app/fishid-cam-fang-log-mit-ki/id6753859615
1•Appventurer•3m ago•1 comments

Show HN: Pingu Unchained an Unrestricted LLM for High-Risk AI Security Research

https://pingu.audn.ai
1•ozgurozkan•5m ago•1 comments

2025.45: Frothiness and the Future

https://stratechery.com/2025/frothiness-and-the-future/
1•feross•7m ago•1 comments

PgPointcloud – A PostgreSQL extension for storing point cloud (Lidar) data

https://pgpointcloud.github.io/pointcloud/
1•steve-chavez•9m ago•0 comments

New Talking Postgres Podcast Ep33 about Postgres and VS Code with Rob Emanuele

https://talkingpostgres.com/episodes/building-a-dev-experience-for-postgres-in-vs-code-with-rob-e...
1•clairegiordano•11m ago•0 comments

EU, China Said to Join Brazil-Led Carbon Market Coalition (COP30)

https://www.bloomberg.com/news/articles/2025-11-07/eu-china-said-to-join-brazil-led-carbon-market...
1•standardUser•14m ago•0 comments

The Relentless Pursuit of the Vorschlag

https://vomtag.com/entry/The-Relentless-Pursuit-of-the-Vorschlag
1•saltysalt•15m ago•0 comments

Warpstock 2025 (OS/2, ArcaOS)

https://www.youtube.com/channel/UCNKJf6d69jWt4AD41yEgawQ
1•todsacerdoti•16m ago•0 comments

The Privacy Manifesto

https://zac1258019.substack.com/p/the-privacy-manifesto
1•rapawel•17m ago•0 comments

An AI model compressed an encyclopedia into an image

https://readmultiplex.com/2025/10/20/an-ai-model-just-compressed-an-entire-encyclopedia-into-a-si...
1•delichon•18m ago•0 comments

YouTube Removes Windows 11 Bypass Tutorials, Claims 'Risk of Physical Harm'

https://news.itsfoss.com/youtube-removes-windows-11-bypass-tutorials/
10•WaitWaitWha•20m ago•2 comments

Mastodon 4.5

https://blog.joinmastodon.org/2025/11/mastodon-4.5/
5•birdculture•20m ago•0 comments

Website Monitors Bus Drivers Who Exceed Speed Limits in Helsinki

https://2fast2hsl.live/
1•jnieminen•26m ago•0 comments

Democrats name their price on ending government shutdown

https://www.axios.com/2025/11/07/senate-democrats-proposal-end-government-shutdown
1•SilverElfin•26m ago•0 comments

An abandoned tent has spurred a hiking mystery in New Hampshire

https://www.boston.com/news/local-news/2025/11/06/abandoned-tent-hiking-mystery-new-hampshire/
2•mykowebhn•26m ago•0 comments

We Used To Actually Own Our Computers.

https://www.youtube.com/watch?v=hF0NKvmQmVA
5•sarimkx•27m ago•1 comments

The File Search Tool in Gemini API

https://blog.google/technology/developers/file-search-gemini-api/
1•tsenturk•28m ago•0 comments

Computational Turing test shows systematic difference between human, AI language

https://arxiv.org/abs/2511.04195
1•anigbrowl•31m ago•0 comments

Cartolina: 3D Terrain Cartography for the Web

https://cartolina.dev/
1•gaws•32m ago•0 comments

The Medici Method

https://www.palladiummag.com/2025/11/07/the-medici-method/
2•sebg•33m ago•0 comments

Rovina's Choice

https://www.newyorker.com/culture/the-new-yorker-documentary/the-shutdown-of-usaid-has-already-ki...
2•perihelions•33m ago•0 comments

Scientists study plant with highest known heat tolerance in Death Valley

https://www.sfgate.com/bayarea/article/calif-scientists-groundbreaking-study-death-valley-2114361...
2•mykowebhn•36m ago•0 comments

Petri AI Testing 'Closes' possible solution without looking

https://github.com/safety-research/petri/issues/20
1•Utharian•36m ago•0 comments

ChineseNationals from UMich lab charged for smuggle biomaterials into US

https://www.justice.gov/usao-edmi/pr/three-chinese-national-scholars-university-michigan-laborato...
3•737min•38m ago•2 comments

AI Tools that I've Seen in the Wild

https://aplaceofmind.notion.site/AI-Tools-that-I-ve-Seen-in-the-Wild-2a40a6eeb81d80a89530daa4d741...
1•behnamoh•38m ago•0 comments

VLC's Jean-Baptiste Kempf Receives the European SFS Award 2025

https://fsfe.org/news/2025/news-20251107-01.en.html
20•kirschner•39m ago•0 comments