frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Free A/B testing plugin for Matomo

https://github.com/nofrillsplugins/matomo-simple-ab-testing
1•Curiositry•2m ago•0 comments

Chinese astronauts are back on Earth after suspected 'space junk' strike left th

https://www.livescience.com/space/space-exploration/chinese-astronauts-are-back-on-earth-after-su...
1•bookmtn•3m ago•0 comments

A-29 Super Tucano Light Attack Aircraft Being Pitched as Drone Hunter

https://www.twz.com/air/a-29-super-tucano-light-attack-aircraft-being-pitched-as-drone-hunter
1•breve•3m ago•0 comments

Group and track health of GitHub repos you use

https://gitfitcheck.com
1•zendai•4m ago•1 comments

First Microprocessor – 50th Anniversary 2020

https://firstmicroprocessor.com/
1•rbanffy•5m ago•0 comments

The politics of purely client-side apps

https://pfrazee.leaflet.pub/3m5hwua4sh22v
1•todsacerdoti•6m ago•0 comments

What Happened to SF Homelessness? – By Scott Alexander

https://www.astralcodexten.com/p/what-happened-to-sf-homelessness
1•rbanffy•6m ago•0 comments

FPGA Based IBM-PC-XT

https://bit-hack.net/2025/11/10/fpga-based-ibm-pc-xt/
1•zdw•10m ago•0 comments

Decart Launches LSD-v2 – New Realtime Video Model

https://twitter.com/DecartAI/status/1989425986891583763
1•decartai•10m ago•1 comments

Clojure in the Browser

https://www.wedesoft.de/software/2025/11/12/scittle-clojure/
1•ibobev•11m ago•0 comments

Go proposal: Context-aware Dialer methods

https://antonz.org/accepted/net-dialer-context/
1•ibobev•12m ago•0 comments

How to Modulo on the C64 and Why You Need It

https://retrogamecoders.com/modulo-c64/
1•ibobev•12m ago•0 comments

I accidentally discovered cross-species microbiome colonization in my dog

https://lightcapai.medium.com/my-accidental-discovery-how-i-broke-my-dogs-identity-with-human-pro...
6•kinderpingui•13m ago•0 comments

Coding with Gemini: Cheerful, Cooperative, and Usually, Wrong

https://lauren.vortex.com/2025/11/14/coding-with-gemini
1•speckx•17m ago•0 comments

The do-gooder dilemma: to disclose or not to disclose

https://news.cornell.edu/stories/2025/11/do-gooder-dilemma-disclose-or-not-disclose
1•geox•19m ago•0 comments

Achieving True file IO safety in Python

https://github.com/rodmena-limited/DataShard
1•rodmena•20m ago•0 comments

Inspect This Snake

https://matthew.rayfield.world/goodies/inspect-this-snake/
2•jihadjihad•21m ago•0 comments

Spatial Noise-Canceling Technology That Rapidly Adapts to Diverse Noise

https://group.ntt/en/newsrelease/2025/11/13/251113a.html
1•HelloUsername•22m ago•0 comments

Awk Tutorial

https://blog.jpalardy.com/posts/awk-tutorial-part-1/
2•ssernikk•23m ago•1 comments

Epstein Estate Documents (By Zeteo)

https://journaliststudio.google.com/pinpoint/search?collection=2283eeed70befac7
1•massenpunkt•23m ago•0 comments

Oceanco delivers 111M superyacht Leviathan owned by Gabe Newell

https://www.boatinternational.com/yachts/news/oceanco-y722-superyacht-leviathan-delivery
2•HelloUsername•24m ago•0 comments

Manhattan Population Explorer

https://manpopex.us/
1•aragonite•24m ago•0 comments

Show HN: SuperCurate – Fast note search and curation

https://getsupercurate.com
1•superdocs1•25m ago•0 comments

I spent $900 on AI coding agents in 8 weeks

3•nthypes•28m ago•2 comments

ThunderMittens for Your ThunderKittens

https://hazyresearch.stanford.edu/blog/2024-11-28-tk-mlx
1•mpweiher•28m ago•0 comments

He Erased Memory in Mice. Then Thought About Erasing His Own

https://nautil.us/he-erased-memory-in-mice-then-thought-about-erasing-his-own-1247163/
1•rbanffy•29m ago•0 comments

Pass on Polymorphic Tables

https://zarar.dev/the-temptation-of-polymorphic-tables/
1•speckx•33m ago•0 comments

Show HN: Shelly Systems – Process Modeling as Code

https://systems.shellysys.com/demo
1•chrisshella2•34m ago•0 comments

Show HN: Zingle – an AI code reviewer for data teams (SQL/dbt/Airflow/Spark)

3•UvrajSB•35m ago•4 comments

There has to be a better way to make titanium

https://www.orcasciences.com/articles/there-has-to-be-a-better-way-to-make-titanium
3•paulpauper•36m ago•0 comments