frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

The 'manosphere' has already infiltrated the workplace. We're only just noticing

https://www.fastcompany.com/91523017/the-manosphere-has-already-infiltrated-the-workplace-were-on...
1•zczc•1m ago•0 comments

The Plot to Kidnap and Assassinate Me

https://www.youtube.com/watch?v=y8i-5907ky4
1•tcp_handshaker•1m ago•0 comments

Rent-a-Ruminant

https://www.rentaruminant.com/
1•bariumbitmap•2m ago•0 comments

Emergency First Responders Say Waymos Are Getting Worse

https://www.wired.com/story/emergency-first-responders-say-waymos-are-getting-worse/
1•tcp_handshaker•2m ago•0 comments

Why did I choose to run that marathon?

https://anushkakarmakar.substack.com/p/1-why-did-i-choose-to-run-that-marathon
1•thinkingkite•3m ago•0 comments

Acupuncture works for pain. Jury is out on everything else

https://www.economist.com/science-and-technology/2026/05/01/does-acupuncture-work
1•bookofjoe•6m ago•1 comments

Tired of high costs, some Americans are importing homes straight from China

https://www.cnn.com/2026/04/25/business/china-imports-americans-homebuilding-costs
1•JumpCrisscross•7m ago•0 comments

A Bill Aimed at Creating Homes Is Leaving Plots Empty Instead

https://www.wsj.com/real-estate/a-bill-aimed-at-creating-homes-is-leaving-plots-empty-instead-c25...
1•JumpCrisscross•7m ago•0 comments

Porting microgpt to Futhark, Part I

https://www.kmjn.org/notes/microgpt_futhark.html
1•fulafel•8m ago•0 comments

No Code Reviews by Default

https://www.raycast.com/blog/no-code-reviews-by-default
1•fagnerbrack•9m ago•0 comments

The College Admissions Chess Game Is More Complicated

https://www.wsj.com/us-news/education/college-admissions-yield-rate-2fb30f42
1•tcp_handshaker•9m ago•0 comments

Learn Algorithms for Interviews, Forget Them for Work

https://fagnerbrack.com/learn-algorithms-for-interviews-forget-them-for-work-c7dc5fe6cd3b
1•fagnerbrack•10m ago•0 comments

Refusal in Language Models Is Mediated by a Single Direction

https://arxiv.org/abs/2406.11717
2•fagnerbrack•10m ago•0 comments

Why is Britain putting America First?

https://vulpesetleo.substack.com/p/what-has-palantir-ever-done-for-us
3•foxandlion•15m ago•0 comments

What predicts Show HN front page success? I analyzed 73k recent ShowHN posts

https://wannalaunch.com/blog/show-hn-what-the-data-says
3•margotli•15m ago•6 comments

Watch Michael (2026) FullMovie Free Available Now Online Streamings [pdf]

https://ia601500.us.archive.org/26/items/michaeltv04/michaeltv04.pdf
2•fgfhf•17m ago•0 comments

The AI supply crunch is here

https://www.economist.com/leaders/2026/04/30/the-ai-supply-crunch-is-here
1•yakkomajuri•17m ago•0 comments

Sneak Preview: The World First Working Sanaii TCP/IP Stack for Amiga OS 1.3

https://mytube.madzel.de/w/5bm2KTwwBTVvhUT4HHWCcG
1•doener•17m ago•0 comments

Russia Poisons Wikipedia

https://www.bettedangerous.com/p/russia-poisons-wikipedia
3•exceptione•17m ago•0 comments

Codex Pets

https://developers.openai.com/codex/app/settings
2•tosh•18m ago•0 comments

Pentagon pursuing containerized 300kW+ laser weapons, Joint Laser Weapon System

https://www.tomshardware.com/tech-industry/pentagon-budget-documents-reveal-its-pursuing-containe...
2•rbanffy•24m ago•0 comments

Seattle-Tacoma International Airport (Sea) Visitor Pass Program

https://www.portseattle.org/page/sea-visitor-pass-program
1•blondie9x•24m ago•0 comments

Bluebuck - bringing it back from extinction

https://colossal.com/bluebuck/
1•andsoitis•26m ago•0 comments

The Fight to Save the Yachats Whale

https://www.theatlantic.com/science/2026/05/yachats-beached-whale-rescue/686978/
2•Brajeshwar•28m ago•0 comments

A more accurate .d.ts bundler for Rollup, powered by Microsoft/API-extractor

https://github.com/benhatsor/rollup-dts-bundler
2•barhatsor•32m ago•1 comments

On a mission to ensure our investments never lose value

https://arells.com
1•jeyakatsa•32m ago•0 comments

I Just Launched the AI Pledge for Humanity. Here's Why

https://scottsantens.substack.com/p/i-just-launched-the-ai-pledge-for
1•2noame•34m ago•3 comments

How can I close over variables in kdb/Q?

https://stackoverflow.com/questions/79240456/how-can-i-close-over-variables-in-kdb-q
1•tosh•34m ago•0 comments

David Bessis on AI destroying mathematics

https://davidbessis.substack.com/p/the-fall-of-the-theorem-economy
1•delis-thumbs-7e•37m ago•0 comments

Show HN: nfsdiag - a NFS diagnostic application

https://github.com/lsferreira42/nfsdiag
1•lsferreira42•37m ago•0 comments