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.

When science communicators spread misinformation

https://sunshowers.io/posts/science-communicator-misinformation/
1•hasheddan•1m ago•0 comments

Anna Groza and Bruce Dang – 0-Click Remote Exploits in an AI World [video]

https://www.youtube.com/watch?v=11EoBiBh5QY
1•beachhead•5m ago•0 comments

Bare Metal Qwen

https://github.com/alesha-pro/llm.asm/tree/main/02-baremetal-os
1•miohtama•6m ago•0 comments

How do you make friends?

1•ViktorPetrov•8m ago•0 comments

Tell HN: Stop using MLX, start telling your agent to write custom kernels

1•thesnarkitecht•8m ago•0 comments

Load balancing across AI subscriptions

1•kevin1chun•9m ago•0 comments

Deploying a payphone on a tiny Pacific Island for fun and zero profit

https://nickvsnetworking.com/deploying-a-payphone-on-a-tiny-pacific-island-for-fun-and-zero-profit/
1•qzervaas•9m ago•0 comments

Canaries in the Coal Mine? Six Facts about the Recent Employment Effects of AI

https://digitaleconomy.stanford.edu/publication/canaries-in-the-coal-mine-six-facts-about-the-rec...
1•softwaredoug•11m ago•0 comments

Voyager 1 Stops Sending Data, Reaches One Light-Day from Earth

https://hackaday.com/2026/11/25/voyager-1-stops-sending-data-reaches-one-light-day-from-earth/
1•CJason•12m ago•0 comments

Aftershock: Yesterday's Linux Kernel Bugs, Tomorrow's Android Exploits

https://arxiv.org/abs/2611.31547
1•beachhead•15m ago•0 comments

I'm a Therapist. I Wish These Patients Would Not Cut Off Their Family Members

https://www.nytimes.com/2026/11/25/opinion/politics-family-midterms-voting-holidays.html
1•vinni2•15m ago•0 comments

ML compiler eng, your job is NP-hard

https://ianbarber.blog/2026/11/24/ml-compiler-eng-your-job-is-np-hard/
1•gmays•15m ago•0 comments

Fail2Install: Turning System Limits into Pre-Auth RCE in Drupal

https://www.slcyber.io/research/fail2install-system-limits-pre-auth-rce-drupal
1•acossta•17m ago•0 comments

Playing with CSS Layers

https://kittygiraudel.com/2026/11/01/playing-with-css-layers/
1•vladde•17m ago•0 comments

Show HN: HN topics bar chart race,I use HTML-in-Canvas (need Chrome 157)

https://iantpe.github.io/hn-topic-race/?cat=langHist
3•iantpe•20m ago•1 comments

Show HN: I made a little gratitude wall for Thanksgiving

https://www.bravoboard.xyz/board/069c3cd2ef614c8397b5bfe250c626b2/
2•tumidpandora•22m ago•0 comments

Show HN: Anvil has won pricing among AI web builders plan starting at $5

https://www.tryanvil.dev/
2•Alumu•24m ago•0 comments

Metamath Proof Explorer Home Page

https://us.metamath.org/mpeuni/mmset.html
3•antiquark•24m ago•0 comments

Anchor – a habit app that doesn't reset your progress when you miss a day

https://getanchor.help/
1•eleventech•25m ago•0 comments

Gordon Ramsay revealed as new Top Gear host

https://www.rte.ie/entertainment/2026/1125/1600666-gordon-ramsay-revealed-as-new-top-gear-host/
1•FinnLobsien•28m ago•0 comments

Happily Ever After?

https://cinemasojourns.com/2026/11/25/happily-ever-after/
1•jjgreen•31m ago•0 comments

Paper Folding Puzzles: Can Multimodal LLMs Perform Spatial Reasoning?

https://ojs.aaai.org/index.php/AAAI/article/view/38364/42326
1•bryanrasmussen•32m ago•0 comments

Much shorter version of GRH Lean verification from Anthropic

https://github.com/lsernau/grh-lean
33•nb-t•33m ago•8 comments

Defang Security Incident

https://defang.io/blog/post/security-incident-november-2026/
19•gregsadetsky•34m ago•2 comments

A Man in Denver Has Been Trapping Waymo Vehicles

https://www.nytimes.com/2026/11/25/us/waymo-denver-egg-throwing.html
2•donohoe•35m ago•0 comments

First publicly-documented wartime interception of a jet-powered drone by laser

https://twitter.com/bayraktar_1love/status/2125619358453911742
4•nomilk•36m ago•1 comments

Prof. Sam Lawler: FUCK FUCK FUCK FUCK FUCK FUCK FUCK

https://mastodon.social/@sundogplanets/117678381079935502
38•marvinborner•37m ago•10 comments

Peco customers will see electric supply rate increase because of data centers

https://www.inquirer.com/business/energy/peco-electricity-rate-increase-data-centers-20261123.html
1•bkandel•40m ago•1 comments

Huang vs. Trump Lol

https://discuss.google.dev/t/trusted-automation-with-google-antigravity-scaling-secure-finance-in...
1•gutwrbetecg•41m ago•0 comments

Ask HN: Nobel Prizes for OpenAI and Anthropic: When?

1•LinaLauneBaer•48m ago•2 comments