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.

Show HN: AI block chain running on GPT2

https://github.com/raphaelwkago69-create/GLYPH
1•w4mwati•3m ago•0 comments

Show HN: Validating an OG-image API before I build the back end

https://autogranthunter.com/og/
1•averystone_hs•7m ago•0 comments

ViewKit – An interactive tool to view dataset files

https://viewkit.app/
1•ayu05•9m ago•1 comments

Saying the Obvious Thing

https://www.seangoedecke.com/saying-the-obvious-thing/
1•walterbell•12m ago•0 comments

Venice mayor proposes dynamic pricing for day-trippers with 50-euro ceiling

https://abcnews.com/International/wireStory/venice-mayor-proposes-dynamic-pricing-day-trippers-50...
1•simonebrunozzi•14m ago•0 comments

An Ordinary Mind on an Ordinary Day

https://www.laphamsquarterly.org/roundtable/ordinary-mind-ordinary-day
1•vinhnx•15m ago•0 comments

This week's 13 most interesting new big tech patents

https://patentlyze.substack.com/p/ads-in-your-ai-chats-pauses-glasses
2•Dfol•16m ago•1 comments

Embedding Information in Disorder

https://thoughts.hmmz.org/2026-07-05.html
1•vinhnx•19m ago•0 comments

Sometimes it is the network: a war story

https://utcc.utoronto.ca/~cks/space/blog/sysadmin/SometimesItIsTheNetwork
2•srijan4•19m ago•0 comments

EdgeBench: Unveiling scaling laws of (AI) learning from real-world environments

https://github.com/ByteDance-Seed/EdgeBench
1•rguiscard•30m ago•0 comments

Pactiamo – Turn a one-line brief into a signable proposal and contract

https://www.pactiamo.com/
1•kkmigu•33m ago•0 comments

Fossils were the perfect home for ancient baby bees

https://www.fieldmuseum.org/about/press/these-fossils-were-the-perfect-home-for-ancient-baby-bees
1•t-3•34m ago•0 comments

CachePilot – Drop-in AI API caching proxy (pay 20% of savings)

https://cachepilot.serveousercontent.com
1•koaw_moi•34m ago•0 comments

Chinese LLMs Doubao, Qwen to shut down personalized AI agents on July 15

https://www.globaltimes.cn/page/202607/1365159.shtml
1•testrun•38m ago•0 comments

Bomb the Arctic, dam the Mediterranean and build a second moon

https://www.theguardian.com/environment/2026/jul/06/five-outlandish-plans-to-remodel-earth-climate
4•tosh•41m ago•1 comments

Show HN: See an AI shopping agent choose which products to recommend

https://catalogdoorman.netlify.app
4•nightowlbuilds•51m ago•0 comments

Show HN: Clone. Swap your products in any viral reel

https://quinn.live/clone/
2•mohitkinra•52m ago•0 comments

China's ByteDance discovers new scaling law that could sustain AI boom

https://www.scmp.com/tech/big-tech/article/3359373/chinas-bytedance-discovers-new-scaling-law-cou...
2•outrunner•57m ago•0 comments

The Largest Vocabulary in Hip Hop

https://pudding.cool/projects/vocabulary/index.html
1•ycombinete•58m ago•0 comments

ByteDance and Alibaba to disable humanlike AI custom agents as new rules loom

https://www.scmp.com/tech/big-tech/article/3359482/bytedance-and-alibaba-disable-humanlike-ai-cus...
7•merlioncity•59m ago•0 comments

Big Tech Has Suddenly Flipped on the AI Jobs Wipeout Scenario

https://www.wsj.com/tech/ai/ai-workers-tech-ceos-job-losses-afc71e15
4•LostMyLogin•1h ago•0 comments

Why QR Code Phishing Is the New 2026 Security Blind Spot

https://www.acronis.com/en/blog/posts/qr-code-phishing-evasive-threats-2026/
2•thunderbong•1h ago•0 comments

PrivadoFinance

https://www.privadofinance.com
1•Greythium•1h ago•0 comments

The Staged Reality of "Lunch Atop a Skyscraper": What They Hid for 70 Years [video]

https://www.youtube.com/watch?v=5VdfCDnKUBQ
1•javatuts•1h ago•0 comments

Android is almost dead – OSnews

https://www.osnews.com/story/145415/android-is-almost-dead/
6•abdelhousni•1h ago•1 comments

The Incomplete History of Computer Science

https://example.org/
2•d1ss0nanz•1h ago•1 comments

Elon Musk's AI data center sparks fight over who can enforce clean air laws

https://www.latimes.com/business/story/2026-06-30/elon-musks-massive-ai-data-center-sparks-fight-...
4•1vuio0pswjnm7•1h ago•0 comments

Short story accused of being AI-written wins overall Commonwealth prize

https://www.theguardian.com/books/2026/jul/01/judges-claims-ai-use-commonwealth-short-story-prize...
1•breve•1h ago•0 comments

Voters Are Turning on AI

https://www.economist.com/podcasts/2026/06/29/voters-are-turning-on-ai
4•1vuio0pswjnm7•1h ago•1 comments

What Is Linux Swap, and Should You Enable It?

https://blog.lyc8503.net/en/post/21-swap-setup/
3•uneven9434•1h ago•0 comments