frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Bug bounty businesses bombarded with AI slop

https://arstechnica.com/ai/2026/05/bug-bounty-businesses-bombarded-with-ai-slop/
1•helsinkiandrew•41s ago•0 comments

Dad Books Are a Dying Breed

https://www.wsj.com/business/media/dad-books-are-a-dying-breed-d9a28b49
1•Caiero•1m ago•0 comments

Samaris OS: A Bootable Linux-Based OS with React UI, Rust Daemons and Local AI

https://github.com/btkhaled/SamarisOS
1•btkhaled•2m ago•0 comments

Google DeepMind's Demis Hassabis emerges as early Anthropic investor

https://www.ft.com/content/8f2a529e-7a1b-4d8e-95be-338d0c4c98f5
1•merksittich•3m ago•0 comments

Adminbolt – manage your hosting via WhatsApp with an AI agent

https://adminbolt.com/blog/manage-hosting-via-whatsapp/
1•jvrpl•5m ago•0 comments

Deterministic vs. Probabilistic Code Generation

https://thetechenabler.substack.com/p/deterministic-vs-probabilistic-code
1•ingve•6m ago•0 comments

Summer Bracelets for Travel, Beach Days and Everyday Wear

https://glintandluck.com/blogs/news/the-best-summer-bracelets-for-travel-beach-days-everyday-wear
1•Chris_taka•7m ago•0 comments

Highly critical security release for Drupal

https://www.drupal.org/psa-2026-05-18
1•huhhuh•8m ago•1 comments

We enable full customization in a front end runtime engine

https://uigen-docs.vercel.app/blog/override-system-technical-deep-dive
1•ombedzi•12m ago•1 comments

iOS-Linuxkit: Linux on iPad, the Hard Way

https://taoofmac.com/space/blog/2026/05/16/1130
1•rcarmo•14m ago•0 comments

Tracking 100k Assets Before Having 100k Assets to Track

https://dunkels.com/adam/city-scale-iot-asset-tracking-simulator/
1•adunk•14m ago•0 comments

The quiet ones among us

https://timesofindia.indiatimes.com/blogs/civil-irony/the-quiet-ones-among-us/
1•abhikul0•14m ago•0 comments

Amazon Cuts Affiliate Commissions Up to 50% for Publishers

https://www.adweek.com/media/amazon-associates-affiliate-rate-cuts-publishers/
2•thm•17m ago•0 comments

I vibecoded my dream game, geoguesser but for guns

https://gunguesser.com
1•abhahbnar•19m ago•0 comments

Show HN: Fast_copy new release with new features

https://github.com/gekap/fast-copy
1•yskapell•20m ago•0 comments

Users turn to jailbreaking their older Kindles as Amazon ends support

https://techcrunch.com/2026/05/16/users-turn-to-jailbreaking-their-older-kindles-as-amazon-ends-s...
1•gluke_bywalker•20m ago•0 comments

A Lesson from the Cockpit

https://www.subbu.org/essays/2026/a-lesson-from-the-cockpit/
1•r4um•21m ago•0 comments

GhostCue: A teleprompter only visible to you while screen recording

https://ghostcue.app/
1•JMiao•22m ago•0 comments

Chat-first AI ads Gen – paste a URL, get a full campaign

https://www.heyad.ai/login?mode=signup
1•cjdesignstudio•24m ago•0 comments

Poland urges officials to ditch Signal for state-run messaging apps

https://cyberinsider.com/poland-urges-officials-to-ditch-signal-for-state-run-messaging-apps/
1•Cider9986•31m ago•0 comments

I'm building a SaaS with ZERO AI features in 2026

2•postauth•35m ago•0 comments

I built an independent SEO tool review site. The honest data after 2.5 months

https://www.indiehackers.com/post/i-built-an-independent-seo-tool-review-site-while-working-full-...
1•digispecial8x•36m ago•0 comments

Migrate from Stainless to APIMatic CodeGen

https://migrate-from-stainless.apimatic.io/
1•m3h•43m ago•0 comments

A NixOS GUI

https://kalken.github.io//ezblog/#ezconf
1•linux4dummies•44m ago•0 comments

Why is rendering text so complicated? [video]

https://www.youtube.com/watch?v=4soZ33MvlW4
1•theis_once•47m ago•0 comments

Humble Tech Book Bundle: The Ultimate Functional Programming

https://www.humblebundle.com/books/ultimate-functional-programming-pragmatic-programmers-books
6•Tomte•51m ago•0 comments

End of a Semester

https://mtendekuyokwa.github.io/blog/end-of-semester/
2•sonderotis•54m ago•1 comments

Using Claude to apply a book, not just summarize it

https://lswith.io/posts/how-i-approached-showing-your-work/
2•lswith•54m ago•0 comments

Automate the Food (2024)

https://chisness.substack.com/p/automate-the-food
2•thelastgallon•57m ago•0 comments

SpaceX IPO set to ensure Elon Musk cannot be fired

https://www.ft.com/content/00382ab9-3dfe-468c-8966-853cd787dd43
3•1vuio0pswjnm7•58m ago•0 comments