frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

'Catnomics': how Japan's feline fixation has become an industry worth billions

https://www.theguardian.com/world/2026/may/27/japan-obsessed-wth-cats-popular-pet-industry-worth-...
1•n1b0m•29s ago•0 comments

Why Does Your AI Agent Work Better for You Than for Me?

https://vexjoy.com/posts/why-your-ai-agent-works-better-for-you/
1•AndyNemmity•29s ago•0 comments

Credit card skimmer disguised as Google Tag Manager

https://anchor.host/so-you-get-hit-with-a-credit-card-skimmer-what-now/
1•logickkk1•47s ago•0 comments

I shipped a real product for $29.63 with five AI agents

https://github.com/vggg/agent-project-bootstrap
2•vggg•6m ago•0 comments

FML-Bench: A Controlled Study of AI Research Agent Strategies

https://arxiv.org/abs/2605.17373
1•matt_d•7m ago•0 comments

Crossing the Proof of Concept Valley

https://deploy95.substack.com/p/crossing-the-poc-valley
1•dddddaviddddd•9m ago•0 comments

When Quiet Undersea Volcanoes Turn Disruptive

https://www.quantamagazine.org/when-quiet-undersea-volcanoes-turn-disruptive-20260526/
1•anujbans•10m ago•0 comments

OpenRouter $113M Series C

https://www.nytimes.com/2026/05/26/business/dealbook/openrouter-ai-models-fundraising.html
1•swyx•13m ago•1 comments

I added achievements to my portfolio site

https://charlie.dudzik.me
1•cd-4•18m ago•0 comments

Power bills more than 250 per cent higher near data centres

https://www.theglobeandmail.com/investing/investment-ideas/article-market-factors-power-bills-mor...
2•cdrnsf•18m ago•0 comments

1.96.0 pre-release testing – Inside Rust Blog

https://blog.rust-lang.org/inside-rust/2026/05/26/1.96.0-prerelease/
1•kazu11max17•22m ago•0 comments

You Can't Stop This Data Center, a Mom Was Told. She Won't Quit

https://www.nytimes.com/2026/05/26/us/data-centers-kassi-solberg.html
3•1vuio0pswjnm7•25m ago•0 comments

Skills Folder Is a Junk Drawer

https://james-pritchard.com/blog/skills-junk-drawer
2•ArcaneMoose•27m ago•0 comments

Ambsheets: Spreadsheets for Exploring Scenarios

https://www.inkandswitch.com/ambsheets/notebook/
1•antran22•30m ago•0 comments

Micro-Expert-Router: Running Mixtral-Class Moe Models on NVMe SSDs Without a GPU

https://github.com/randyap8-wq/Micro-Expert-Router-SSD-Streamed-MoE-MER
1•randyap8•30m ago•0 comments

OpenAI's Altman says AI unlikely to lead to 'jobs apocalypse'

https://www.reuters.com/world/asia-pacific/openais-altman-says-ai-unlikely-lead-jobs-apocalypse-2...
4•1vuio0pswjnm7•31m ago•0 comments

Finding deadlocks in CuTe kernels with SPIN

https://metaworld.me/blog/public/Statically-finding-races-in-CUTE-kernels-or-Proving-absences-of-...
2•matt_d•32m ago•0 comments

A Case for Tracing Based DSL Kernel Languages

https://metaworld.me/blog/public/A-Case-for-Tracing-Based-DSL-Kernel-Languages
2•matt_d•34m ago•0 comments

Billionaire Mark Cuban says bye-bye Bitcoin: Why he is 'disappointed' by crypto

https://fortune.com/2026/05/26/mark-cuban-bitcoin-disappointed-crypto/
4•1vuio0pswjnm7•40m ago•0 comments

Google's Angle Merges Wayland Support, Unblocking Chromium Embedded Framework

https://www.phoronix.com/news/ANGLE-Merges-Wayland
4•DefineOutside•41m ago•0 comments

We reduced RAG retrieval cost 10× with a hippocampus-inspired memory substrate

https://www.bricbybric.ae/blog/hippocampus-memory-engine
4•aneesalsajir•41m ago•0 comments

The Codex Showcase

https://www.augmentedswe.com/p/openai-shows-you-how-to-use-codex
4•wordsaboutcode•44m ago•0 comments

Arias: Human Proof for FOSS Contributions

https://lwn.net/Articles/1074534/
2•prakashqwerty•45m ago•0 comments

The Coming Coordination Calamity

https://surfingcomplexity.blog/2026/05/24/the-coming-coordination-calamity/
2•wapasta•57m ago•0 comments

Ask HN: Looking for experienced web dev to make math website

2•marysminefnuf•1h ago•1 comments

Researchers Warn of WiFi 7 Routers as 'A Potential Means for Surveillance'

https://gizmodo.com/researchers-issue-warning-about-tech-that-could-turn-every-router-into-a-pote...
5•transpute•1h ago•3 comments

Clanker: A Word for the Machine

https://lucumr.pocoo.org/2026/5/26/clankers/
5•doppp•1h ago•1 comments

Solo founder building SaaS. looking for partner

2•albedim•1h ago•0 comments

How to Build an API-First Front End with OpenAPI, Orval, TanStack Query, Zod

https://reactdevelopment.substack.com/p/how-to-build-an-api-first-frontend
3•javatuts•1h ago•0 comments

Gear Commit: Dev gadget box personalized from GitHub activity

https://gearcommit.nanocorp.app/
4•silversharck•1h ago•0 comments