frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

My First Agentic Coding Project: GitTop

https://hjr265.me/blog/building-gittop-with-agentic-coding/
1•birdculture•47s ago•0 comments

The Bay Area Considers the Unthinkable: Life Without BART

https://www.nytimes.com/2026/03/10/us/bart-bay-area-san-francisco-transit.html
1•JumpCrisscross•1m ago•0 comments

The rise of malicious repositories on GitHub

https://rushter.com/blog/github-malware/
2•yakattak•2m ago•0 comments

With H-1B work visa lottery underway, experts worry new rules hurt business

https://www.wbur.org/news/2026/03/12/h-1b-visas-lottery-trump-overhaul
1•rawgabbit•2m ago•0 comments

Delay the Inference

https://aishwaryagoel.com/delay-the-inference/
1•agcat•7m ago•0 comments

Detecting Smart Glasses from your phone

https://nearby-glasses-alert.pages.dev/
1•modexapps•7m ago•0 comments

Bcachefs 1.37 with Linux 7.0 Support, Erasure Coding Stable and New Sub-Commands

https://www.phoronix.com/news/Bcachefs-1.37-Released
1•shaicoleman•8m ago•0 comments

LLM FFN benchmarks on a 4‑core HP All‑in‑One

https://rolv.ai/
1•heggenhougen•9m ago•2 comments

From Metaphysical Unity to Moral-Ecological Memory (DT2/RRR) Gianluigi Segalerba

https://www.academia.edu/165188201/From_Metaphysical_Unity_to_Moral_Ecological_Memory_A_Disenchan...
1•WGriffinIII•10m ago•0 comments

Nova–Self-hosted personal AI that learns from corrections and fine-tunes itself

https://github.com/HeliosNova/nova
1•heliosnova•10m ago•2 comments

Safire Memo [pdf]

https://www.archives.gov/files/presidential-libraries/events/centennials/nixon/images/exhibit/rn1...
1•stickynotememo•12m ago•0 comments

Show HN: Webassembly4J Run WebAssembly from Java

1•tegmentum•20m ago•0 comments

TerraInk: Cartographic Poster Engine-creates unique and customizable map posters

https://github.com/yousifamanuel/terraink
1•thunderbong•23m ago•0 comments

Civic opinion sharing to promote democracy

https://www.ourpublicvoice.org/
1•devpromgr•23m ago•1 comments

New study raises concerns about AI chatbots fueling delusional thinking

https://www.theguardian.com/technology/2026/mar/14/ai-chatbots-psychosis
4•pseudolus•27m ago•1 comments

Global EV sales hit 1.1M – Europe surges while the US slides

https://electrek.co/2026/03/12/global-ev-sales-hit-1-1-million-europe-surges-while-the-us-slides/
1•pseudolus•29m ago•1 comments

The Hype-Beast Crisis

https://excipio.tech/blog/the-hype-beast-crisis/
2•lvales•32m ago•0 comments

Sanity Check Your Docs for Human Readability

https://docsalot.dev/tools/docs-human-readability
2•fazkan•34m ago•0 comments

Bypass LLM's guardrails with logical prompts – no coding

https://charalamposkitzoglou.substack.com/p/the-contextual-singularity-exploiting
2•rhsxandros•35m ago•1 comments

Offline password generator based on physical dice

https://dicendo.app/
2•rafaldot•36m ago•1 comments

Millwright: Smarter Tool Selection from Agent Experience

https://minor.gripe/posts/2026-03-13-millwright_smarter_tool_selection_with_adaptive_toolsheds/
2•dnautics•38m ago•0 comments

Show HN: Playtest my browser-based RPG

https://archipelago-sandy.vercel.app/
3•blinkbat•40m ago•0 comments

I Hate Anthropic and You Should Too

https://danielmiessler.com/blog/why-you-should-hate-anthropic
5•arkhadus•42m ago•4 comments

Self-Driving Codebase: Background agents and the next era of software delivery

https://background-agents.com/
2•iBelieve•43m ago•0 comments

CIA Prepares Criminal Referral of Tucker Carlson, as Israel et al. Demand Arrest

https://greenwald.substack.com/p/cia-prepares-criminal-referral-of
9•treetalker•46m ago•0 comments

Giving MCP servers a voice in tool selection

https://divanv.com/post/server-side-tool-gating/
2•divanvisagie•52m ago•0 comments

Manjaro Linux 2.0 Manifesto

https://forum.manjaro.org/t/manjaro-2-0-manifesto/186171
3•hmm37•55m ago•1 comments

To fall asleep faster, try cognitive shuffling

https://www.washingtonpost.com/wellness/2026/03/14/insomnia-cognitive-shuffling/
3•bookofjoe•58m ago•1 comments

Steve Jobs's 10-80-10 Rule Is More Useful in the AI Era

https://www.inc.com/jessica-stillman/steve-jobs-10-80-10-rule-is-even-more-useful-in-the-ai-era/9...
2•rosiesherry•59m ago•1 comments

Show HN: Open-source playground to red-team AI agents with exploits published

https://github.com/fabraix/playground
4•zachdotai•59m ago•0 comments