frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Google Externalised the Cost of Renaming Gmail

https://nativerse-ventures.com/gmail-rename-cascade
1•Srinathprasanna•2m ago•0 comments

Show HNZ A native C/GTK Linux tray internet monitor to log ISP dropouts

https://github.com/lsferreira42/internet-indicator
1•lsferreira42•8m ago•0 comments

Show HN: A social feed with no strangers

https://www.grateful.so/
2•rpatni•10m ago•0 comments

A protective case for your glasses

https://thangs.com/designer/Gazzaladra/3d-model/Glasses%20case-1511373
1•dsego•11m ago•0 comments

Taking on CUDA with ROCm: 'One Step After Another'

https://www.eetimes.com/taking-on-cuda-with-rocm-one-step-after-another/
1•mindcrime•13m ago•0 comments

Vegetative Patients May Be More Aware Than We Knew

https://www.nytimes.com/2026/04/09/magazine/vegetative-states-conscious-aware.html
2•bookofjoe•15m ago•1 comments

Should capitalism be a choice?

https://cyrusradfar.com/thoughts/should-capitalism-be-a-choice
1•cyrusradfar•16m ago•0 comments

Major history podcasts are being faked

https://old.reddit.com/r/podcasting/comments/1sjgscs/major_history_podcasts_are_being_faked/
1•softwaredoug•18m ago•0 comments

Sam Altman's home targeted in second attack

https://sfstandard.com/2026/04/12/sam-altman-s-home-targeted-second-attack/
2•babelfish•21m ago•1 comments

Welcome to Agents Week

https://blog.cloudflare.com/welcome-to-agents-week/
1•cebert•22m ago•0 comments

They accidentally started a green screen revolution [video]

https://www.youtube.com/watch?v=Y3Dfw969itU
3•qingcharles•33m ago•0 comments

Show HN: Vapephone

https://wefumevapes.com/
1•wvlia5•35m ago•0 comments

KindleModding

https://kindlemodding.org/
1•fallinditch•37m ago•0 comments

Tech valuations are back to pre-AI boom levels

https://www.apollo.com/wealth/the-daily-spark/tech-valuations-back-to-pre-ai-boom-levels
33•akyuu•38m ago•4 comments

Linux Out-of-Bounds Access Fixed for Unprivileged Users with Crafted Certs

https://www.phoronix.com/news/Linux-OOB-Special-Certificate
2•moehm•49m ago•0 comments

Adventures in Slop: Can an AI Agent Generate Web Traffic?

https://www.tomdalling.com/blog/adventures-in-slop-can-an-ai-agent-generate-traffic/
1•ingve•50m ago•0 comments

Is Nvidia [stock] Worth 400% More? (video)

https://www.youtube.com/watch?v=xXyeTXLFfzs
1•hank808•54m ago•1 comments

Largest Curation of IAS

https://sstflix.ai
1•suschlebinger•54m ago•1 comments

Warning to drivers as Hyundai recalls 300k cars over life-threatening glitch

https://www.dailymail.co.uk/news/article-15726527/hyundai-cars-recall-seatbelt-anchor-detach.html
1•Bender•57m ago•1 comments

Git fixup is magic (and Magit is too)

https://arialdomartini.github.io/git-fixup
1•birdculture•58m ago•0 comments

Gephi – The Open Graph Visualization Platform

https://gephi.org
1•maxloh•1h ago•0 comments

India's frugal AI models are a blueprint for resource-strapped nations

https://restofworld.org/2026/india-frugal-ai-sarvam-krutrim-sovereign/
3•i7l•1h ago•0 comments

Oilfield Units: a Measurement System so Cursed it made me Change Career [video]

https://www.youtube.com/watch?v=sdWEGzWFcCc
1•CharlesW•1h ago•0 comments

At Coachella, Justin Bieber spends half an hour surfing YouTube

https://www.sfgate.com/sf-culture/article/bieber-coachella-22202157.php
2•turtlegrids•1h ago•0 comments

Vlookup Still Beats XLOOKUP in Poll of 1,975 Excel Users (53% vs. 36% vs. 12%)

https://accountsdraft.com/resources/xlookup-vs-vlookup-vs-index-match-excel-poll-results
2•Rob_Benson-May•1h ago•0 comments

Show HN: Transcribe, analyse, and chat with your voice conversations

https://github.com/nezhar/voicevault
1•nezhar•1h ago•0 comments

The Death of Character in Game Console Interfaces

https://vale.rocks/posts/game-console-interfaces
2•PaulHoule•1h ago•1 comments

Pi Coding Agent [video]

https://www.youtube.com/watch?v=O-96yYDPMOc
1•davidkunz•1h ago•0 comments

Ask HN: On autistic spectrum, best way to live?

9•beaunative•1h ago•0 comments

How the CIA Forgot the Art of Spying

https://www.politico.com/magazine/story/2017/03/cia-art-spying-espionage-spies-military-terrorism...
8•cwwc•1h ago•0 comments