frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

CEO laid off 80% of staff 2 years ago. He would do it again

https://finance.yahoo.com/news/ceo-laid-off-nearly-80-185033733.html
1•websku•1m ago•0 comments

Show HN: Sidecar – AI Social Manager (Analyzes past hits to write new posts)

https://sidecar.bz/http:/localhost:45678/
1•ecotto123•1m ago•1 comments

Be Wary of Digital Deskilling

https://calnewport.com/be-wary-of-digital-deskilling/
1•calstad•2m ago•0 comments

Bento got acquired by Linktree and shut down – so we made an alternative

https://avely.me/
2•FlavioAd•3m ago•0 comments

A geopolitical dashboard to track live conflicts and compare news outlets

https://geopoliticsmonitor.com/
1•Zzadda•3m ago•0 comments

The AI Gazes at Its Navel

http://funcall.blogspot.com/2026/01/the-ai-gazes-at-its-naval.html
1•djha-skin•3m ago•0 comments

Paramount Threatens Board Fight over Warner Bros. Discovery

https://www.nytimes.com/2026/01/12/business/media/paramount-warner-bros-discovery-board-fight.html
1•Sparkle-san•4m ago•1 comments

If god said it he'll do it

https://proverbs31.org/read/devotions/full-post/2021/03/02/if-god-said-it-hell-do-it
1•marysminefnuf•4m ago•0 comments

The Worst Devices of CES 2026 [video]

https://www.youtube.com/watch?v=cxZgILm95BU
2•ColinWright•4m ago•0 comments

Increased file size limits and expanded inputs support in Gemini API

https://blog.google/innovation-and-ai/technology/developers-tools/gemini-api-new-file-limits/
1•meetpateltech•5m ago•0 comments

No Code Is Dead

https://thenewstack.io/no-code-is-dead/
1•ms7892•5m ago•1 comments

Show HN: Spec Driven Development Plugin for Claude Code

https://github.com/jsegov/shipspec-claude-code-plugin
1•segov•7m ago•0 comments

Earth's frozen regions are sending a clear warning about climate change

https://theconversation.com/earths-frozen-regions-are-sending-a-clear-warning-about-climate-chang...
1•PaulHoule•7m ago•0 comments

Google Gemini Partnership with Apple Will Go Beyond Siri Revamp

https://twitter.com/NewsFromGoogle/status/2010746083170046376
1•7777777phil•7m ago•1 comments

How alone do you feel?

https://chicagosignguy.com/blog/how-alone-do-you-feel.html
2•publicdebates•8m ago•0 comments

Why Reddit Is a Goldmine for Market Research

https://fluxzap.com/reddit-market-research-goldmine/
1•taimurkazmi•8m ago•0 comments

Major Streaming Services Try to Take Pirate Sites Down Globally via Indian Court

https://torrentfreak.com/disney-netflix-crunchyroll-try-to-take-pirate-sites-down-globally-throug...
2•gslin•9m ago•0 comments

Show HN: FlowDeck – Stop switching to Xcode just to hit Build

https://flowdeck.studio/
1•afterxleep•10m ago•0 comments

Show HN: Securing Docker Builds

https://github.com/avkcode/buildkit-nsjail-sandbox-blog
1•KyleVlaros•13m ago•0 comments

Show HN: AI in SolidWorks

https://www.trylad.com
2•WillNickols•16m ago•0 comments

Show HN: Woid – 3x Faster Runtime Polymorphism. C++23

https://github.com/akopich/woid
1•akopich•17m ago•0 comments

Chesspire: "Slay the Spire" but for Chess

https://lykrast.com/chesspire
1•gaws•19m ago•0 comments

What If ASI Leads to Stasis?

https://thinking.luhar.org/2026/01/what-if-asi-leads-to-stasis/
2•rluhar•19m ago•1 comments

Enterprise Integration Patterns: Process Manager

https://james-carr.org/posts/2026-01-05-advent-of-eip-day-10-process-manager/
1•carrja99•19m ago•0 comments

Pwning Claude Code in 8 Different Ways

https://flatt.tech/research/posts/pwning-claude-code-in-8-different-ways/
2•kschaul•19m ago•0 comments

Cursor vs. antigravity after a week of real use

1•okaris•21m ago•0 comments

Show HN: Senlo - self-hosted open-source email management system

https://github.com/IgorFilippov3/senlo
1•igorfilippov3•21m ago•0 comments

The truth behind the 2026 J.P. Morgan Healthcare Conference

https://www.owlposting.com/p/the-truth-behind-the-2026-jp-morgan
1•crescit_eundo•23m ago•0 comments

PutHouse – Earn income automatically with risk management built-in

https://puthouse.com
1•jansonlau•23m ago•0 comments

Apple Foundation Models will be based on Gemini

https://blog.google/company-news/inside-google/company-announcements/joint-statement-google-apple/
2•spott•25m ago•2 comments