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.

Chinese car company patents voice-activated 'in-vehicle toilet'

https://www.upi.com/Odd_News/2026/04/17/Seres-patent-in-car-toilet/8281776442128/
1•speckx•1m ago•0 comments

See and hear galaxies evolve from the dawn of the universe

https://ras.ac.uk/news-and-press/research-highlights/see-and-hear-galaxies-evolve-dawn-universe
1•gmays•8m ago•0 comments

Tario-2: A Whole-Transcriptome Foundation Model from H&E Alone

https://www.noetik.blog/p/tario-2-a-whole-transcriptome-foundation
2•abhishaike•8m ago•0 comments

Online PDF Password Protection Tool in 2025: Securing Your Digital Documents

https://amorpdf.com/online-pdf-password-protection-tool-in-2025/
1•microespana•10m ago•0 comments

"Show Me the Incentive, and I'll Show You the Outcome."

https://www.fixtheincentives.org/
3•paulpauper•10m ago•0 comments

In Defense of Utopia

https://ary31415.substack.com/p/in-defense-of-utopia
1•paulpauper•10m ago•0 comments

The Cashier Standard – Age Verification Without Surveillance

https://claude.ai/public/artifacts/7fe74381-a683-4f49-9c2b-171cba4b3e59
1•anonym29•10m ago•0 comments

If You Want a Better World, Act Like You Live in It

https://www.theatlantic.com/books/2026/04/henry-david-thoreau-great-american-dissident/686823/
1•paulpauper•11m ago•0 comments

I Went Back to the Dumb Phone

https://substack.magazinenongrata.com/p/back-to-the-dumb-phone
1•fictionreader•11m ago•0 comments

Shakespeare's 'missing' London house mapped with new discovery

https://www.kcl.ac.uk/news/shakespeares-missing-london-house-mapped-with-new-discovery-1
1•geox•12m ago•0 comments

Show HN: Wrong Side of Zero – Google Flow Music [video]

https://www.youtube.com/watch?v=Syhgdct_VC4
1•modinfo•12m ago•0 comments

How to Read a QR Code from a Screenshot on Your Laptop Without Using Your Phone

https://www.codeava.com/blog/read-qr-code-screenshot-laptop-desktop
1•codeava•14m ago•0 comments

What Is a Hydrogen Gas Turbine?

https://www.greengasturbines.com/blog/what-is-a-hydrogen-gas-turbine-definition-types-how-they-work
1•codeava•15m ago•0 comments

Verijit – Up to 100x faster Verilog simulation [video]

https://www.youtube.com/watch?v=PXgUsEjvAOY
2•can_lehmann•15m ago•0 comments

'Everything is coming down': ChatGPT ads are getting cheaper

https://digiday.com/marketing/everything-is-coming-down-chatgpt-ads-are-getting-cheaper/
2•speckx•16m ago•0 comments

Court unanimously sides with oil and gas companies in suit over damage to coast

https://www.scotusblog.com/2026/04/court-unanimously-sides-with-oil-and-gas-companies-in-suit-ove...
1•Jimmc414•22m ago•0 comments

Xtrace-skill: xtrace – Command-line CPU Profiling for macOS as a skill

https://github.com/Kr1sso/xtrace-skill
1•mpweiher•24m ago•0 comments

Paper, Love and Software

https://github.com/mistachkin/docs/blob/trunk/paper_love_and_software.md
1•BaconDaddyX•25m ago•1 comments

You paid for it, you should be comfortable in it

https://idiallo.com/blog/you-paid-for-it-you-should-be-comfortable-in-it
1•speckx•28m ago•0 comments

Philz Coffee retreats from controversial plan

https://www.sfchronicle.com/food/restaurants/article/philz-reverses-pride-flag-ban-22212158.php
2•_doctor_love•29m ago•2 comments

A Close Look at a Spinlock

https://blog.regehr.org/archives/2173
2•jruohonen•32m ago•0 comments

Ask HN: In the AI world what does "great" look like?

2•armcat•32m ago•0 comments

How Netflix Built the Operations Layer Behind Live at Scale

https://netflixtechblog.com/the-human-infrastructure-how-netflix-built-the-operations-layer-behin...
4•tcp_handshaker•32m ago•0 comments

China surfaces details of spacecraft for Moon landing by 2030

https://jatan.space/moon-monday-issue-267/
2•tcp_handshaker•33m ago•0 comments

Doubling the voltage: What 800 V architecture changes in EVs

https://arstechnica.com/cars/2026/03/doubling-the-voltage-what-800-v-architecture-really-changes-...
1•PaulHoule•33m ago•0 comments

Exts to Odrzywołek's EML Uni Operator: Hybrid Best Routing, Phantom Attractors

https://www.monogate.dev/
1•zonked45•34m ago•0 comments

Why AI Is Not Getting You Out of a Job

https://yeikoff.xyz/blog/17-04-2026-not-out-of-a-job/
2•iglesiastj•35m ago•0 comments

OpenAI to spend more than $20B on Cerebras chips, receive stake

https://www.reuters.com/technology/openai-spend-more-than-20-billion-cerebras-chips-receive-equit...
2•gaurangt•36m ago•2 comments

Local Model Router: Ollama/OpenAI-compat bridges for local LLMs via llama.cpp

1•g023•36m ago•0 comments

The King James Bible (2016)

https://www.thehindubusinessline.com/blink/talk/tongue-in-check/article8476057.ece
1•bookofjoe•37m ago•0 comments