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.

A database is a store for global variables

https://www.seangoedecke.com/a-database-is-a-store-for-global-variables/
1•nettlin•1m ago•0 comments

How accurately did science fiction stories predict AI?

https://jamesyu.org/science-fiction-predictions-of-ai/
1•jamesjyu•2m ago•0 comments

Dynamic force sensing by bone delays aging and extends lifespan in mice

https://www.cell.com/cell-metabolism/fulltext/S1550-4131(26)00433-X
1•geox•4m ago•0 comments

Codetations: Rich, Granular, and Maintainable Annotations for Evolving Code

https://ztatlock.net/pubs/2026-uist-codetations/
1•matt_d•9m ago•0 comments

Stop Dirty Data Centres

https://stopdirtydatacentres.eu/
1•jruohonen•11m ago•0 comments

From Linux to Wikipedia: How Volunteers Build the Internet

https://thereader.mitpress.mit.edu/from-linux-to-wikipedia-how-volunteers-build-the-internet/
4•jonbaer•21m ago•0 comments

Servo Browser Engine Continues Broadening Web Compatibility, New Features

https://www.phoronix.com/news/Servo-0.7-Changes
1•qtqtqt•23m ago•1 comments

Personality traits are less stable than people think

https://www.nature.com/articles/s44271-026-00515-7
1•rohanm93•24m ago•0 comments

OpenSSH flaw lets attackers log into password-disabled Linux accounts

https://www.bleepingcomputer.com/news/security/openssh-flaw-lets-attackers-log-into-password-disa...
1•Skyfall106•26m ago•1 comments

Lilithbrown

1•HackerNewslilit•30m ago•0 comments

Lab-grown meat was supposed to save the world. What happened?

https://www.theguardian.com/news/ng-interactive/2026/nov/05/lab-grown-meat-save-the-world-food-re...
2•andsoitis•31m ago•0 comments

18 Years of GOG – Anniversary Showcase

https://www.gog.com/en/anniversary
2•SockThief•32m ago•0 comments

Bomb threats disrupt at least 25 flights across Europe, raising hybrid war fears

https://www.theguardian.com/world/2026/nov/05/bomb-threats-disrupt-flights-across-europe-and-rene...
3•andsoitis•37m ago•0 comments

US FAA says it will phase out floppy disks from air traffic control by 2028

https://www.reuters.com/world/us/us-faa-says-it-will-phase-out-floppy-disks-air-traffic-control-b...
1•ChrisArchitect•38m ago•1 comments

Apple Watch Live Rewind and Siri Recap: Features and Limitations

https://memova.ai/journal/apple-watch-live-rewind-siri-recap/
1•ViolaWang•42m ago•0 comments

Running MicroPython on the Allwinner T527's XuanTie E906 RISC-V Core

http://allthingsembedded.blogspot.com/2026/10/micropython-on-e906.html
1•tcmichals•43m ago•0 comments

Silicon Valley's Bad Night

https://www.theatlantic.com/technology/2026/11/americans-really-dont-like-data-centers-elections/...
16•ent101•44m ago•11 comments

AI and the Future of the Product Role

https://www.svpg.com/ai-and-the-future-of-the-product-role/
1•swolpers•44m ago•0 comments

Are We Cooked? CMU Faculty on AI, Careers and Learning [video]

https://www.youtube.com/watch?v=U2DDKdUvq6k
1•lmc•45m ago•0 comments

Meta Warns Midterm Backlash Threatens AI Data Centers

https://epyhia.com/meta-warns-midterm-backlash-threatens-ai-data-centers-goldman-sees-45
3•arbuge•47m ago•0 comments

Letterboxd in Negotiations to Be Acquired by New York Times Company

https://variety.com/2026/digital/news/letterboxd-negotiations-new-york-times-1236940881/
2•cdrnsf•47m ago•0 comments

Move fast and don't break (safety critical) things

https://bscholl.substack.com/p/move-fast-and-dont-break-safety-critical
3•gmays•54m ago•1 comments

Claude Code, but for SEO

https://twitter.com/brodieseo/status/2118299596292305356
11•brodieclark•56m ago•2 comments

Queen's University Belfast students find Neptune's elusive Trojan asteroids

https://phys.org/news/2026-11-queen-university-belfast-students-neptune.html
1•Skyfall106•57m ago•0 comments

Native macOS storage monitor with an interactive disk explorer

https://github.com/nglippa/storage-checker
1•busterbuilds•58m ago•0 comments

DApp's Numbers, with the Working Shown

https://github.com/globalmpc/rwa-kit-js
4•A_Jblessing•1h ago•0 comments

Nvidia CEO called in by lawmakers over ties to alleged Chinese spy

https://www.axios.com/2026/11/05/jensen-huang-nvidia-congress-china
2•DGAP•1h ago•1 comments

What does LoRA specialized Muon optimizer even mean?

https://zrt.wtf/do-lora-optimizers-matter/
1•polymorph1sm•1h ago•0 comments

Vaucanson's Digesting Duck

https://www.atlasobscura.com/places/canard-digerateur-de-vaucanson-vaucansons-digesting-duck
4•gregsadetsky•1h ago•0 comments

Pwn.college

https://pwn.college/
18•tripdout•1h ago•1 comments