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.

Are You Carrying Technical Debt Without Realizing It?

https://www.techneedsolutions.com/insights/are-you-carrying-technical-debt-without-realizing-it
1•ramzez•32s ago•0 comments

How we protected MEGA Pass against clickjacking attacks

https://blog.mega.io/how-we-protected-mega-pass-against-clickjacking-attacks
2•dotcoma•1m ago•0 comments

Mass Immigration: All Arguments Ranked and Debunked [video]

https://www.youtube.com/watch?v=2CVuL-QpvQs
1•joe_mamba•2m ago•0 comments

Tracking AI's Contribution to GDP Growth

https://www.stlouisfed.org/on-the-economy/2026/jan/tracking-ai-contribution-gdp-growth
1•giuliomagnifico•2m ago•0 comments

Make It Right

https://www.stephenlewis.me/blog/make-it-right/
1•monooso•3m ago•0 comments

Mediaite Starts a Newsletter to Summarize Media Newsletters

https://www.nytimes.com/2026/01/21/business/media/mediaite-media-newsletter.html
1•bookofjoe•3m ago•1 comments

Invisible Modes on Steam and Discord Are Useless

1•xmrcat•4m ago•0 comments

Ask HN

1•ZJPhillips•5m ago•0 comments

My local news site redirects you to a third party website for ad blockers

https://www.clintmcmahon.com/Blog/ad-blocker-redirect-kstp
1•speckx•7m ago•0 comments

Fighter Mafia

https://en.wikipedia.org/wiki/Fighter_Mafia
1•azhenley•8m ago•0 comments

(Parade DB) How We Made Writes 10x Faster for Search

https://www.paradedb.com/blog/increased-write-performance
1•dhruv_ahuja•8m ago•0 comments

Show HN: A memory learning layer for AI agents to learn on the job

https://www.versanovatech.com
4•gauravsc•9m ago•0 comments

The First 30 Seconds Now Decide Everything on YouTube – YouTube Algorithm 2026

https://fluxzap.com/the-first-30-seconds-now-decide-everything-on-youtube-youtube-algorithm-2026/
1•taimurkazmi•10m ago•0 comments

Malan Chat, a full immersion language learning app for 62 languages

3•sam_osterfeld•11m ago•0 comments

Show HN: NetHackPlayer – Have Claude Play NetHack

https://github.com/pj4533/NetHackPlayer
1•pj4533•12m ago•0 comments

Show HN: Segre – Safe CLI to organize messy folders by type or date

https://www.npmjs.com/package/segre
1•shubhampawade•13m ago•0 comments

The Data Center as a Computer: Designing Warehouse-Scale Machines, Edition 4

https://play.google.com/store/books/details/The_Data_Center_as_a_Computer_Designing_Warehouse_?id...
1•tanelpoder•17m ago•1 comments

Google won't stop replacing our news headlines with terrible AI

https://www.theverge.com/tech/865168/google-says-ai-news-headlines-are-feature-not-experiment
2•cdrnsf•18m ago•0 comments

Grok floods X with sexualized images of women and children

https://counterhate.com/research/grok-floods-x-with-sexualized-images/
3•taimurkazmi•19m ago•1 comments

Parents might age faster or slower based on how many kids they have

https://www.scientificamerican.com/article/parents-might-age-faster-or-slower-based-on-how-many-k...
1•Brajeshwar•20m ago•1 comments

Plant Produces Plump, Fake Berries to Trick Birds into Spreading Its Offspring

https://www.smithsonianmag.com/smart-news/this-plant-produces-plump-fake-berries-to-trick-birds-i...
2•Brajeshwar•20m ago•0 comments

We Have No Idea How to Code. So We Got Claude to Code This Article for Us

https://www.wsj.com/tech/ai/anthropic-claude-vibe-coding-experiment-a4a3bb0f
1•stefap2•22m ago•0 comments

Drug capable of reversing spinal cord injuries is in the clinical trial

https://ensaiosclinicos.gov.br/news/546
1•dlojudice•22m ago•1 comments

Maybe my own RSS Reader could be useful for you too

https://www.whileforloop.com/en/blog/2026/01/23/feedy-rss-reader-for-e-ink/
2•lukas346•23m ago•1 comments

Best Hacker News videos normalized by number of HN users

https://sql.clickhouse.com?query=V0lUSCB5ZWFybHlfdXNlcnMgQVMgKAogICAgU0VMRUNUCiAgICAgICAgdG9ZZWFy...
1•eamag•24m ago•0 comments

Tiny falcons are helping keep the food supply safe on cherry farms

https://insideclimatenews.org/news/22012026/michigan-cherry-farms-american-kestrel-food-safety/
3•duxup•24m ago•0 comments

Pandas 3.0

https://pandas.pydata.org/community/blog/pandas-3.0.html
4•jonbaer•27m ago•0 comments

Databases Don't Know Why You're Asking

https://inferal.com/blog/databases-dont-know-why/
1•refset•28m ago•0 comments

Scribe reduces agent token usage by 30% with no loss of accuracy

https://sibylline.dev/articles/2026-01-22-scribe-swebench-benchmark/
1•CuriouslyC•28m ago•0 comments

Portable Cartesian brace expansion in your shell

https://neosmart.net/blog/portable-cartesian-brace-expansion-in-your-shell/
1•ComputerGuru•29m ago•0 comments