frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

U.S. Ambassador to EU: Stop Fining Big Tech

https://www.cnbc.com/2026/03/27/big-tech-eu-fines-ai-data-centers-us-ambassador-puzder.html
1•vrganj•47s ago•0 comments

The statistical basis of Fermi estimates(2021)

https://hapax.github.io/physics/hacks/mathematics/statistics/fermi-log-normal/
1•o4c•2m ago•0 comments

Translating non-trivial codebases with Claude (C++ to Java, Haskell to Clojure)

https://blog.danieljanus.pl/2026/03/26/claude-nlp/
1•nathell•2m ago•0 comments

Hacking Physics from the Back of a Napkin(2020)

https://hapax.github.io/physics/teaching/hacks/napkin-hacks/#sec-3
1•o4c•2m ago•0 comments

Forensic beats Mem0 with 90.1% on LOCOMO

https://forensicmemory.com/proof.html
1•strobo•7m ago•0 comments

Treason in the Futures Markets

https://paulkrugman.substack.com/p/treason-in-the-futures-markets
2•Gasp0de•7m ago•0 comments

Machine Learning for Android Engineers

https://7422889060748.gumroad.com/l/ml-android-engineers
1•vsaytech•8m ago•1 comments

Spanish woman dies by euthanasia after long legal battle with father

https://www.bbc.com/news/articles/c7vqdd23y0vo
1•stared•14m ago•0 comments

Voxtral from Mistral

https://mistral.ai/news/voxtral-tts
1•wiradikusuma•16m ago•0 comments

Palestinian toddler returns injured after IDF detention in Gaza

https://news.sky.com/story/gaza-toddler-released-from-israeli-custody-with-suspected-torture-woun...
5•lr0•20m ago•2 comments

Empiricism and Rationalism in Software Testing

http://joshvoigts.com/articles/empiricism-and-rationalism-in-software-testing/
1•redman25•21m ago•0 comments

ChatGPT Founder's Experience (Book)

https://startupik.com/the-altman-blueprint/
1•startupik•26m ago•0 comments

Alex Karp says only trade workers and neurodivergents will survive in the AI era

https://fortune.com/2026/03/24/palantir-ceo-alex-karp-two-people-successful-in-ai-era-vocational-...
8•anticensor•30m ago•2 comments

Scientists uncovered the nutrients bees were missing

https://www.sciencedaily.com/releases/2026/03/260327000518.htm
2•Modified3019•30m ago•0 comments

Fast, accurate and comprehensive userland text measurement algorithm

https://twitter.com/_chenglou/status/2037713766205608234
2•lumannnn•32m ago•0 comments

Big-Endian Testing with QEMU

https://www.hanshq.net/big-endian-qemu.html
2•zmodem•34m ago•0 comments

Tailscale'd into Homelabbing

https://rugu.dev/en/blog/homelabbing_01/
1•birdculture•35m ago•0 comments

Doom in CSS

https://nielsleenheer.com/articles/2026/css-is-doomed-rendering-doom-in-3d-with-css/
2•runxiyu•36m ago•0 comments

Show HN: Newspaper-style HN reader (PWA) with articles and comments side by side

https://hackernewstimes.com
1•terryds•37m ago•0 comments

Show HN: I optimized a Java log scanner from 872ms to 78ms (Zero GC)

https://github.com/Yosefnago/java-deep-dive
1•yosinago•38m ago•0 comments

JWST solves decades-long mystery about why Saturn appears to change its spin

https://phys.org/news/2026-03-jwst-decades-mystery-saturn.html
1•bookmtn•38m ago•0 comments

Quiet Restructuring of Work: AI, Job Compression, and the Risk

https://medium.com/@andreaswalterkoellen/the-quiet-restructuring-of-work-ai-job-compression-and-t...
2•taubek•39m ago•0 comments

InpharmD (YC W21) Is Hiring – Senior Ruby on Rails Engineer (Rails 8, Ruby 3)

https://inpharmd.com/jobs/senior-ruby-on-rails-engineer
1•tulasichintha•41m ago•0 comments

Brief life of Harvard CIA agent who helped install the shah of Iran

https://www.harvardmagazine.com/2010/12/kermit-roosevelt
2•geox•45m ago•0 comments

Ask HN: What are you using for DAO gov data after Tally (tally.xyz) shuts down?

1•Sem_pre•46m ago•0 comments

European Commission investigating breach after Amazon cloud account hack

https://www.bleepingcomputer.com/news/security/european-commission-investigating-breach-after-ama...
3•dryarzeg•48m ago•1 comments

Preparation – The underrated potential for CoMo workshops

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

Cocoa-Way – Native macOS Wayland compositor for running Linux apps seamlessly

https://github.com/J-x-Z/cocoa-way
4•OJFord•51m ago•0 comments

Building Tanks While the Ukrainians Master Drones

https://www.theatlantic.com/national-security/2026/03/who-needs-tanks-age-drones/686540/
1•doener•53m ago•1 comments

Electricity Maps

https://app.electricitymaps.com/map/live/fifteen_minutes
2•sph•56m ago•0 comments