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.

Show HN: Tickstem – Python SDK for cron, uptime and heartbeat monitoring

https://github.com/tickstem/python
1•m_barsukou•2m ago•0 comments

SpaceX and Anthropic, xAI's Two Companies, Elon Musk and SpaceXAI's Future

https://stratechery.com/2026/spacex-and-anthropic-xais-two-companies-elon-musk-and-spacexais-future/
1•swolpers•8m ago•0 comments

Modern Lab Experiments

https://wanda.fiu.edu/boeglinw/courses/Modern_lab_manual3/index.html
2•nill0•18m ago•0 comments

Privatemode.ai – AI provider with confidential computing

https://www.privatemode.ai/
2•pelzatessa•18m ago•1 comments

Lil Snack

https://www.lilsnack.com/
2•delian66•20m ago•0 comments

Too Much Type

https://toomuchtype.com/
3•marksully•20m ago•0 comments

Panel on Language Design (PG, John Maeda, Jonathan Rees, G. Steele) (2001) [video]

https://www.youtube.com/watch?v=agw-wlHGi0E
2•kerim-ca•24m ago•1 comments

Appendix: Mental Health for the Engineering Student

https://ariadacapo.net/articles/2026-05-10-appendix-a7-mental-health-for-the-engineering-student/
2•2DcAf•28m ago•0 comments

David Weinberger interviews Paul Graham about 'Taste for Makers' (2006) [video]

https://www.youtube.com/watch?v=s2DkhL_Bypo
2•kerim-ca•31m ago•1 comments

2.21B Won Frozen in Virtual Currency Shutdowns

https://www.chosun.com/english/market-money-en/2026/05/12/A24FVGP2G5HD3CCDX23UHRWFQY/
3•01-_-•33m ago•0 comments

Unitree GD01: China's $537k rideable transformer robot is now in production

https://gagadget.com/en/709729-unitree-gd01-chinas-537k-rideable-transformer-robot-is-now-in-prod...
7•rguiscard•38m ago•0 comments

I'm Making Strandfall, a Solarpunk Orienteering Larp

https://mssv.net/2026/04/29/im-making-strandfall-a-solarpunk-orienteering-larp/
2•adrianhon•39m ago•0 comments

Canva's Magic Layers AI Changed "Palestine" to "Ukraine" in User Designs

https://gizmodo.com/canva-admits-its-ai-tool-removed-palestine-from-designs-apologizes-for-any-di...
7•lebowska•43m ago•0 comments

Texas Attor­ney Gen­er­al Sues Net­flix for Spy­ing on Kids and Con­sumers

https://www.texasattorneygeneral.gov/news/releases/attorney-general-ken-paxton-sues-netflix-spyin...
2•frb•48m ago•0 comments

Teen Boys and Young Men Are Injecting Peptides in Search of Perfection

https://www.wsj.com/health/wellness/boys-peptides-stacks-looksmaxxing-trevor-larcom-835e58cd
4•petethomas•50m ago•1 comments

Ducklake-SDK: Use DuckLake from Python or Rust without going through DuckDB

https://github.com/borchero/ducklake-sdk
2•borchero•54m ago•0 comments

Learning Software Architecture

https://matklad.github.io/2026/05/12/software-architecture.html
43•surprisetalk•57m ago•0 comments

Face ID Search

https://www.faceidsearch.com/en
2•bellamoon544•1h ago•0 comments

URLSession to Electrons: how networking works under the hood

https://blog.jacobstechtavern.com/p/urlsession-to-electrons
2•jakey_bakey•1h ago•0 comments

Show HN: Affordable Survey Software

https://www.survser.com/
3•pkundr•1h ago•0 comments

DuckDB/DuckLake multi-user in the browser with Read/Write and WebSocket notify

https://ducklake-with-vgi.query-farm.services/
3•rustyconover•1h ago•0 comments

TanStack NPM Packages Compromised in Ongoing Mini Shai-Hulud Supply-Chain Attack

https://socket.dev/blog/tanstack-npm-packages-compromised-mini-shai-hulud-supply-chain-attack
2•croes•1h ago•1 comments

Why should you have a data room? A stage-by-stage guide

https://vastpoint.substack.com/p/eng-why-should-you-have-a-data-room
2•kaku05ac•1h ago•0 comments

Lies, damned lies, and Elastic's benchmarks

https://www.gouthamve.dev/lies-damned-lies-and-elastics-benchmarks/
3•shutty•1h ago•0 comments

Loki Patera

https://en.wikipedia.org/wiki/Loki_Patera
2•DanielKehoe•1h ago•0 comments

SIEM and SoC: A Guide for Security Leaders in 2026

https://threatcrush.com/blog/siem-and-soc
2•buffer_overlord•1h ago•0 comments

EU Cloud Comparison Matrix

https://eualternative.eu/eu-cloud-comparison/
9•sparkling•1h ago•0 comments

Show HN: A benchmark where LLMs make memes from current news

https://memebench.net
4•max-azendorf•1h ago•1 comments

HDMI 2.1 Display Stream Compression (DSC) Ready for Amdgpu Linux Driver

https://www.phoronix.com/news/HDMI-2.1-DSC-AMDGPU-FRL
13•WithinReason•1h ago•0 comments

GitLab CI Functions

https://docs.gitlab.com/ci/functions/
3•yla92•1h ago•0 comments