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.

Skill and UI kit to understand PRs faster

https://github.com/annanay25/explain-pr
1•annanay•22s ago•1 comments

She died at the San Diego border. A surveillance camera was in plain sight

https://www.technologyreview.com/2026/09/21/1144170/border-towers-surveillance-feature/
1•joozio•2m ago•0 comments

Move On – export your AI chats, file GDPR deletion, escalate if ignored

https://github.com/ur-grue/move-on
1•ur-grue•3m ago•0 comments

Show HN: I made ShipStreak to keep daily shipping gaps public

https://www.shipstreak.space/?via=hn
1•kljr_dev•3m ago•0 comments

Pakistan Orders Hospital Beds Clear for Mass Casualty Ahead of Protest

https://www.dropsitenews.com/p/pakistan-military-protest-imran-khan-sisters-detained-hospitals-beds
1•xbmcuser•7m ago•0 comments

Ask HN: Is Astra's writing starting to bother anyone else?

1•demibabs•7m ago•0 comments

QBZ (Qobuz desktop client for Linux) shutting down

https://qbz.lol/
1•herrherrmann•8m ago•0 comments

Show HN: Cheap and easy throwaway VPN server

https://a.l3x.in/blog/throwaway-vpn/
1•alexfortin•12m ago•0 comments

AI staff complain of mental toll over fears of threat to society

https://www.ft.com/content/60870960-f433-48ca-bc2c-708686a69ae7
2•bazzmt•14m ago•0 comments

Where Improvement Becomes the Problem

https://medium.com/@gurvinder372/article-22-where-improvement-becomes-the-problem-f2c1f3671eaa
1•gps372•15m ago•0 comments

Bk2204/talk-rust-in-Git: GitHub repository with SHA-256 commit IDs

https://github.com/bk2204/talk-rust-in-git
1•gotosun1•16m ago•0 comments

The Ultimate Reading List for Founders

https://grafista.io/blog/posts/the-ultimate-reading-list-for-founders
1•GiorgosGennaris•17m ago•0 comments

Complex KDA:Understanding and Enhancing the Expressivity of Kimi Delta Attention

https://arxiv.org/abs/2609.24797
1•jul8234•17m ago•0 comments

CP/M-80 2.2 Compilers

https://medium.com/@davidly_33504/cp-m-80-2-2-compilers-91958a8f1d58
1•rbanffy•18m ago•0 comments

How Claude is uplifting biomolecular modeling

https://www.anthropic.com/research/claude-uplifts-biomolecular-modeling
3•gmays•18m ago•0 comments

Beyond Context Windows: Evaluating Long-Term Memory for AI Agents

https://twitter.com/AgentMemoryL/status/2101312784688726331
1•IreneAI•24m ago•0 comments

A Summer of AI Optimization

https://lemire.me/blog/2026/09/22/a-summer-of-ai-optimization/
1•chmaynard•27m ago•0 comments

Be the first human to review your own code

https://abhinavr.me/posts/be-the-first-human-to-review-your-own-code/
1•avr5500•29m ago•0 comments

AMD has now officially joined the trillion-dollar club

https://companiesmarketcap.com/amd/marketcap/
2•AbuAssar•39m ago•1 comments

Axon Training Pod (ATP) VR for training police officers

https://www.axon.com/products/axon-training-pod
2•nephihaha•41m ago•0 comments

GeoRisk Trace – An evidence-gated agent that abstains on unsupported risk paths

https://github.com/ViviLoveAI/GeoRiskAgent
1•ViviLoveAI•41m ago•0 comments

When Pixels Collide – The Story of Reddit's Place (2017)

https://web.archive.org/web/20170409173246/http://sudoscript.com/reddit-place/
1•pmlnr•42m ago•0 comments

Speechify for Cartesia Users

https://chromewebstore.google.com/detail/lisen-free-read-aloud-wit/jjglbebakkebkafjilbfnejmgpdphbhp
1•theaugustin•43m ago•0 comments

Windows Exploitation Techniques: Dangling COM Object Registrations

https://projectzero.google/2026/09/windows-dangling-com.html
1•mfrw•43m ago•0 comments

What you can build with TypeSafe AI Jev

https://vercel.com/i/jev-use-cases
1•flashbrew•48m ago•0 comments

Deploy Drunk

https://korshakov.com/posts/deploy-drunk
1•denysvitali•50m ago•0 comments

Genesis – The Bible verse that far-right Americans misinterpret to defend Israel

https://www.trtworld.com/article/5081f8ade0cf
3•timeoperator•50m ago•0 comments

Keep Using Your Intel Mac in 2025?

https://aslihan.live/posts/why-intel-mac-in2025/
2•aslihana•53m ago•2 comments

AMD briefly joins the $1T club

https://www.theregister.com/systems/2026/09/21/perpetual-underdog-amd-nips-at-nvidias-heels-as-it...
3•mdp2021•56m ago•0 comments

Can gzip be a language model?

https://nathan.rs/posts/gzip-lm/
2•networked•56m ago•0 comments