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.

Shield Messenger A Tor-native,P2P encrypted messenger with built-in Solana/Zcash

https://github.com/abokenan444/shield-messenger
1•abokenan444•6m ago•1 comments

AutoResearchClaw

https://github.com/aiming-lab/AutoResearchClaw
1•frozenseven•6m ago•0 comments

The Space Data Center Mass Budget Behind 10× Power Density

https://research.33fg.com/analysis/the-space-data-center-mass-budget-behind-10x-power-density
1•T-A•7m ago•0 comments

//go:fix inline and the source-level inliner

https://go.dev/blog/inliner
1•vismit2000•10m ago•0 comments

Claude Code tips for non-programmers

https://thewriting.dev/claude-code-isnt-just-for-developers/
1•r0rshrk•14m ago•0 comments

Three Claude Skills to Sharpen Judgment

https://age-of-product.com/three-ai-skills-to-sharpen-judgment/
1•swolpers•14m ago•0 comments

Hybrid AI Desktop Layer Combining DOM-Automation and API-Integrations

https://github.com/BiamOS/BiamOS
1•BiamOS•15m ago•1 comments

Jazzband Is Sunsetting

https://jazzband.co/
2•taubek•17m ago•0 comments

Base44 can now power any front end with standalone back end as a service

https://base44.com/blog/base44-backend-platform
1•yoavcwix•22m ago•0 comments

LocalCowork

https://github.com/Liquid4All/cookbook/tree/main/examples/localcowork
1•armcat•28m ago•0 comments

MaiaSpace: Europe steps up in the race for reusable rockets

https://www.euronews.com/my-europe/2026/03/13/maiaspace-europe-steps-up-in-the-race-for-reusable-...
1•vrganj•29m ago•0 comments

Str:::Lab Studio – run and test Flink SQL from the browser

https://coded-streams.github.io/strlabstudio/
1•nestormartourez•33m ago•1 comments

How LLMs and coding agents change the dynamics of adopting Rust

https://mdwdotla.medium.com/revisiting-rust-in-2026-ae8720cc7f2c
1•mad•34m ago•0 comments

Show HN: Fortress Language: Cybersecurity DSL

1•CzaxTanmay•36m ago•0 comments

Show HN: Voice-tracked teleprompter using on-device ASR in the browser

https://github.com/larsbaunwall/promptme-ai
1•lbaune•39m ago•1 comments

The O16g Manifesto – Outcome Engineering

https://o16g.com/manifesto/
1•stigi•39m ago•0 comments

When Is Enough?

https://ivanca.github.io/ai/elite/2026/03/15/when-is-enough/
1•AmbroseBierce•41m ago•0 comments

I built V2 of my AI answer generator

https://99helpers.com/tools/ai-answer-generator
2•nickk81•46m ago•1 comments

Home-Made Shock Diamond

https://physics.stackexchange.com/questions/41293/home-made-shock-diamond
1•whalee•47m ago•0 comments

Six ingenious ways how Canon DSLRs used to illuminate their autofocus points

https://exclusivearchitecture.com/03-technical-articles-CSDS-00-table-of-contents.html
2•ExAr•47m ago•1 comments

Britain's Populist Right Has Surrendered Its Mind to America

https://liambyrne.substack.com/p/take-back-control
3•tastyface•47m ago•0 comments

The enshittification of Amazon paperback books

https://www.alexerhardt.com/en/enshittification-amazon-paperback-books/
10•aerhardt•48m ago•1 comments

Microsoft Hasn't Had a Coherent GUI Strategy Since Petzold

https://www.jsnover.com/blog/2026/03/13/microsoft-hasnt-had-a-coherent-gui-strategy-since-petzold/
2•freetonik•50m ago•0 comments

Largest German gym for humanoid robots being built in Munich

https://www.heise.de/en/news/Largest-German-gym-for-humanoid-robots-being-built-in-Munich-1120721...
1•thm•50m ago•0 comments

Open Alleged PhotoDNA

https://github.com/ArcaneNibble/open-alleged-photodna
1•edent•52m ago•0 comments

Multi-Claude – manage multiple Claude CLI accounts with shared and cloud sync

https://www.npmjs.com/package/@ghackk/multi-claude
1•geeky_geeker•53m ago•1 comments

The Operator That Dethroned a King: Python's Walrus Operator Story

https://techlife.blog/posts/the-operator-that-dethroned-a-king-pythons-walrus-operator-story/
1•clarkmaxwell•54m ago•0 comments

I Used Claude Code to reverse engineer a 13-year-old game binary

https://old.reddit.com/r/ClaudeAI/comments/1ru3irp/i_used_claude_code_to_reverse_engineer_a/
2•virgildotcodes•55m ago•1 comments

Agent Context Is Data. Treat It That Way

https://medium.com/@a.mandyev/agent-context-is-data-treat-it-that-way-cd8bcfd03ced
1•andrey_m•59m ago•1 comments

UK must build own nuclear missiles, say Lib Dems

https://www.bbc.co.uk/news/articles/cy0dz1k0rr4o
1•mmarian•1h ago•0 comments