frontpage.
newsnewestaskshowjobs

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.

Debian and the Sirens

https://joeyh.name/blog/entry/Debian_and_the_sirens/
1•klez•4m ago•0 comments

Autistici/inventati: Manifesto – who we are and what do we want (2002)

https://www.inventati.org/who/manifesto
4•wise_blood•6m ago•0 comments

Show HN: Airtight – single-file portfolio tracker, zero servers, works offline

https://airtight.sell.app
1•AirTight•6m ago•0 comments

Show HN: Doodle AI: open-source photo-to-doodle avatar generator

https://doodleai.art/
1•Yash16•6m ago•1 comments

To Human Is to Explore

https://panchamkhaitan.com/blog/to-human-is-to-explore
1•panchamkhaitan•7m ago•0 comments

Agent Security Is a Systems Problem: What 247 Papers Say About Secure AI Agents

https://www.truefoundry.com/blog/llm-agent-security-runtime-control-planes
1•LogTrim•8m ago•0 comments

The best workflow engine is a programming language

https://vercel.com/blog/the-best-workflow-engine-is-a-programming-language
1•theanonymousone•14m ago•0 comments

Sharp rise in incidents of AI escaping users' control, research finds

https://www.theguardian.com/technology/2026/aug/29/sharp-rise-in-incidents-of-ai-escaping-users-c...
1•geoffbp•16m ago•0 comments

AI Image Generation on an RP2350 Microcontroller

https://cpldcpu.github.io/2026/08/28/ai-image-generation-on-a-rp-pico-2-microcontroller/
1•rcarmo•18m ago•1 comments

Show HN: Explore JVM class-resolution traces as interactive directed graphs

https://github.com/menzew/class-trace-explorer
1•benzewdu•19m ago•0 comments

Skirminiac – An AI-first deterministic wargame

https://www.skirminiac.com
1•LouisSaifer•30m ago•1 comments

Impact of Google AI Overviews on Wikipedia Traffic

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6164926&__cf_chl_tk=yAhL3EN950gYq8L72ow6o3fGC...
1•alexcos•32m ago•0 comments

Talk Is Cheap. Code Is Cheap. Show Me the Source

https://idlip.in/posts/show-me-source/
1•birdculture•33m ago•0 comments

Over 8,300 Gitea servers vulnerable to code execution attacks

https://www.bleepingcomputer.com/news/security/over-8-300-gitea-servers-vulnerable-to-code-execut...
2•geoffbp•39m ago•0 comments

Htmx and Django LiveView, side by side

https://en.andros.dev/blog/94d14a9e/htmx-and-django-liveview-side-by-side/
1•andros•39m ago•0 comments

How to sign up for a virtual power plant–and decide whether you should

https://www.technologyreview.com/2026/08/28/1142956/how-to-sign-up-for-a-virtual-power-plant-and-...
2•joozio•43m ago•0 comments

The Reliable Team Trap

https://kevingoldsmith.substack.com/p/the-reliable-team-trap
2•fagnerbrack•43m ago•1 comments

Zero-Day EoP in Kaspersky Endpoint Security Lets Attackers Own the Protector

https://forum.cktn.de/t/big-breaking-guardian-turned-gatekeeper-zero-day-eop-in-kaspersky-endpoin...
1•CHEF-KOCH•47m ago•1 comments

Hyperuplink: Discuss Like It's 1998

https://xn--gckvb8fzb.com/hyperuplink-discuss-like-its-1998/
2•ErenayDev•50m ago•0 comments

GaussianGPT: Generating 3D scenes autoregressively with Gaussian Splatting

https://github.com/nicolasvonluetzow/GaussianGPT
2•DarenWatson•1h ago•0 comments

I Built the Mazda Suitcase Car [video]

https://www.youtube.com/watch?v=rpV4phLQsE4
2•the-mitr•1h ago•0 comments

Bookhoarder: Open-Source, Self-Hosted ePub Library on Object Storage

https://bookhoarder.dev/
2•0megion•1h ago•1 comments

e is a customizable self-aware Emacs-like editor written in Chez Scheme

https://github.com/paveluv/e
7•p4bl0•1h ago•4 comments

Show HN: Tokensift, an open-sourced token-efficiency linter for LLM prompts

https://github.com/ritenv/tokensift
2•ritenv•1h ago•0 comments

FuturePost Unlock – Create time-locked QR codes that unlock your message later

https://www.haoqibit.com/futurepost-unlock
2•Popang•1h ago•0 comments

We probed every remote server in the official MCP registry (15,329 URLs)

https://fetchgate.dev/blog/mcp-registry-audit-2026
2•chiefgrowth•1h ago•0 comments

Answers to questions about upstreaming patches

https://www.thomas-huehn.com/answers-upstreaming/
2•Tomte•1h ago•0 comments

I tried vibe-coding a memory tool for AI and stared into the abyss of philosophy

https://arbustoemchamas.substack.com/p/i-naively-tried-vibe-coding-a-memory
2•milon•1h ago•1 comments

Beyond the Editing Canvas: Evidence Divergence in Ooxml-to-LLM Ingestion

https://arxiv.org/abs/2608.25880
3•sbulaev•1h ago•0 comments

Samsung's Processing-in-Memory (PIM)

https://chipsandcheese.com/p/hot-chips-2026-samsungs-processing
21•ingve•1h ago•1 comments