frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

EU and Brazil conclude agreements on free and safe data flows

https://ec.europa.eu/commission/presscorner/home/en
1•marcuschong•2m ago•0 comments

Show HN: A simple, transaction-safe SQL migration tool

https://github.com/samueldurantes/siquil
1•samueldurante•2m ago•0 comments

Building multi AI awareness in AI group chats

https://cochat.ai/building-multi-ai-awareness-how-cochat-enables-ai-models-to-coexist-in-group-co...
1•intheleantime•3m ago•0 comments

CVE-2026-23993: JWT authentication bypass in HarbourJwt via "unknown alg"

https://pentesterlab.com/blog/cve-2026-23993-harbourjwt-unknown-alg-jwt-bypass
1•lovedhacker•7m ago•0 comments

PostgreSQL on Kubernetes vs. VMs: A Technical Decision Guide

https://stormatics.tech/blogs/postgresql-on-kubernetes-vs-vms-a-technical-decision-guide
3•ioololaa•8m ago•0 comments

Clawdbot Remembers Everything

https://twitter.com/manthanguptaa/status/2015780646770323543
1•jxmorris12•9m ago•0 comments

Ask HN: Block short-form videos on home network?

1•mark13•10m ago•0 comments

Windows 11 KB5074109 update nukes Nvidia gaming performance and stability

https://www.notebookcheck.net/Windows-11-KB5074109-update-nukes-Nvidia-gaming-performance-and-sta...
1•akyuu•10m ago•0 comments

Gladys West, Unsung Figure in Development of GPS, Dies at 95

https://www.nytimes.com/2026/01/27/science/gladys-west-dead.html
1•donohoe•12m ago•0 comments

TikTok claimed bugs blocked anti-ICE videos, Epstein mentions; experts call BS

https://arstechnica.com/tech-policy/2026/01/tiktok-claimed-bugs-blocked-anti-ice-videos-epstein-m...
2•coloneltcb•12m ago•0 comments

Cursed Units 3: The British Empire Strikes Back [video]

https://www.youtube.com/watch?v=PWbfVcDcfFw
1•nativeit•13m ago•0 comments

Old Insurance Maps – Georeferencing Sanborn Fire Insurance Maps on Modern Maps

https://oldinsurancemaps.net/
1•lapetitejort•13m ago•1 comments

Show HN: An interactive jazz study guide

https://vladris.com/jazz-book/
1•vladris•14m ago•0 comments

Lightweight Transformer Architectures for Edge Devices in Real-Time Applications

https://arxiv.org/abs/2601.03290
1•PaulHoule•14m ago•0 comments

CVE-2026-22709: Critical Sandbox Escape in Vm2 Enables Arbitrary Code Execution

https://www.endorlabs.com/learn/cve-2026-22709-critical-sandbox-escape-in-vm2-enables-arbitrary-c...
2•gnabgib•15m ago•0 comments

The Plan Is to Make the Internet Worse. Forever. – Bastani and Doctorow [video]

https://www.youtube.com/watch?v=7wE8G-d7SnY
4•foofoo4u•15m ago•0 comments

TurboScribe

https://grokipedia.com/page/TurboScribe
1•leiferik•21m ago•0 comments

An LLM that's 7500× stupider

https://evanhahn.com/an-llm-thats-7500x-stupider/
1•EvanHahn•30m ago•1 comments

Tell HN: JumpCloud 2FA appears to be down

1•sgammon•30m ago•0 comments

Amazon to pay $309M to U.S. shoppers in settlement over returns

https://www.reuters.com/legal/government/amazon-pay-309-million-us-shoppers-settlement-over-retur...
5•TMWNN•31m ago•0 comments

Notes on Starting to Use Django

https://jvns.ca/blog/2026/01/27/some-notes-on-starting-to-use-django/
1•ingve•34m ago•0 comments

DeepSeek OCR2

https://huggingface.co/deepseek-ai/DeepSeek-OCR-2
1•chaosprint•34m ago•1 comments

Linux CLI for extracting archives inside a sandbox (alpha)

https://github.com/Chechelpo/Sandex
1•Chechelpo•34m ago•1 comments

Zero-ETL lakehouses for Postgres people

https://neon.com/blog/zero-etl-lakehouses-for-postgres-people
2•gmac•35m ago•0 comments

How I manage tasks in 2026

https://www.shubhro.com/how-manage-tasks-2026/
2•shbhrsaha•37m ago•0 comments

Disentangling unification and implicit coercion (subtyping interaction problem)

https://www.jonmsterling.com/01JQ/
1•matt_d•37m ago•0 comments

NASA plane makes belly landing at Ellington Airport

https://www.khou.com/article/news/local/ellington-field-nasa-plane-belly-landing-houston-texas/28...
4•ortusdux•38m ago•2 comments

Attention Is Not What You Need

https://arxiv.org/abs/2512.19428
3•bilsbie•40m ago•0 comments

Why AI Coding Advice Contradicts Itself

https://www.anup.io/why-ai-coding-advice-contradicts-itself/
1•lalitmaganti•40m ago•0 comments

Learnings from Anders Hejlsberg: The architect behind C# and TypeScript

https://github.blog/developer-skills/programming-languages-and-frameworks/7-learnings-from-anders...
1•ingve•41m ago•0 comments