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.

The 500k-ton typo: Why data center copper math doesn't add up

https://investinglive.com/news/the-500000-ton-typo-why-data-center-copper-math-doesnt-add-up-2026...
1•thebeardisred•18s ago•0 comments

Utah Full Steam Ahead on Nuclear Industry While Wyoming Avoids

https://cowboystatedaily.com/2026/01/13/experts-clash-over-reprocessing-nuclear-waste-claims-as-w...
1•Bender•24s ago•0 comments

Pi – Multimodal Planetary Defense

https://arxiv.org/abs/2110.07559
1•dgroshev•48s ago•0 comments

Hijacking Bluetooth Accessories Using Google Fast Pair

https://whisperpair.eu/
1•ledoge•50s ago•0 comments

Volvo tells us why having Gemini in your next car is a good thing

https://arstechnica.com/cars/2026/01/exclusive-volvo-tells-us-why-having-gemini-in-your-next-car-...
1•Bender•2m ago•1 comments

Baseline Testing

https://keploy.io/blog/community/what-is-baseline-testing
1•sophielane•3m ago•1 comments

Ask HN: Hacker News users called me clanker twice, I am human though

1•Imustaskforhelp•5m ago•3 comments

You Are Claude Code, Anthropic's Official CLI

https://twitter.com/fullstacktard/status/2011689957413576979
1•fullstacktard•8m ago•0 comments

Zero-Copy SIMD Parsing for Unaligned Reads in Binary Protocols

https://github.com/Lunyn-HFT/lunary
1•birdculture•8m ago•0 comments

A Taxonomy of AI Narrative Evidence Failure in Enterprise Contexts

https://www.aivojournal.org/a-taxonomy-of-ai-narrative-evidence-failure-in-enterprise-contexts/
1•businessmate•9m ago•1 comments

How a billionaire encouraged Trump to acquire Greenland

https://www.theguardian.com/us-news/2026/jan/15/ronald-lauder-billionaire-donor-donald-trump-ukra...
1•mdhb•9m ago•0 comments

Show HN: Okscale – Create color scales using OKLCH curves

https://okscale.ely.al/
1•elyalon•10m ago•0 comments

Design Apps in Seconds – Appiliy

https://appiliy.com
1•MichaelFKnight•11m ago•1 comments

How reliable are published vector search benchmark results?

https://blog.ydb.tech/are-published-ann-benchmarks-dbms-results-trustworthy-f2573eca4e07
2•blinkov•12m ago•0 comments

TSMC smashes forecasts with record profit, flags more US factories

https://www.reuters.com/world/china/tsmc-likely-post-fourth-quarter-profit-leap-driven-by-ai-boom...
2•aurareturn•13m ago•0 comments

Germany's Merz Admits Nuclear Exit Was Strategic Mistake

https://clashreport.com/world/articles/germanys-merz-admits-nuclear-exit-was-strategic-mistake-fz...
6•neamar•14m ago•1 comments

Wine 11.0 Released

https://gitlab.winehq.org/wine/wine/-/releases/wine-11.0
4•SanjayMehta•16m ago•1 comments

Open Wearables – Unified Health Data Platform

https://www.openwearables.io/
1•nickypicky•17m ago•0 comments

Secret ICE Programs Revealed

https://www.kenklippenstein.com/p/21-secret-ice-programs-revealed
6•jbegley•18m ago•2 comments

Genetic Therapy Aims to Bring Hearing to Those Born Deaf

https://hackaday.com/2026/01/14/genetic-therapy-aims-to-bring-hearing-to-those-born-deaf/
1•wtcactus•19m ago•0 comments

SpaceX will attempt to reach Mars by the end of 2026

https://www.msn.com/en-ie/money/other/elon-musk-surprises-everyone-spacex-will-attempt-to-reach-m...
1•wtcactus•21m ago•1 comments

Experts want insight in takeover of Digid (Dutch National ID service) by US firm

https://www.dutchnews.nl/2026/01/experts-want-insight-into-takeover-of-digid-platform-by-us-firm/
1•teekert•21m ago•1 comments

Show HN: CallTuv - A browser dialer with 80+ hours of calls in its first month

https://calltuv.com/en-US
1•yosid•22m ago•1 comments

Cold Weather and the Hard Limits of Micromobility Batteries

https://micromobility.io/news/cold-weather-and-the-hard-limits-of-micromobility-batteries
1•prabinjoel•22m ago•0 comments

Show HN: I modeled personal success as a control system with Bayesian priors

https://mondonno.github.io/successus/sample-h1.html
2•mondonno•24m ago•0 comments

It's hard to nake money from software alone

https://twitter.com/i/status/2011542744989077863
1•rohmanhakim•26m ago•0 comments

Unix v4 Source Code Commentary

https://github.com/unix-v4-commentary/unix-v4-source-commentary
2•todsacerdoti•26m ago•1 comments

ARPA-H launches program for the 46% of U.S. counties don't have a cardiologist

https://www.statnews.com/2026/01/13/arpa-h-advancing-clinical-agentic-ai-use-heart-disease/
2•brandonb•33m ago•0 comments

Ernesto Gurgle Velante

https://www.facebook.com/ernesto.gurgelvalente.3
1•PROPILS•33m ago•0 comments

SydBox – pank rock Rust scalpel alternative to Bubblewrap

https://gitlab.exherbo.org/sydbox/sydbox
2•dlahoda•34m ago•0 comments