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.

How to use storytelling to fit inline assembly into Rust

https://www.ralfj.de/blog/2026/03/13/inline-asm.html
1•vinhnx•8m ago•0 comments

Reinventing Python's AsyncIO

https://blog.baro.dev/p/reinventing-pythons-asyncio
1•vinhnx•8m ago•0 comments

I've taught people how to use AI – here's what I've learned

https://www.theguardian.com/lifeandstyle/ng-interactive/2026/mar/10/teaching-ai-what-i-learned
1•coloradoave22•13m ago•0 comments

Show HN: Ffetch v5 – TypeScript-first fetch client

https://www.npmjs.com/package/@fetchkit/ffetch
1•gkoos•19m ago•0 comments

Everyone within humanities can contribute to the study of AI

https://www.universiteitleiden.nl/en/news/2025/09/stephan-raaijmakers-everyone-within-humanities-...
1•teleforce•22m ago•0 comments

Ex-Windows chief praises MacBook Neo, laments Surface defeat

https://www.windowscentral.com/microsoft/ex-windows-chief-calls-macbook-neo-a-paradigm-shifting-c...
3•walterbell•23m ago•0 comments

Riva: Local-first observability for AI agents

https://github.com/sarkar-ai-taken/riva
1•sarkarsaurabh27•28m ago•1 comments

From CIA to CEO, Spies Step Out of the Shadows and into the Boardroom

https://www.bloomberg.com/news/features/2026-03-13/former-cia-spies-launch-defense-tech-startups-...
2•jbegley•29m ago•0 comments

Musk Says xAI Must Be Rebuilt as Co-Founders Exit

https://www.wsj.com/tech/musk-says-xai-must-be-rebuilt-as-co-founders-exit-47770dfa
1•1vuio0pswjnm7•29m ago•0 comments

Hegseth on CNN: 'The sooner David Ellison takes over that network, the better'

https://thehill.com/policy/defense/5782562-hegeseth-criticizes-cnn-iran/
2•KnuthIsGod•30m ago•0 comments

xAI Co-Founder Toby Pohlen Is Latest Executive to Depart

https://www.bloomberg.com/news/articles/2026-02-27/xai-co-founder-toby-pohlen-is-latest-executive...
2•1vuio0pswjnm7•33m ago•1 comments

Lawyers in landmark social media addiction trial make final appeals to the jury

https://apnews.com/article/meta-instagram-facebook-trial-social-media-addiction-0e99c9ba615942172...
3•1vuio0pswjnm7•37m ago•0 comments

Nasdaq Proposes New "Fast Entry" Rule for the Nasdaq-100 Index

https://www.ashurst.com/en/insights/nasdaq-proposes-new-fast-entry-rule-for-the-nasdaq-100-index/
1•walterbell•39m ago•0 comments

Tethyr Cloud: Open Agent discovery, zero vendor lock-in (AIdeas Semi-finalist)

https://builder.aws.com
1•walmsles•41m ago•1 comments

Wool – A no-nonsense distributed Python runtime

https://github.com/wool-labs/wool
3•bzurak•41m ago•2 comments

Approximating π using Monte Carlo Simulation

https://graui.de/code/montePi/
1•joebig•46m ago•0 comments

Can a 100-Year-Old Mouse Save Disney?

https://www.honest-broker.com/p/can-a-100-year-old-mouse-save-disney
1•paulpauper•47m ago•0 comments

The Anthropic Institute

https://www.anthropic.com/news/the-anthropic-institute
1•gmays•48m ago•0 comments

I built AP score calculators for all 24 AP exams

https://apscorecalc.com/
1•smarthomeu•52m ago•1 comments

A Hard Reset, and What Comes Next

https://digg.com
2•magnust•53m ago•1 comments

Show HN: Free email finder for job seekers – find and contact hiring managers

https://www.dropzone.careers
1•qcarroll•55m ago•5 comments

exe-muttha-fukken-dev.exe.xyz [fan page]

https://exe-muttha-fukken-dev.exe.xyz
1•indigodaddy•55m ago•0 comments

The Church of Artificial Superintelligence

https://churchofai.webzum.com
2•billions•57m ago•0 comments

'Not built right the first time', Musk's xAI is starting over again, again

https://techcrunch.com/2026/03/13/not-built-right-the-first-time-musks-xai-is-starting-over-again...
2•jnord•1h ago•0 comments

The Most Accurately Predicted Genocide in History

https://thewalrus.ca/sudan-genocide/
10•garbawarb•1h ago•1 comments

Show HN: Lazy as a Service – Antigravity Extension

https://github.com/lakathekolla/laas
1•lakruoneX•1h ago•0 comments

I Fixed Green Screens [video]

https://www.youtube.com/watch?v=3Ploi723hg4
1•Khaine•1h ago•0 comments

The Middle Loop

https://annievella.com/posts/the-middle-loop/
1•vinhnx•1h ago•0 comments

Meta Delays Rollout of New A.I. Model After Performance Concerns

https://www.nytimes.com/2026/03/12/technology/meta-avocado-ai-model-delayed.html
2•gmays•1h ago•0 comments

Revenue can hide unprofitable freelance clients

2•SoloBossFounder•1h ago•2 comments