frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Wolves, Sheep, and Gypsies

https://world.hey.com/dhh/wolves-sheep-and-gypsies-ba44af6a
1•fratellobigio•35s ago•0 comments

Faceblind – Client side only video face blurring

https://kmcheung12.github.io/faceblind/
1•a_c•37s ago•1 comments

Show HN: StarSkew, a privacy-light Amazon review analyzer (starskew.com)

https://starskew.com/
1•KETpXDDzR•2m ago•0 comments

May 2, 1960: Dick Clark survives the Payola scandal

https://www.history.com/this-day-in-history/may-2/dick-clark-survives-the-payola-scandal
1•petethomas•3m ago•0 comments

Show HN: I built a chat client that uses embeddings to cluster messages by topic

https://llmtrail.com/demo
1•doantam•3m ago•0 comments

Don't Make the Same Mistake as Me [video]

https://www.youtube.com/watch?v=0F5zG1RQ4Ig
1•fenced_load•4m ago•0 comments

Open Source AI Harness Profiler – discover where tf your tokens are going

https://github.com/TryRekon/Rekon
1•TreDub•5m ago•1 comments

Show HN: RockCode an unofficial vibe-coded macOS client for Grok CLI

https://github.com/natanavra/rockcode
1•natanavra•5m ago•0 comments

AMD's Jerry Sanders, Silicon Valley's tough guy (2001)

https://www.sfgate.com/bayarea/article/PROFILE-Jerry-Sanders-Silicon-Valley-s-tough-2873326.php
1•stmw•6m ago•0 comments

Where should you move for higher pay?

https://economist.com/graphic-detail/2026/07/21/where-should-you-move-for-higher-pay
1•andsoitis•8m ago•0 comments

So Let It Be

https://soletitbe.dev/
1•stoke7143•8m ago•0 comments

Ask HN: Post active but comments got flagged

1•spirosoik•9m ago•1 comments

"Drawing" the Mona Lisa with GPT-5.6, Claude, Gemini, and Grok

https://www.tryai.dev/blog/ai-drawing-arena-colored-pencils-claude-gpt-grok
2•hershyb_•9m ago•1 comments

Where do the biggest diamonds come from? Geology now has answers

https://theconversation.com/where-do-the-worlds-biggest-diamonds-come-from-geology-now-has-answer...
2•bryanrasmussen•10m ago•0 comments

Short positions in SpaceX reach 32% of float

https://www.cnbc.com/2026/07/21/bets-against-spacex-grow-to-32percent-of-float-as-elon-musk-warns...
1•darth_avocado•11m ago•0 comments

Oracle critical July patch update advisory contains 1,449 new security patches

https://www.oracle.com/security-alerts/cpujul2026.html
1•speckx•13m ago•0 comments

A digestion of the Jacobian conjecture counterexample

https://terrytao.wordpress.com/2026/07/21/a-digestion-of-the-jacobian-conjecture-counterexample/
4•jeremyscanvic•13m ago•0 comments

Bring Your Own Harness

https://andreapivetta.com/posts/bring-your-own-harness.html
1•ziggy42•13m ago•0 comments

K0s – The Zero Friction Kubernetes

https://github.com/k0sproject/k0s
1•porjo•15m ago•0 comments

Show HN: RootBadger – a modern Usenet-style discussion platfor

https://rootbadger.com
1•yodabytz•16m ago•0 comments

Treasury Flags Concern over 'Potentially Abusive' Tax Trades

https://www.bloomberg.com/news/articles/2026-07-21/treasury-flags-concern-over-potentially-abusiv...
1•petethomas•16m ago•0 comments

Nvidia Vera Rubin Driving Performance per Watt, Lowest Token Cost for Partners

https://www.hpcwire.com/off-the-wire/nvidia-vera-rubin-driving-performance-per-watt-lowest-token-...
2•rbanffy•17m ago•0 comments

Show HN: Browser Tools SDK – an optimal browser harness for agents

https://libretto.sh/browser-tools
2•tanishqkanc•20m ago•0 comments

Show HN: Unified workspace where AI knows your business

https://zetadeck.com
1•not_wowinter13•20m ago•0 comments

iOS 27 code suggests Apple could restrict leased devices after missed payments

https://9to5mac.com/2026/07/21/ios-27-code-suggests-apple-could-restrict-leased-devices-after-mis...
1•cdrnsf•21m ago•0 comments

Ask HN: What is your onboarding/discovery process for a new client or project?

2•urnicus•22m ago•0 comments

Octen: We stayed quiet and built the fastest search on Earth

https://twitter.com/KZouAPT/status/2079569508549673409
2•devchandra•24m ago•3 comments

Show HN: Meltbox – where your agents send you briefs

https://meltbox.ai/
2•flysonic10•26m ago•0 comments

MovieSoon: What's coming to a theater near you

https://moviesoon.eu/
3•taubek•26m ago•0 comments

Cisco Antares: A New Family of Cheap, Open-Source, Compact Security AI Models

https://securityboulevard.com/2026/07/cisco-antares-a-new-family-of-open-source-inexpensive-compa...
6•CrankyBear•27m ago•0 comments