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.

Post-Micturition Convulsion Syndrome

https://en.wikipedia.org/wiki/Post-micturition_convulsion_syndrome
1•thunderbong•1m ago•0 comments

Google shows small models analyze smartphone screens to predict what users want

https://research.google/blog/small-models-big-results-achieving-superior-intent-extraction-throug...
1•rexbee•5m ago•0 comments

The Uncomfortable Math of Working for Yourself

https://thomasunise.com/the-uncomfortable-math-of-working-for-yourself/
2•eeko_systems•7m ago•0 comments

A Massacre in Mashhad

https://www.newyorker.com/news/as-told-to/a-massacre-in-mashhad
1•petethomas•8m ago•0 comments

What Margaret Atwood Would Like You to Know

https://newrepublic.com/article/204118/margaret-atwood-like-know-book-lives-memoir-review
1•petethomas•8m ago•1 comments

Lilliputian Hallucinations

https://www.sciencedirect.com/science/article/pii/S0149763421001068
1•rammy1234•10m ago•0 comments

Show HN: gRPC Transport for HashiCorp/Raft

https://github.com/dhiaayachi/raft-grpc-transport
1•neo2006•13m ago•0 comments

Tesla launches robotaxi rides in Austin with no human safety driver

https://techcrunch.com/2026/01/22/tesla-launches-robotaxi-rides-in-austin-with-no-human-safety-dr...
1•lateforwork•13m ago•0 comments

Slonk: Slurm on Kubernetes for ML Research at Character.ai

https://blog.character.ai/slonk/
1•gmays•17m ago•0 comments

Curl shutters bug bounty program to remove incentive for submitting AI slop

https://www.theregister.com/2026/01/21/curl_ends_bug_bounty/
1•botanicals6•18m ago•1 comments

Human OS for AI

https://humanreadiness.org
1•cp248•19m ago•0 comments

Hotels Are Getting Rid of Proper Bathroom Doors and Guests Are Revolting

https://www.wsj.com/lifestyle/travel/hotels-bathroom-no-doors-8cb4bf96
1•Geekette•20m ago•2 comments

I Don't Want Your Business Card

https://benhouston3d.com/blog/I-dont-want-your-business-card
1•bhouston•23m ago•1 comments

The Internet Is Dead

https://anihil.com/the-internet-is-dead
1•anihil•26m ago•0 comments

Starting a Startup at 25, 35, or 45 Is Not the Same Decision

https://nauma.ai/blog/p/starting-a-startup-at-25-35-or-45/
2•alx_sukhanov•26m ago•0 comments

Asking Grok to delete fake nudes may force victims to sue in Musk's chosen court

https://arstechnica.com/tech-policy/2026/01/asking-grok-to-delete-fake-nudes-may-force-victims-to...
3•uprooted•27m ago•0 comments

ChatGPT Is a Dead-End

https://garymarcus.substack.com/p/breaking-sir-demis-hassabis-becomes
1•mandeepj•29m ago•0 comments

U.S. Formally Withdraws from World Health Organization

https://www.nytimes.com/2026/01/22/us/politics/united-states-withdraws-world-health-organization....
38•reaperducer•34m ago•13 comments

Never Pay for Online Dating

https://web.archive.org/web/20100821041938/http://blog.okcupid.com/index.php/why-you-should-never...
3•sogen•36m ago•2 comments

America's next big critical minerals source could be coal mine pollution

https://theconversation.com/americas-next-big-critical-minerals-source-could-be-coal-mine-polluti...
1•PaulHoule•39m ago•0 comments

Exposed: Investigative journalism on radiation exposure during air travel

https://www.scrippsnews.com/scripps-news-investigates/exposed
1•DrZootron•39m ago•0 comments

What if the reason we can't find Dark Matter is simply because it doesn't exist?

https://zenodo.org/records/17911993
3•emsti•42m ago•1 comments

Purchase by Bending Spoons (Harvest App)

https://old.reddit.com/r/HarvestApp/comments/1q25xpy/purchase_by_bending_spoons/
1•mikhael•42m ago•0 comments

Trump threatens pollsters after New York Times survey shows sagging disapproval

https://www.politico.com/news/2026/01/22/trump-poll-nyt-approval-00740786
8•SilverElfin•43m ago•2 comments

Clj-pack – Package Clojure apps into self-contained binaries without GraalVM

https://github.com/avelino/clj-pack
1•avelino•44m ago•1 comments

Not all Chess960 positions are equally complex

https://arxiv.org/abs/2512.14319
1•MaysonL•45m ago•0 comments

Signal's Censorship Circumvention is susceptible to AiTM attacks

https://www.reversemode.com/2026/01/signals-censorship-circumvention-is.html
2•cyb0rg0•45m ago•1 comments

Bugs Apple Loves

https://www.bugsappleloves.com
158•nhod•48m ago•43 comments

Shamash is a powerful JVM architecture enforcement tool for teams

https://github.com/aalsanie/shamash
1•aalsanie•48m ago•1 comments

How Space Manufacturing Could Be Revolutionary – Scott Manley

https://www.youtube.com/watch?v=U1sH2a57eXM
1•connorboyle•50m ago•0 comments