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.

Raising money fucked me up

https://blog.yakkomajuri.com/blog/raising-money-fucked-me-up
1•yakkomajuri•1m ago•0 comments

AWS databases now live on Vercel Marketplace and v0

https://vercel.com/blog/aws-databases-are-now-live-on-the-vercel-marketplace-and-v0
1•braebo•2m ago•0 comments

Show HN: Interactive timeline of US Military bases and interventions, 1900–today

https://metaphorician.com/american-empire
1•metaphorician•2m ago•0 comments

QucsStudio: A free and powerful circuit simulator

https://qucsstudio.de/
1•teleforce•3m ago•0 comments

I Used Claude to Build a Transcription Bot That Learns from Its Mistakes

https://www.macstories.net/stories/how-i-used-claude-to-build-a-transcription-bot-that-learns-fro...
1•tosh•3m ago•0 comments

Reverse Engineering a Biometric Attendance Machine

https://www.onlydarshan.com/posts/first_blog
1•leon3rd•5m ago•0 comments

ICML Experimental Program Using Google's Paper Assistant Tool (PAT)

https://blog.icml.cc/2026/01/14/icml-experimental-program-using-googles-paper-assistant-tool-pat/
1•fahrbach•5m ago•0 comments

Show HN: Upseated – Swap airplane seats with other passengers

https://upseated.com/
1•ndshrubs•6m ago•0 comments

Automation Isn't Innovation

2•geroge_kyaw•10m ago•0 comments

Writes vs. Talks About Software

https://substack.com/inbox/post/184880745
3•markferree•10m ago•0 comments

Trump vows tariffs on eight European nations over Greenland

https://www.reuters.com/world/europe/trump-vows-tariffs-eight-european-nations-over-greenland-202...
3•TechTechTech•12m ago•0 comments

My free biohacking database with AI matching turns 1

https://dopamine.club/
2•ainthusiast•16m ago•1 comments

Worse Than the Dot Com Bubble

https://www.wheresyoured.at/dot-com-bubble/
2•abhi_kr•17m ago•1 comments

Ask HN: Best book(s) to learn Assembly as a first language

1•abkt•17m ago•0 comments

AI beyond LLMs: a wearable foundation model

https://www.empirical.health/blog/wearable-foundation-model-jets/
1•brandonb•18m ago•0 comments

America at 250

https://www.economist.com/interactive/america-at-250
2•andsoitis•20m ago•0 comments

GitHub Space Shooter turns GitHub contribution graphs into space shooter

https://github.com/czl9707/gh-space-shooter
1•ohjeez•20m ago•0 comments

The Charts that Explain 2025

https://hbr.org/2025/12/the-hbr-charts-that-help-explain-2025
2•gmays•21m ago•0 comments

Do Institutional Investors Raise Housing Prices?

https://nicholasdecker.substack.com/p/do-institutional-investors-raise
2•JumpCrisscross•21m ago•0 comments

Show HN: Play poker with friends in the browser – with built-in video chat

https://kosmi.io/poker/
1•hauxir•22m ago•0 comments

Show HN: Nelson Muntz Claude Code Plugin

https://github.com/zkarimi22/nelson-muntz
1•zknowledge•24m ago•0 comments

Designing a Key-Value Store(2021)

https://yusufaytas.com/designing-a-key-value-store/
5•guinesscoder•24m ago•0 comments

Show HN: Sitdown Instead of Stand Ups

https://www.getsitdown.com
1•jarlen•26m ago•0 comments

Show HN: How I stopped ChatGPT from manipulating me

1•axismundi•26m ago•0 comments

Should You Be 'Fibermaxxing'?

https://www.nytimes.com/2025/07/08/style/fibermaxxing-tiktok-trend.html
3•brandonb•27m ago•1 comments

Crusade against usury reaches Wall Street

https://www.economist.com/finance-and-economics/2026/01/14/donald-trumps-crusade-against-usury-re...
3•andsoitis•27m ago•0 comments

Show HN: Frigatebird – analytical SQL engine built from first principles

https://github.com/Frigatebird-db/frigatebird
2•nottorus•28m ago•0 comments

JSON-Render: AI –> JSON –> UI

https://json-render.dev/
1•michaelmior•30m ago•0 comments

Multiplicity of the Soul: Time Travel

https://atmankalena.substack.com/p/multiplicity-of-the-soul-time-travel
2•Trifectorium•30m ago•1 comments

Invisibility is the maintainer's reward for competence

https://www.joanwestenberg.com/the-rime-of-the-ancient-maintainer/
1•danielfalbo•32m ago•0 comments