frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Amiga newspaper article from June 2000

https://old.reddit.com/r/amiga/comments/1tjhj5p/amiga_newspaper_article_from_june_2000/
1•doener•57s ago•0 comments

Mechanism design for large language models (2025)

https://research.google/blog/mechanism-design-for-large-language-models/
1•bayindirh•2m ago•1 comments

Show HN: (Better) Chrome Tab Manager

https://tabswish.in/
1•kanishkdan98•4m ago•0 comments

Show HN: SoMatic – Vision-based OS automation framework for AI agents

https://github.com/Smyan1909/SoMatic
1•smyansondur•5m ago•0 comments

Back up your TOTP codes by printing their QR codes

https://til.andrew-quinn.me/posts/back-up-your-totp-codes-by-just-printing-them/
1•hiAndrewQuinn•6m ago•0 comments

CPPL: A Circuit Prompt Programming Language

https://arxiv.org/abs/2605.17892
1•chrsw•7m ago•0 comments

Wikimedia Foundation Community Tech team has been disbanded, engineers laid off

https://en.wikipedia.org/wiki/Wikipedia:Village_pump_(WMF)
1•zczc•10m ago•0 comments

Show HN: Physics AI – Visualizing physics problems with step-by-step derivations

https://physicsai.chat
2•vansbree•12m ago•0 comments

Study: ChatGPT and other AI bots made errors before Scottish election

https://www.theguardian.com/technology/2026/may/20/ai-chatbots-chatgpt-replika-grok-gemini-misinf...
1•cdrnsf•13m ago•0 comments

LLM System Design Benchmark

https://nqbao.com/llm-system-design/
1•dcu•16m ago•0 comments

How NetEase Built an Authentic Wuxia Open World in Where Winds Meet

https://80.lv/articles/how-netease-built-an-authentic-wuxia-open-world-in-where-winds-meet
1•dlcmh•17m ago•0 comments

Planetary research: Innovative Mars rovers 'swim' through the sand

https://www.uni-wuerzburg.de/en/news-and-events/einblick/single/news/sandfish-mars-rover/
1•layer8•19m ago•0 comments

Opal Pathtracer

https://github.com/a-sumo/opal-pathtracer
1•rslice•20m ago•1 comments

My Mental Model for "Is It Worth Automating?" Has Changed

https://hermanschaaf.com/the-is-it-worth-automating-mental-model-has-changed/
1•jeffbobries•20m ago•0 comments

U.S. to Award Nine Quantum-Computing Firms $2B and Take Equity Stakes

https://www.wsj.com/tech/u-s-to-award-quantum-computing-firms-2-billion-and-take-equity-stakes-73...
1•giuliomagnifico•21m ago•1 comments

Show HN: A timeline of recent open source CVE intensity and volume

https://supplychain.fail
1•mariusvaporware•22m ago•0 comments

Show HN: PocketWebTools – Private AI tools that run in the browser

https://pocketweb.tools/
2•shafkathullah•22m ago•0 comments

Gjbjhb

1•Adityasshinde23•26m ago•0 comments

Friend or foul? Exploring the ancient bond between pigeons and people

https://phys.org/news/2026-05-friend-foul-exploring-ancient-bond.html
1•pseudolus•26m ago•1 comments

Show HN: Boatswain, A macOS menu bar app for Fathom Analytics

https://github.com/mirshko/boatswain
2•mirshko•27m ago•0 comments

Apache NetBeans 30

https://netbeans.apache.org/front/main/index.html
4•Tomte•29m ago•0 comments

Billionaire Brexit backers pledge to fight to keep Britain out of EU

https://www.telegraph.co.uk/business/2026/05/20/billionaire-brexit-backers-vow-fight-keep-britain...
3•robtherobber•32m ago•0 comments

Forma – Smart AI Autofill for Job and Placement Forms (100% Local, No Cloud)

https://chromewebstore.google.com/detail/forma/ahfjcnieohnohdjphlachclibhleinbi
1•chahalgoyal•33m ago•0 comments

AI Agent Support for Grid Global Accounts

https://www.lightspark.com/news/lightspark/introducing-ai-agent-support-for-grid-global-accounts
1•janandonly•34m ago•0 comments

Show HN: Post-Money SAFE Calculator – what your investment buys

https://safe.pimenov.cc/
1•kirushik•34m ago•0 comments

A Girl Who Couldn't Draw Home

https://www.gailweiner.com/post/the-girl-who-couldn-t-draw-home
8•signa11•36m ago•2 comments

AI Generated Code Looked Right, but the Data Was Wrong

https://mljar.com/blog/ai-generated-code-looked-right-data-was-wrong/
1•pplonski86•36m ago•0 comments

Show HN: Changed boring tip in Claude Code to a joke

https://github.com/vipulawl/claude-tips
1•vipulag•36m ago•0 comments

Bun's rust rewrite is a marketing stunt

https://versary.town/blog/bun-s-rust-rewrite-is-a-marketing-stunt/
1•signa11•38m ago•0 comments

Automating Science

https://scholar.social/@khinsen/116601408534950095
1•complex_pi•38m ago•0 comments