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.

KLM flight attendant hospitalized after contact with hantavirus

https://nltimes.nl/2026/05/07/klm-flight-attendant-hospitalized-contact-hantavirus-cruise-ship-pa...
1•TechTechTech•30s ago•0 comments

Show HN: LaoTzu Writer Studio

1•homeonthemtn•42s ago•0 comments

Anthropic strikes SpaceX data center deal as it plows ahead on AI coding

https://www.reuters.com/business/retail-consumer/anthropic-unveils-dreaming-feature-help-its-ai-a...
1•andsoitis•2m ago•0 comments

Show HN: Bliss – a backlog quality scanner for AI-powered teams

https://trybliss.io/
1•nitzano•2m ago•0 comments

Package Darwinism, the Furture of Open Source, Adapt or Get Archived

https://twitter.com/pelaseyed/status/2052345682984407067
1•homanp•2m ago•0 comments

Anthropic Gets in Bed with SpaceX

https://www.wired.com/story/anthropic-spacex-compute-deal-colossus/
1•andsoitis•3m ago•0 comments

Coding Agent Harness Comparison 2026: Claude Code, Codex, Amp

https://techstackups.com/comparisons/coding-agent-harness-comparison-2026/
1•ritzaco•3m ago•0 comments

Anthropic, SpaceX announce compute deal that includes space development

https://www.cnbc.com/2026/05/06/anthropic-spacex-data-center-capacity.html
1•andsoitis•3m ago•0 comments

Chasing AI Memory SOTA: Beating the Benchmark, Missing the Point

https://xmemory.ai/chasing-sota-in-ai-memory/
1•alex_petrov•6m ago•1 comments

Show HN: Review code changes in a terminal and share with your coding agent

https://github.com/alexfedosov/diffnotes
1•alex_x•8m ago•0 comments

I forget most of what I read, so I built an AI to quiz me on it

https://refresherai.com/
1•go-native•8m ago•1 comments

Bariatric surgery procedures fall below 200k, first time since 2020

https://medicalxpress.com/news/2026-05-bariatric-surgery-procedures-fall.html
2•PaulHoule•13m ago•0 comments

Red, White, and Psychedelic

https://hollisrobbinsanecdotal.substack.com/p/red-white-and-psychedelic
1•HR01•14m ago•0 comments

Mizar: The first usable proof assistant for mathematics

https://lawrencecpaulson.github.io//2026/05/07/Mizar.html
1•chmaynard•17m ago•0 comments

Denmark's state-run postal service will no longer deliver letters (2025)

https://apnews.com/article/postnord-denmark-postal-service-mail-ce78db2f2234a50e676063fac790a617
2•donohoe•20m ago•0 comments

Guide for Migrarting from Radix UI to Base UI

https://shadcnstudio.com/blog/migrate-from-radix-ui-to-base-ui
1•devsoul•20m ago•0 comments

Grand Theft Oil Futures: Insider traders keep making a killing at our expense

https://paulkrugman.substack.com/p/grand-theft-oil-futures
2•Qem•21m ago•0 comments

How the oilman's president boosted a green transition

https://www.ft.com/content/a55fb3cb-2b04-4608-9b96-6544e8b803df
1•doener•22m ago•0 comments

LinkedIn profile visitor lists belong to the people, says Noyb

https://www.theregister.com/offbeat/2026/05/05/noyb-cries-foul-on-linkedin-withholding-profile-vi...
3•robin_reala•23m ago•0 comments

I built a free GPT to test whether an idea is clear before launch

https://chatgpt.com/g/g-69f11b6a1bdc8191ab1100acf60f40cc-deltdx-free
1•DELTAX-Editions•25m ago•0 comments

Godot Usage and Engine Growth

https://godotengine.org/article/godot-growth-stats-2026/
1•__natty__•28m ago•0 comments

Practical Effect Handlers in Capability-Passing Style

https://link.springer.com/chapter/10.1007/978-3-030-83128-8_3
1•noelwelsh•29m ago•1 comments

Idempotency Is Easy Until the Second Request Is Different

https://blog.dochia.dev/blog/idempotency/
2•ludovicianul•29m ago•0 comments

Show HN: AICW Video open-source to cut video into clips with captions, voiceover

https://github.com/aicw-io/aicw-video
1•eummm•33m ago•0 comments

Open-Ended Survey? Definition, Benefits, AI Analysis and How to Create One

https://figshare.com/articles/journal_contribution/_sup_strong_What_Is_an_Open-Ended_Survey_Defin...
1•aishasajjad•35m ago•0 comments

Solod v0.1: Go ergonomics, practical stdlib, native C interop

https://antonz.org/solod-v0-1/
2•blenderob•36m ago•0 comments

AI Agent Is Lying to You in 2026 – and It's Getting Worse

https://travel4fun4u1.substack.com/p/your-ai-agent-is-lying-to-you-in
4•Travel4Fun4U•41m ago•0 comments

Rivercane: Our Native Bamboo

https://www.nps.gov/articles/000/rivercane.htm
1•thunderbong•42m ago•0 comments

The Life and Times of an American Tween

https://www.newyorker.com/magazine/2026/05/11/the-life-and-times-of-an-american-tween
1•ironyman•43m ago•0 comments

Apple discontinues 256GB Mac Studio option

https://9to5mac.com/2026/05/05/apples-most-powerful-mac-studio-loses-its-last-remaining-ram-upgra...
1•chriswep•44m ago•0 comments