frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

High Performance Spaceflight Computing (HPSC)

https://www.nasa.gov/game-changing-development-projects/high-performance-spaceflight-computing-hpsc/
1•rbanffy•2m ago•0 comments

Browser Run: now running on Cloudflare Containers, it's faster and more scalable

https://blog.cloudflare.com/browser-run-containers/
1•tjek•3m ago•0 comments

Research on mildew contamination affecting the sound quality of analog tapes

https://www.nature.com/articles/s40494-026-02592-7
1•crousto•3m ago•0 comments

SicariusGuard – Solana token safety oracle for AI agents (MCP server)

https://github.com/Chronolapse411/sicarius-guard
1•Chronolapse411•6m ago•0 comments

Source Hydrated Infrastructure Models

https://medium.com/ndi-sg/source-hydrated-infrastructure-models-shim-3c145c7c91c0
1•alienchow•7m ago•0 comments

Brazil's Atlantic forest records lowest deforestation in 40 years

https://www.theguardian.com/world/2026/may/14/brazil-atlantic-forest-deforestation-record
1•robtherobber•7m ago•0 comments

The Siri for Families Apple Will Never Build

https://taoofmac.com/space/blog/2026/05/14/1220
1•rcarmo•11m ago•0 comments

A Dark-Money Campaign Is Paying Influencers to Frame Chinese AI as a Threat

https://www.wired.com/story/super-pac-backed-by-openai-and-palantir-is-paying-tiktok-influencers-...
1•xbmcuser•11m ago•0 comments

N8n-MCP – MCP server for generating and debugging n8n workflows

https://github.com/AutomateLab-tech/n8n-mcp
1•automatelabs•12m ago•0 comments

Bacterial defense system builds DNA in unexpected new way to stop viruses

https://phys.org/news/2026-04-bacterial-defense-dna-unexpected-viruses.html
1•PaulHoule•13m ago•0 comments

Neanderthals may have drilled out a cavity 59,000 years ago

https://www.npr.org/2026/05/13/nx-s1-5815047/neanderthal-tooth-dentistry-cavity-drill
1•shinryudbz•14m ago•0 comments

YouTube to Transcript, timestamped video transcriptions

https://pelicantools.app/tools/youtube-to-transcript
1•cristyg0101•14m ago•0 comments

AI won't always make you faster

https://krellixlabs.com/en/blog/ai-wont-always-make-you-faster
2•radu_me•17m ago•0 comments

What Matters in Practical Learned Image Compression

https://apple.github.io/ml-pico/
1•bennett_dev•17m ago•0 comments

Show HN: Fuse-stripped-notebooks uses FUSE to make .ipynb better for grep

https://github.com/bm371613/fuse-stripped-notebooks
1•bm371613•18m ago•0 comments

Show HN: SuperCSV – a typed, self-describing data format

https://supercsv.com/
1•mmm1•18m ago•0 comments

The New Raycast

https://www.raycast.com/blog/the-new-raycast
3•swah•20m ago•0 comments

The jobs apocalypse: a (very) short history

https://www.economist.com/finance-and-economics/2026/05/14/the-jobs-apocalypse-a-very-short-history
1•andsoitis•20m ago•0 comments

The European Union backs Italy's right to make Meta pay for news

https://www.niemanlab.org/2026/05/the-eu-backs-italys-right-to-make-meta-pay-for-news/
4•giuliomagnifico•20m ago•0 comments

NTT Docomo Ends I-Mode Mobile Service That Pioneered the Emoji

https://www.nippon.com/en/japan-topics/g02591/
1•eternalreturn•21m ago•0 comments

The Maze of Doom Model of Software Engineering

https://cultofsoftwareengineering.substack.com/p/the-maze-of-doom-model-of-software
1•daddevmusings•22m ago•0 comments

Digital Euro: Data protectionists demand digital cash, not surveillance

https://www.heise.de/en/news/Digital-Euro-Data-protectionists-demand-digital-cash-not-surveillanc...
2•robtherobber•24m ago•0 comments

Signing off in a world of what's next

https://om.co/2026/05/12/signing-off-in-a-world-of-whats-next/
1•herbertl•27m ago•0 comments

Thucydides Trap

https://en.wikipedia.org/wiki/Thucydides_Trap
2•theletterf•28m ago•0 comments

Hello Universe: NASA's Next-Gen RISC-V Space Processor Undergoes Testing

https://www.jpl.nasa.gov/news/hello-universe-nasas-next-gen-space-processor-undergoes-testing/
1•rbanffy•28m ago•0 comments

The Readable Mind: LLMs as Psychological Infrastructure (2026)

https://zenodo.org/records/20179361
2•dhedegreen•30m ago•0 comments

Working Hard

https://joy.ente.com/working-hard/
4•darthShadow•31m ago•1 comments

Ten Things Every Trial Lawyer Could Learn from Vincent La Guardia Gambini [pdf]

https://s3.amazonaws.com/law-media/uploads/198/35361/original/Anderson_TenThings_SU2016.pdf
1•kmstout•31m ago•0 comments

Ryan Cohen hits back at eBay, says his takeover proposal should not be dismissed

https://www.reuters.com/world/ryan-cohen-says-ebay-directors-should-not-dismiss-his-proposal-with...
5•AdmiralAsshat•32m ago•1 comments

Myths about /dev/urandom

https://www.2uo.de/myths-about-urandom/
4•signa11•33m ago•0 comments