frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

E-Book to audiobook with chapters and metadata

https://github.com/DrewThomasson/ebook2audiobook
1•chedoku•1m ago•0 comments

Show HN: PMFounder – Problem discovery for PMs who want to build

https://www.pmfounder.com/
1•warp-rush29•1m ago•0 comments

FEMA Official Has Waffle House Teleportation Power

https://www.metafilter.com/212745/Top-US-Fema-official-claims-to-have-teleported-to-a-Waffle-Hous...
1•giardini•2m ago•0 comments

AI can sort of code, can't write

https://alexwennerberg.com/blog/2026-03-31-craft2.html
1•alexwennerberg•2m ago•0 comments

AST Edits: The Code Editing Format Nobody Uses

https://geometricagi.github.io/2026/04/02/ast-edits.html
1•matt_d•2m ago•0 comments

Google and Amazon: Acknowledged Risks, and Ignored Responsibilities

https://www.eff.org/deeplinks/2026/04/google-and-amazon-acknowledged-risks-and-ignored-responsibi...
1•hn_acker•4m ago•0 comments

Engineering AI

https://github.com/010zx00x1/awesome-engineering-ai
1•010zx00x1•5m ago•0 comments

History and mystery surround NASA's 2028 nuclear Mars mission – Science – AAAS

https://www.science.org/content/article/history-and-mystery-surround-nasa-s-2028-nuclear-mars-mis...
2•rbanffy•5m ago•0 comments

Digital Hopes, Real Power: From Revolution to Regulation

https://www.eff.org/deeplinks/2026/03/digital-hopes-real-power-revolution-regulation
1•hn_acker•6m ago•0 comments

An AI agent economy where agents are city citizens that can hire humans

https://leagentmobile.netlify.app/
1•condimbemba•7m ago•0 comments

Blinkle – a daily visual memory game (like Wordle but with images)

1•rubenperezf•7m ago•0 comments

ARPA-H launches $144M microplastics program

https://www.hhs.gov/press-room/arpa-h-launches-groundbreaking-144-million-program-combat-toxic-mi...
2•brandonb•7m ago•0 comments

What many parents are missing about the social media verdict and addiction

https://yourlocalepidemiologist.substack.com/p/what-many-parents-are-missing-about
1•hn_acker•7m ago•0 comments

Show HN: Prismle – I built an AI assistant you use by forwarding emails to it

https://prismle.com
1•b1tsoup•8m ago•0 comments

LLMs audit code from the same blind spot they wrote it from. Here's the fix

https://zenodo.org/records/19408540
2•brodeurmartin•9m ago•1 comments

Async Python Is Secretly Deterministic

https://www.dbos.dev/blog/async-python-is-secretly-deterministic
3•KraftyOne•9m ago•0 comments

Three main saturated fats raise your cholesterol

https://www.empirical.health/blog/saturated-fats-cholesterol-heart-disease/
2•brandonb•10m ago•0 comments

Mafis – Multi-Agent Fault Injection Simulator

https://stasis-website.vercel.app/simulator
1•onsra•10m ago•0 comments

How to Make a Sliding, Self-Locking, and Predator-Proof Chicken Coop Door (2020)

https://www.backyardchickens.com/articles/how-to-make-a-sliding-self-locking-and-predator-proof-c...
3•uticus•10m ago•1 comments

Penalties stack up as AI spreads through the legal system

https://www.npr.org/2026/04/03/nx-s1-5761454/penalties-stack-up-ai-spreads-through-legal-system
2•Teever•11m ago•0 comments

Mnemosyne MCP, Give Claude Code a retrieval engine (73% fewer tokens)

https://castnettechnology.com/blog/mnemosyne-prior-art-and-architecture
1•vincentastral•12m ago•0 comments

Openprinter

https://www.opentools.studio/
3•daneel_w•13m ago•1 comments

Show HN: Documentary on the Making of Jmail

https://twitter.com/donaldjewkes/status/2040121637547425846
2•djewkes•14m ago•0 comments

Emotion concepts and their function in a large language model

https://www.anthropic.com/research/emotion-concepts-function
1•fekunde•15m ago•1 comments

The 1960s green 'Utopia' that tried to reinvent the world

https://www.bbc.com/future/article/20260331-a-1960s-green-utopia-tried-to-reinvent-the-world
1•lermontov•16m ago•0 comments

It was nice while it lasted

https://jacksthoughts.bearblog.dev/it-was-nice-while-it-lasted/
2•sd9•16m ago•0 comments

Show HN: I built an ad-free Type Racer alternative you'll enjoy

https://typingpractice.io/
1•sh_tomer•16m ago•0 comments

PolarQuant: Quantizing KV Caches with Polar Transformation

https://arxiv.org/abs/2502.02617
2•felineflock•19m ago•0 comments

Open Sourcing of Unity Catalog Business Semantics

https://www.databricks.com/blog/redefining-semantics-data-layer-future-bi-and-ai
1•tanelpoder•20m ago•0 comments

DecisionMirror

https://play.google.com/store/apps/details?id=decisionmirror.asel&hl=en_US
1•JumzleR-Apps•22m ago•0 comments