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.

Tesla's Self-Driving Has Gotten Amazing

https://pogueman.substack.com/p/dammit-teslas-self-driving-has-gotten
1•andsoitis•6m ago•0 comments

Google offers buyouts to staff in its business unit who aren't 'all in'

https://www.businessinsider.com/google-offers-exit-packages-to-some-business-unit-employees-2026-2
1•samaysharma•8m ago•0 comments

Indian teacher who created hundreds of learning centers wins $1M Global Prize

https://apnews.com/article/global-teacher-prize-rouble-nagi-54a0fbd628ab062aba569457ff01d21d
1•thunderbong•10m ago•0 comments

ModernBERT in Jax/Flax

https://github.com/arnavw/modernbert-flax
1•alien0006•13m ago•0 comments

My team lead wants 25% of the total bonus pool for himself

https://old.reddit.com/r/founder/comments/1r2lj1g/my_team_lead_wants_25_of_the_total_bonus_pool_for/
1•fanux•14m ago•0 comments

AI Assisted Linguistic Synth:Mapping Proto-Sinaitic Roots in Voynich Manuscript

https://github.com/adrihd/voyters
1•adrihd•15m ago•1 comments

Inside the Secret Smear Machine That's Targeting Hollywood

https://www.hollywoodreporter.com/business/business-news/inside-hollywood-smear-campaign-scandals...
1•mikhael•15m ago•0 comments

Software Is User Action

https://software-action.hyperclay.com/
2•YounesDz•24m ago•0 comments

Separation of Concerns: You're Cutting Along the Wrong Line

https://inferal.com/blog/separation-of-concerns/
1•donutshop•30m ago•0 comments

The Farm That 600M IP Addresses Called Home

https://www.connected.app/library/the-farm-that-600-million-ip-addresses-called-home-1u5uogt
2•kirkouimet•31m ago•0 comments

Bay Area tech CEO given $3.5M severance payment as company lays off hundreds

https://www.sfgate.com/tech/article/workday-severance-layoffs-21346064.php
5•starkparker•31m ago•2 comments

Terence Tao: Machine Assistance and the Future of Research Mathematics [video]

https://www.youtube.com/watch?v=SuTxpKggY30
1•20repsquat•37m ago•1 comments

Show HN: Motivé – AI-generated cover letters tailored to job descriptions

https://motive8.ca
1•joelkapuku•39m ago•0 comments

The Royal Game of Ur [video]

https://www.youtube.com/watch?v=WZskjLq040I
1•theGeatZhopa•40m ago•0 comments

Show HN: Self-updating engineering blogs repo with GitHub Actions

https://github.com/mayankchauhan971/engineering-blogs
2•break_prod•45m ago•0 comments

Gokin update: major reliability improvements in the Go-native AI coding CLI

https://github.com/ginkida/gokin
1•ginkida•48m ago•0 comments

1.3M Epstein documents index on Postgres

https://old.reddit.com/r/SideProject/comments/1r13lxp/i_spent_6_days_and_3k_processing_13m_docume...
3•jerdman76•48m ago•0 comments

Show HN: Crank – The SSH Terminal Manager for Engineers Who Refuse to Close Tabs

https://github.com/nexivibe/crank
4•mathgladiator•52m ago•0 comments

Warcraft III Peon Voice Notifications for Claude Code

https://github.com/tonyyont/peon-ping
3•doppp•52m ago•0 comments

Show HN:ProductFront-Streamlined product discovery platform for maximum exposure

https://www.productfront.tech
1•kdev28•53m ago•0 comments

D Programming Language

https://dlang.org/
7•arcadia_leak•53m ago•2 comments

Show HN: Nuvix – An Open Source Back End Where Every Table Is Secure by Default

https://github.com/Nuvix-Tech/nuvix
1•ravikantsaini•54m ago•0 comments

How to Use Google Trends and SEO Tools to Brainstorm Blog Topics

https://kitful.ai/blog/how-to-use-google-trends-and-seo-tools-to-brainstorm-blog-topics
1•eashish93•56m ago•0 comments

Show HN: SpearTrades – Crypto risk, health and portfolio analytics

https://speartrades.com
1•sumitarya•1h ago•0 comments

Programming Aphorisms

https://matklad.github.io/2026/02/11/programming-aphorisms.html
2•signa11•1h ago•0 comments

The "Are You Sure?" Problem: Why Your AI Keeps Changing Its Mind

https://www.randalolson.com/2026/02/07/the-are-you-sure-problem-why-your-ai-keeps-changing-its-mind/
1•EvgeniyZh•1h ago•0 comments

Show HN: Cross-platform audio notifications for Claude Code

https://github.com/ChanMeng666/claude-code-audio-hooks
1•chanmeng666•1h ago•0 comments

Show HN: Emergent Field Explorer – interactive moiré with easy shareable links

https://mcbgl.vercel.app/?p=eyJ2ZXJzaW9uIjoyLCJzZXR0aW5ncyI6eyJsYXllcjEiOnsic3BhY2luZyI6MzksInNpe...
1•mcdowell_atx•1h ago•0 comments

My wife calls me, panicked

https://twitter.com/ModernDad/status/2021675945581486309
9•latchkey•1h ago•3 comments

Ask HN: How do you deal with long Covid?

4•manx•1h ago•2 comments