frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

JPMorgan says Europe has its own AI play

https://www.axios.com/2025/11/21/ai-defense-stocks-europe
1•doener•1m ago•0 comments

Waymo cars flock to quiet Palo Alto street

https://www.mv-voice.com/technology/2025/11/21/nobody-asked-for-this-waymo-cars-flock-to-quiet-pa...
1•bmurray7jhu•1m ago•0 comments

Linux Application Development by Example

https://www.oreilly.com/library/view/linux-application-development/9780135325612/
1•teleforce•3m ago•0 comments

OpenAI Demo'd Fixing Issue #2472 Live. It's Still Open

https://blog.tymscar.com/posts/openaiunmergeddemo/
1•tymscar•4m ago•0 comments

The Memory OS

https://pickle.com/memory/
2•Anon84•4m ago•0 comments

Show HN:Nkv – a tiny key-value store with pub/sub and keyspaces for shared state

https://github.com/nkval/nkv
1•uncle_decart•6m ago•1 comments

Downsampling: Largest-Triangle-Three-Buckets and the Fourier Transform

https://daniel.mitterdorfer.name/posts/2024-01-30-downsampling-lttb-and-fft/
1•wonger_•6m ago•0 comments

Treasury, IRS provide guidance for individuals who received tips or overtime

https://www.irs.gov/newsroom/treasury-irs-provide-guidance-for-individuals-who-received-tips-or-o...
1•standardUser•6m ago•0 comments

Private Equity's New Venture: Youth Sports

https://jacobin.com/2025/11/youth-sports-hockey-private-equity
6•wahnfrieden•8m ago•1 comments

CrowdStrike catches insider feeding information to hackers

https://www.bleepingcomputer.com/news/security/crowdstrike-catches-insider-feeding-information-to...
2•el_duderino•8m ago•0 comments

Trump's support for pro-AI proposal fuels Maga backlash

https://www.ft.com/content/e087e732-5f71-4db3-b613-830f3cee313d
1•zerosizedweasle•10m ago•1 comments

Mr Roberts Goes to Hollywood, Part 1: A Digital Anvil

https://www.filfre.net/2025/11/mr-roberts-goes-to-hollywood-part-1-a-digital-anvil/
1•doppp•10m ago•0 comments

Learning to Rank with Bandit

https://mixpeek.com/blog/learning-to-rank-with-bandit-multimodal-search-guide
1•Beefin•11m ago•0 comments

Business contracts are transport agnostic

https://jensrantil.github.io/posts/simple-contract-testing/
2•mooreds•11m ago•0 comments

A Brief, Incomplete, and Mostly Wrong History of Programming Languages

http://james-iry.blogspot.com/2009/05/brief-incomplete-and-mostly-wrong.html
2•mooreds•12m ago•0 comments

Hot Takes to Fight About at Thanksgiving

https://www.nytimes.com/interactive/2025/11/20/dining/thanksgiving-hot-takes.html
2•whack•12m ago•1 comments

Google begins showing ads in AI Mode (AI answers)

https://www.bleepingcomputer.com/news/artificial-intelligence/google-begins-showing-ads-in-ai-mod...
4•thm•12m ago•2 comments

Show HN: DeepSeek-OCR with MPS and CPU Support

https://twitter.com/dogacel0/status/1991629702575079783
2•dogacel•12m ago•0 comments

Show HN: OCR Arena – A playground for OCR models

https://www.ocrarena.ai/battle
2•kbyatnal•16m ago•0 comments

Show HN: Markov Models and Dynamical Systems – Software and Book

https://gitlab.com/fraserphysics/hmmds
2•fraserphysics•19m ago•0 comments

You Can Now Make PS2 Games in JavaScript

https://jslegenddev.substack.com/p/you-can-now-make-ps2-games-in-javascript
2•tosh•19m ago•0 comments

Rise and fall of opium-fueled Sassoon dynasty, 'Rothschilds of the East' (2022)

https://www.timesofisrael.com/the-rise-and-fall-of-the-opium-fueled-sassoon-dynasty-the-rothschil...
2•walterbell•20m ago•0 comments

Frankenstein Is Not Your AI Metaphor (At Least, Not Like That)

https://every.to/working-overtime/frankenstein-is-not-your-ai-metaphor-at-least-not-like-that
1•everyeleanor•20m ago•0 comments

There Is Only One AI Company

https://www.wired.com/story/ai-industry-monopoly-nvidia-microsoft-google/
2•NegativeLatency•20m ago•0 comments

Kagi News AI thinks Ukraine has NATO membership

https://news.kagi.com:443/world/2025112021/trump-pushes-28-point-peace-plan-for-ukraine?shared=1
2•sciencejerk•20m ago•1 comments

Good riddance to Auth0 and social logins

https://bitbytebit.substack.com/p/good-riddance-to-social-logins-and
3•recroad•22m ago•0 comments

Evidence of mass brutality accompanied collapse of first pan-European culture

https://www.science.org/content/article/headless-bodies-hint-why-europe-s-first-farmers-vanished
2•stmw•23m ago•0 comments

Assessment of energy efficiency of battery electric buses in cold regions

https://www.sciencedirect.com/science/article/pii/S136192092500344X
1•PaulHoule•23m ago•0 comments

EU prosecutors dismiss probe into chief of Europe's most powerful party

https://www.ftm.eu/articles/eu-prosecutors-drop-investigation-epp-manfred-weber
1•robtherobber•24m ago•0 comments

Building DSL in Go: From Simple Rules to Complex Workflows

https://skoredin.pro/blog/golang/building-dsl-go
1•ibobev•24m ago•0 comments