frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Fair Screen – Detect Cluely/Interview Coder Kind of Interview Cheating Tools

https://fairscreen.co/
1•anantha2024•1m ago•1 comments

Show HN: Dream Decoder AI – Jungian dream analysis with 3D visualization

1•brandonmillsai•2m ago•0 comments

NTSB report: Decryption of images from the Titan submersible camera [pdf]

https://data.ntsb.gov/Docket/Document/docBLOB?ID=18741602&FileExtension=pdf&FileName=Underwater%2...
1•bmurray7jhu•7m ago•0 comments

Rare earths: Federal backing and tech advances U.S. catch up to China

https://www.npr.org/2025/11/21/nx-s1-5601696/rare-earths-china-trump-pentagon
1•andsoitis•9m ago•0 comments

The corrupt culture of sports gambling

https://www.nationalreview.com/magazine/2026/01/the-corrupt-culture-of-sports-gambling/
1•hhs•11m ago•0 comments

What rituals from the past teach us about panic and anxiety

https://psyche.co/ideas/what-rituals-from-the-past-teach-us-about-panic-and-anxiety
1•herbertl•13m ago•0 comments

What If Aliens Don't Actually Do Science?

https://lithub.com/what-if-aliens-dont-actually-do-science/
2•bookofjoe•17m ago•0 comments

The pedant's progress through history

https://www.spectator.co.uk/article/the-pedants-progress-through-history/
1•hhs•18m ago•0 comments

Mechanical power generation using Earth's ambient radiation

https://news.ycombinator.com/submit
1•worik•18m ago•0 comments

Letting coding agents access runtime output (my current approach)

https://foundinglean.substack.com/p/the-best-improvement-ive-made-to
1•dsmurrell•19m ago•0 comments

Automatic Alt Text Generation

https://github.com/alexander-turner/alt-text-llm
1•Turn_Trout•19m ago•0 comments

Preserving Time for Focused Work

https://rfd.shared.oxide.computer/rfd/0083
1•lwhsiao•20m ago•0 comments

PasLLM: An Object Pascal inference engine for LLM models

https://github.com/BeRo1985/pasllm
3•nor-and-or-not•25m ago•1 comments

X begins rolling out the 'About this account' feature to users' profiles

https://techcrunch.com/2025/11/21/x-begins-rolling-out-the-about-this-account-feature-to-users-pr...
3•paulatreides•28m ago•0 comments

Nuclear Energy

https://ourworldindata.org/nuclear-energy
1•andsoitis•30m ago•1 comments

AWS Security Incident Response now provides agentic AI-powered investigation

https://aws.amazon.com/blogs/security/accelerate-investigations-with-aws-security-incident-respon...
1•dan_l2•34m ago•0 comments

The Fatal Trap UBI Boosters Keep Falling Into

https://thereader.mitpress.mit.edu/the-fatal-trap-ubi-boosters-keep-falling-into/
2•pseudolus•42m ago•1 comments

https://kaausia45-jpg.itch.io/eidos-ai-powered-game-prototyping-environment

https://kaaussia45-jpg.itch.io/eidos-ai-powered-game-prototyping-environment
2•symbol_reasoner•42m ago•1 comments

Seekdb, an open source AI native search database

https://github.com/oceanbase/seekdb
2•cloudsql•46m ago•0 comments

G20 summit adopts declaration despite US boycott, opposition

https://www.reuters.com/sustainability/climate-energy/g20-leaders-meet-south-africa-seeking-agree...
1•geox•48m ago•1 comments

The silver bullet fallacy

https://timharford.com/2025/11/the-silver-bullet-fallacy/
1•hhs•53m ago•0 comments

Hands on with North Korea's illegal phones

https://www.youtube.com/watch?v=3olqrQtjPfc
1•arittr•54m ago•0 comments

The dream to fill the American desert with exotic animals [video]

https://www.youtube.com/watch?v=cZEG22uvW9Q
1•mapmeld•56m ago•0 comments

The Rise of the 'Just in Case' MRI

https://www.nytimes.com/2025/11/22/business/dealbook/full-body-mri.html
2•bonsai_spool•1h ago•2 comments

Show HN: Spectrum – A daily sorting puzzle game

https://spectrumsort.com/
1•sirmeeps•1h ago•0 comments

Japan weighs fines to strengthen personal data protection

https://www.japantimes.co.jp/news/2025/10/30/japan/japan-personal-information-fine/
2•PaulHoule•1h ago•0 comments

Show HN: A simple AI infographic generator, simply turn text prompt to visual

https://infografa.com/
1•pikpok12•1h ago•0 comments

WhatsApp messages revealed ex-Reform politician's pro-Russian bribes

https://www.bbc.com/news/articles/cn8vnv3dk0vo
2•breve•1h ago•0 comments

HP and Dell disable HEVC hardware decoding on select laptops as royalties rise

https://videocardz.com/newz/hp-and-dell-disable-hevc-hardware-decoding-on-select-laptops-as-royal...
2•MaximilianEmel•1h ago•1 comments

College 'sticker prices' have risen dramatically

https://text.npr.org/nx-s1-5600854
2•mooreds•1h ago•0 comments