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.

80-year-old woman with Alzheimer's took mushrooms and started speaking again

https://www.microdos.in/en/research/alzheimers-case-report-psilocybin-speech
1•LaurensDerisks•1m ago•0 comments

Ask HN: What model/AI platform do you use for the automatic translations?

1•cristaloleg•4m ago•0 comments

The Update Framework (TUF)

https://apis.io/providers/tuf/
1•ankitg12•4m ago•0 comments

Ask HN: Who knows someone at GitHub/NPM team?

2•punkpeye•8m ago•0 comments

'Complex numbers are not needed for quantum mechanics'

https://www.livescience.com/physics-mathematics/complex-numbers-are-not-needed-for-quantum-mechan...
2•bookofjoe•11m ago•0 comments

GPS Camerastamp Photo

https://play.google.com/store/apps/details?id=com.gpsmapcamera.timestamp.geotaglocation.video&hl=...
1•karlnelsone•12m ago•1 comments

Stardew Valley Mod Enables 3D and VR

https://www.rockpapershotgun.com/stardew-valley-enters-the-third-dimension-thanks-to-this-in-deve...
2•makaimc•13m ago•0 comments

Show World

https://zenodo.org/records/21294371
1•mbinatorom•13m ago•0 comments

Show HN: TTSC, TypeScript v7 ToolChain, plugin and codegraph reducing 90% tokens

https://github.com/samchon/ttsc
1•autobe•18m ago•0 comments

Email link phishing: why your email app should always show the URL

https://www.phpfreelance.co.uk/blog/email-link-phishing-your-app-should-always-show-the-url
1•vcdk•22m ago•0 comments

Mini Shai-Hulud: Where SLSA's Boundaries Fall

https://slsa.dev/blog/2026/05/mini-shai-hulud-what-slsa-can-and-cannot-do
1•ankitg12•23m ago•0 comments

TV Time is shutting down. The alternatives are buckling

https://reacham.app/demand-leaks/tv-time
1•neddinn•24m ago•1 comments

AB 2047: California's 3D Printer Surveillance Bill Advances

https://reclaimthenet.org/california-wants-a-watcher-inside-every-3d-printer
2•bilsbie•25m ago•0 comments

Modern environment designs overstimulate the brain, causing discomfort, stress

https://www.stir.ac.uk/news/2026/july-2026-news/looks-good-feels-bad-stirling-led-review-explains...
1•giuliomagnifico•26m ago•0 comments

EU Commission: addictive design Instagram and Facebook in breach of the DSA

https://ec.europa.eu/commission/presscorner/home/en
4•jeroenhd•26m ago•1 comments

Show HN: The Flint Programming Language

https://github.com/flint-lang/flintc
2•zweiler1•27m ago•0 comments

Meta risks $12B EU fine over addictive Instagram and Facebook feeds

https://www.theverge.com/policy/963872/meta-eu-addictive-design-200b-fine-risk-digital-services-a...
2•ilreb•27m ago•0 comments

Size Does Matter

https://nh3.dev/blog/05-bloat
1•cristaloleg•28m ago•0 comments

Cybersecurity Startup Publishes Infostealers to NPM

https://opensourcemalware.com/blog/cybersecurity-startup-publishes-infostealers-to-npm
2•ledoge•29m ago•0 comments

China tests sea-based rocket booster recovery system

https://www.reuters.com/science/china-successfully-tests-sea-based-rocket-booster-recovery-system...
2•olalonde•33m ago•0 comments

The Road to Iroh 1.0

https://www.iroh.computer/blog/the-road-to-iroh-1-0
1•pimterry•33m ago•0 comments

Show HN: Concepticon – manage your team's domain language like source code

https://thinkingtools.software/concepticon/
1•james_ross•33m ago•1 comments

Brown says AI make student brain no work good, teacher should help use it better

https://www.theregister.com/ai-and-ml/2026/07/09/brown-says-ai-make-class-dumb-teacher-must-help-...
1•pseudolus•34m ago•1 comments

Igloo Inc

https://www.igloo.inc/
2•treexs•36m ago•0 comments

20M data entries, a backtest engine, ML models, agents. Still haven't traded

https://demo.botlance.ai/
1•lamne•40m ago•0 comments

More Flock cameras cut down in Houston amid some privacy concerns

https://abc13.com/post/more-flock-cameras-cut-down-houston-amid-privacy-concerns/19471780/
2•pseudolus•40m ago•0 comments

Build Your Own Game Day

https://aws.amazon.com/blogs/architecture/build-your-own-game-day-to-support-operational-resilience/
1•ankitg12•45m ago•0 comments

Replit is the best coding agent

2•anitroves•45m ago•1 comments

EU-hosted analytics isn't the same as EU-owned analytics

https://plausible.io/blog/eu-hosted-analytics-vs-eu-owned-analytics
1•shivam-dev•46m ago•0 comments

Unified Memory, Explained: Why Mini PCs Can Run 70B Models a Big GPU Can't

https://vettedconsumer.com/unified-memory-explained-why-mini-pcs-can-run-70b-models-a-big-gpu-can...
1•ermantrout•46m ago•0 comments