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.

A New Home for the Tiffany "Garden Landscape" Window [video]

https://www.youtube.com/watch?v=KfdWikGUT5Q
1•bookofjoe•39s ago•0 comments

Perception and Reality

https://twitter.com/johndefeo/status/2012128684459876573
1•lleddell•1m ago•0 comments

Efficiently sorting gigabytes: the Deferred Merge Sort

https://superluminal.eu/efficiently-sorting-gigabytes-the-deferred-merge-sort/
1•ibobev•3m ago•0 comments

Seestar ALP Launcher

https://www.4rknova.com//blog/2026/01/18/seestar-alp-debian
1•ibobev•3m ago•0 comments

Former USDS Leaders Launch Tech Reform Project to Fix What DOGE Broke

https://www.wired.com/story/former-usds-leaders-launch-tech-reform-project-doge/
1•brandonb•4m ago•0 comments

Integrating post-growth economics into transformative adaptation

https://www.sciencedirect.com/science/article/pii/S0921800925003787
1•robtherobber•4m ago•0 comments

Designing 'One Hundred and One Dalmatians'

https://animationobsessive.substack.com/p/designing-one-hundred-and-one-dalmatians
1•vinhnx•4m ago•0 comments

More than 100k people urge MPs to ban social media for under-16s in UK

https://www.theguardian.com/media/2026/jan/15/people-urge-mps-ban-social-media-under-16s-uk-starmer
1•pmg101•5m ago•1 comments

Sweden deploys fighter jets to Iceland

https://www.sverigesradio.se/artikel/sverige-skickar-stridsflyg-till-island
2•madspindel•5m ago•0 comments

3,500 Miles in 2025

https://danielmangum.com/posts/3500-miles-2025/
1•hasheddan•8m ago•0 comments

Remembering Beyond Vietnam on MLK Day

https://werd.io/remembering-beyond-vietnam-on-mlk-day/
1•benwerd•10m ago•0 comments

Trump links Greenland dispute to not getting Nobel Peace Prize

https://www.bbc.co.uk/news/articles/cy7mev35x2lo
6•ndsipa_pomu•10m ago•1 comments

25 Years of IntelliJ Idea

https://www.jetbrains.com/lp/intellij-idea-25/
1•bartekpacia•12m ago•0 comments

Engaging healthily with chess: an Acceptance and Commitment therapist's guide

https://lichess.org/@/tackyshrimp/blog/engaging-healthily-with-chess-an-acceptance-and-commitment...
1•guytv•13m ago•0 comments

Just Fucking Validate Your Startup Idea

https://justfuckingvalidateyourstartupidea.com/
1•mehulfanawala•15m ago•0 comments

Making file synchronization safer with confirmation and protocol validation

https://www.bytesyncapp.com/blog/bytesync-2026-1-released-confirm-before-sync-safer-sessions-and-...
1•paulfresquet•15m ago•0 comments

Show HN: I built a playground for image and video models

https://mitte.ai
1•akoculu•15m ago•0 comments

A new open-source benchkit to create your own reproducible database benchmarks

https://www.exasol.com/blog/reproducible-database-benchmarks-benchkit/
8•exagolo•15m ago•0 comments

Predicting Best Picture at the 2026 Academy Awards

https://www.r-bloggers.com/2026/01/predicting-best-picture-at-the-2026-academy-awards/
1•7777777phil•18m ago•0 comments

Designing Better Adventures

https://quarter--mile.com/designing-better-adventures
1•surprisetalk•19m ago•0 comments

For the Minimalist: Best, Lightest, Smallest, and Cheapest Money Clip

https://bookofjoe2.blogspot.com/2025/12/for-minimalist-worlds-best-lightest.html
1•surprisetalk•19m ago•0 comments

Georgism Through Land Leasing

https://progressandpoverty.substack.com/p/georgism-through-land-leasing
1•surprisetalk•19m ago•0 comments

Trustworthy technology is available now, but will we decide to use it?

https://aol.codeberg.page/eci/
1•mixmastamyk•20m ago•0 comments

Ask HN: Where to find VC fund or investor for project in Europe?

3•nicksbg•20m ago•0 comments

Show HN: I got tired of spammy calculator sites, so I built cpm-calculator.io

https://cpm-calculator.io/
1•cby821555203•20m ago•0 comments

They Wanted a University Without Cancel Culture. Then Dissenters Were Ousted

https://www.politico.com/news/magazine/2026/01/16/civil-war-university-of-austin-bari-weiss-00729...
2•Tomte•21m ago•0 comments

Trump tells Norway's PM he has no obligation to 'think purely of peace'

https://www.euronews.com/2026/01/19/trump-tells-norways-pm-he-has-no-obligation-to-think-purely-o...
7•Tomte•24m ago•0 comments

Show HN: I made AI as easy as sending an email

https://emailmcp.co
1•ihmissuti•24m ago•0 comments

The battle against bad bread had a role in women's lib

https://worksinprogress.co/issue/fermenting-revolution/
3•bensouthwood•24m ago•0 comments

Nushell: Dataframes

https://www.nushell.sh/book/dataframes.html
1•tosh•24m ago•0 comments