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.

Architecture as Mitigation

https://blog.ljebu.com/#architecture-as-mitigation
1•ljebu•36s ago•0 comments

Glyph Protocol for Terminals

https://rapha.land/introducing-glyph-protocol-for-terminals/
1•citruscomputing•40s ago•0 comments

AI and software security: the slop is now signal

https://anderegg.ca/2026/05/10/ai-and-software-security-the-slop-becomes-signal
1•Brajeshwar•1m ago•0 comments

Show HN: Liquid Radius – Unifies macOS Tahoe's inconsistent window corners

https://liquidradius.com/
1•OlyEvans•2m ago•0 comments

Why age assurance laws matter for developers

https://github.blog/news-insights/policy-news-and-insights/why-age-assurance-laws-matter-for-deve...
1•voxleone•3m ago•0 comments

Munitions startup, Heaviside, out of stealth – $28M Series A

https://www.tectonicdefense.com/exclusive-heaviside-emerges-from-stealth-with-28m-series-a/
1•dth2m5•3m ago•1 comments

Copilot "auto-pilot" system instructions making models worst

1•xlayn•3m ago•0 comments

The Surprisingly Long Life of the Vacuum Tube

https://www.construction-physics.com/p/the-surprisingly-long-life-of-the
1•surprisetalk•4m ago•0 comments

Taskiq – distributed task queue for Python

https://github.com/taskiq-python/taskiq
1•hyzyla•4m ago•0 comments

LeoLabs

https://platform.leolabs.space/visualizations/leo#view=originCountry
1•structuredPizza•5m ago•0 comments

How financial architectures shaped (and is shaping) Chinese drug development

https://www.owlposting.com/p/how-financial-architectures-shaped
1•crescit_eundo•5m ago•0 comments

Chinese court rules it illegal to replace workers with AI purely to cut costs

https://www.yahoo.com/news/articles/chinese-court-rules-illegal-replace-043000635.html
1•speckx•7m ago•0 comments

Pwn2Own Berlin 2026 just hit a wall

https://twitter.com/IntCyberDigest/status/2053802477019906058
1•metnewgr•8m ago•0 comments

Windows 11 is getting a macOS-like speed boost

https://www.theverge.com/news/927736/windows-11-is-getting-a-macos-like-speed-boost
1•stormed•8m ago•0 comments

What I've learned designing agentic workflows for docs

https://passo.uno/agentic-workflows-for-docs/
1•theletterf•9m ago•0 comments

Machines for Conviviality – dancing bees and emotionally intelligent machines

https://intempus.org/writings/machines-for-conviviality/
1•Twarner•9m ago•0 comments

Anthropic's Claude used in attempted compromise of Mexican water utility

https://www.cybersecuritydive.com/news/anthropics-claude-compromise-mexican-water-utility/819710/
1•speckx•11m ago•0 comments

Show HN: I built an anonymous social platform after fighting with LinkedIn's API

https://discreetvoice.com
1•arstep•11m ago•0 comments

Notes on YC P26, halfway through the batch

https://www.natecation.com/notes-on-yc-spring-2026/
1•18nleung•11m ago•0 comments

Cochlear Implant

https://en.wikipedia.org/wiki/Cochlear_implant
1•tosh•12m ago•0 comments

Token, Harness, OpenClaw, RAG, MCP, Agent – What's the Difference?

https://medium.com/ob4ai/token-harness-openclaw-rag-mcp-agent-whats-the-difference-one-map-makes-...
2•xiaocuiusa•13m ago•0 comments

The Teaching Computer

https://build.ms/2026/5/11/the-teaching-computer/
2•mergesort•13m ago•0 comments

Common MongoDB Mistakes Developers Make (and How to Fix Them)

https://visualeaf.com/blog/3-common-mongodb-mistakes/
3•RoxiHaidi•14m ago•1 comments

A gradient component library – inspired by retinaa.ch

https://github.com/alexpuliatti/gradient-components
1•duchamp_s•15m ago•0 comments

Sulfurous hell world might change the way we classify exoplanets

https://www.scientificamerican.com/article/this-sulfurous-hell-world-might-change-the-way-we-clas...
1•beardyw•16m ago•0 comments

AI to undo your AI writing

https://sinceerly.com/
1•cratermoon•17m ago•0 comments

Ask HN: What SIMD for fixed point references do you use

1•xphos•17m ago•0 comments

A Job at OpenAI Became the Greatest Lottery Ticket of the AI Boom

https://www.wsj.com/tech/openai-employee-stock-sales-71ed10bd
3•littlexsparkee•19m ago•2 comments

Environmental Footprint of a Low-Fat Vegan Diet in People with Type 1 Diabetes

https://www.sciencedirect.com/science/article/pii/S2475299126000740
1•PaulHoule•19m ago•0 comments

Skill-kit – TypeScript SDK for building agent skills as typed state machines

https://github.com/contentful/skill-kit
1•ravel•20m ago•0 comments