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.

GPT Image 2 Launch

https://twitter.com/arena/status/2046670703311884548
1•twtw99•1m ago•0 comments

Usmnt players designed the boldest kits in generations for World Cup 2026

https://www.theguardian.com/football/2026/mar/16/usmnt-kits-world-cup-2026
1•PaulHoule•3m ago•0 comments

Flex Routing (EU and EFTA) for Copilot LLM Data Processing

https://learn.microsoft.com/en-us/microsoft-365/copilot/copilot-flex-routing
1•raffael_de•3m ago•1 comments

I don't want your PRs anymore

https://dpc.pw/posts/i-dont-want-your-prs-anymore/
2•speckx•3m ago•0 comments

Courier: Real-Time Messaging for ESP32

https://interconnected.org/home/2026/04/21/courier
1•beardicus•4m ago•0 comments

Bond: A new AI social network that turns memories into discoveries

https://www.bond.now/
1•johndavisonr•5m ago•0 comments

Nothing ever dies. It merely becomes embarrassing

https://www.experimental-history.com/p/nothing-ever-dies-it-merely-becomes
1•paulpauper•6m ago•0 comments

The New Age of Performance Anxiety

https://www.theatlantic.com/culture/2026/04/screen-people-stage-fright-performance-anxiety/686803/
1•paulpauper•6m ago•0 comments

What It's Like to Live with an Experimental Brain Implant

https://spectrum.ieee.org/bci-user-experience
1•digital55•6m ago•0 comments

Wearable health tech might be Tim Cook's greatest legacy

https://www.theverge.com/tech/915976/tim-cook-john-ternus-apple-watch-health-tech-wearables
1•paulpauper•7m ago•0 comments

The Fossils 1969

https://www.youtube.com/watch?v=bn1uhSS1cDo
1•indigodaddy•7m ago•0 comments

Amtrak's "1MB" National Route Map PDF Is a 574MB File

https://www.amtrak.com/train-routes
1•tech234a•7m ago•0 comments

Iconiq, Go-To Wealth Adviser for Tech's Elite, Is Putting Billions into AI

https://www.bloomberg.com/news/articles/2026-04-17/iconiq-advisor-to-tech-billionaires-emerges-as...
1•petethomas•7m ago•0 comments

The power keeping wages low

https://text.npr.org/g-s1-118071
1•mooreds•8m ago•0 comments

InvenTree: Open-source inventory management system with OpenAPI

https://github.com/inventree/InvenTree
1•matmair•9m ago•1 comments

Brex founder open sourced his stack for running the company through OpenClaw

https://github.com/brexhq/CrabTrap
1•ofabioroma•10m ago•1 comments

Cube Sandbox: Instant, Concurrent, Secure and Lightweight Sandbox for AI Agents

https://docs.cubesandbox.ai/
1•bpierre•10m ago•0 comments

Plastic film covered in tiny pillars can tear apart viruses on contact

https://theconversation.com/new-plastic-film-covered-in-thousands-of-tiny-pillars-can-tear-apart-...
2•geox•11m ago•0 comments

Privacy raised during teen social media ban tech trial were ignored

https://www.themandarin.com.au/311397-privacy-raised-during-teen-social-media-ban-tech-trial-were...
1•cdrnsf•11m ago•0 comments

OpenAI Shuts Down Sora AI? But Why?

https://www.bbc.com/news/articles/c3w3e467ewqo
2•shockedstorys•16m ago•0 comments

Show HN: FMQL – graph query and bulk-edit CLI for Markdown and YAML frontmatter

https://github.com/buyuk-dev/fmql
1•buyukdev•16m ago•1 comments

Retro Rewind – Video Store Simulator

https://store.steampowered.com/app/3552140/Retro_Rewind__Video_Store_Simulator/
1•doener•18m ago•0 comments

Can you spend $600K on B300 GPU Server? Which LLM will you run on this?

https://www.dihuni.com/
1•tech_curator•18m ago•0 comments

The Deskilling Paradox

https://signalintent.net/2026/04/21/the-deskilling-paradox/
1•tokonomy_dev•20m ago•0 comments

Lotus Wiper: a new threat targeting the energy and utilities sector

https://securelist.com/tr/lotus-wiper/119472/
1•campuscodi•21m ago•0 comments

Perry, a TypeScript compiler written in Rust that targets nine platforms

https://www.perryts.com/
1•bpierre•21m ago•0 comments

What Drives AI Crawler Traffic?

https://www.searchenginejournal.com/68-million-ai-crawler-visits-show-what-drives-ai-search-visib...
1•restlessforge•21m ago•1 comments

NSA loads Anthropic Mythos cyberattack while Pentagon says it cannot

https://aitwerp.com/signals/nsa-cyberattack-consent-bypassed/
1•Inziu•23m ago•0 comments

Delegation as an OS Primitive

https://mz.attahri.com/posts/delegation-as-os-primitive/
1•mohamedattahri•25m ago•0 comments

Stop Paying the JSON Tax

https://columnar.tech/blog/stop-paying-the-json-tax//
1•ianmcook•26m ago•0 comments