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.

Show HN: Grid Data Explorer

https://open-energy-transition.github.io/Awesome-Electrical-Grid-Mapping/
1•ly0n•2m ago•0 comments

Aisee.live

1•ccbakla•4m ago•0 comments

Agent Communication Context Protocol (ACCP)- Proposal

https://github.com/RupertCloud/Agentmail/blob/main/docs/accp/SPEC.md
1•amonte•5m ago•0 comments

Development Statistics for the 7.2 Kernel

https://lwn.net/Articles/1088776/
1•pykello•6m ago•0 comments

Show HN: Memo – a local-first scratchpad for developers who live in the terminal

https://blackstardesigns.github.io/memo/
1•virtualbrain•7m ago•0 comments

Three handy tools for separating pseudoscience from the real deal [video]

https://aeon.co/videos/three-handy-tools-for-separating-pseudoscience-from-the-real-deal
1•andsoitis•9m ago•0 comments

Twitter, Bluesky and OnlyFans

https://bsky.app/profile/siridahl.com/post/3mshzivspwk22
1•Arnt•11m ago•0 comments

Climate change is strengthening El Niño, coral records suggest

https://www.science.org/content/article/climate-change-strengthening-el-ni%C3%B1o-coral-records-s...
3•shymaple•12m ago•0 comments

Reflecting on Progress Against HIV

https://www.hiv.gov/blog/americas-250th-reflecting-on-progress-against-hiv
1•andsoitis•14m ago•0 comments

Show HN: Use Spot Instance Fearlessly

https://github.com/softcane/spot-vortex-agent
1•pradeep1177•16m ago•0 comments

Show HN: Beckon, distinct sounds for what your AI coding agent needs

https://github.com/Isa1asN/beckon
2•esayas_beshah•19m ago•0 comments

The Fountain of Youth: A Tale of Parabiosis, Stem Cells, and Rejuvenation

https://pmc.ncbi.nlm.nih.gov/articles/PMC5662775/
1•andsoitis•24m ago•0 comments

Q-CTRL Achieves GPS-Free Quantum Gravimetric Navigation Demo in Field Trial

https://q-ctrl.com/blog/q-ctrl-achieves-worlds-first-gps-free-quantum-gravimetric-navigation-demo...
1•davedx•24m ago•1 comments

Show HN: AI Game Playtester

https://ziva.sh/full-game-tester
1•OsrsNeedsf2P•24m ago•0 comments

Macro Memo: Regime Change

https://www.citriniresearch.com/p/macro-memo-regime-change
1•stareatgoats•25m ago•0 comments

A Historic Settlement over Social Media Addiction [video]

https://www.youtube.com/watch?v=oJxIYlfvVsY
1•handfuloflight•25m ago•0 comments

Hot or Not: What Makes Some Places Naturally High in Radioactivity

https://science.howstuffworks.com/environmental/earth/geology/hot-not-what-makes-some-places-natu...
1•leonidasrup•27m ago•0 comments

That's a Lot of YAML

https://noyaml.com/
1•hisamafahri•27m ago•0 comments

Show HN: Gaussian Blue Noise for image stippling (Colab demo)

https://colab.research.google.com/drive/1W15QE8Boj0-Ld4Dx3N2tLqwfV0qw0ZZB?usp=sharing
1•adec314•28m ago•0 comments

Fuzzy terminal manager to pick, configure, and run local GGUF models

https://github.com/skorotkiewicz/run-model
1•modinfo•30m ago•0 comments

A startup claims it's found a drug to make your blood young

https://www.technologyreview.com/2026/08/27/1143037/startup-claims-its-found-a-drug-to-make-your-...
1•joozio•30m ago•1 comments

Tips for giving S-tier demos

https://newsletter.posthog.com/p/how-to-demo
1•fagnerbrack•30m ago•0 comments

Grimm's Law

https://en.wikipedia.org/wiki/Grimm%27s_law
1•ogogmad•34m ago•0 comments

Open-source vacuum robot cleaner

https://github.com/makerspet/oomwoo
2•jinqueeny•39m ago•0 comments

Agent Swarms Are a Distributed Systems Problem

https://www.trychroma.com/engineering/transactions
1•handfuloflight•41m ago•0 comments

Norway's King Harald dies aged 89

https://www.bbc.com/news/articles/clykn5p5172o
4•punnerud•42m ago•0 comments

Game over for Unauthorized AI Performances: The Sag-Aftra Video Game Strike

https://sites.suffolk.edu/jhtl/2025/10/30/game-over-for-unauthorized-ai-performances-the-sag-aftr...
1•Bluestein•42m ago•0 comments

America's Never-Ending Battle Against Flesh-Eating Worms

https://www.theatlantic.com/science/archive/2020/05/flesh-eating-worms-disease-containment-americ...
2•Bluestein•45m ago•1 comments

Show HN: KIIP Study – exam practice for Korea's immigration test (16 languages)

https://www.kiipstudy.com
1•sangkwun•45m ago•1 comments

Show HN: FnScribe – Open-source, offline dictation for macOS

https://github.com/AlgorithmicResearchGroup/fnscribe
3•modagent•46m ago•2 comments