frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Who Buys Custom Silicon?

https://www.youtube.com/watch?v=nf-4YGZp998
1•johncole•3m ago•0 comments

Satlas: Real-time space situational awareness

https://satlas.app/
1•jonbaer•3m ago•0 comments

6502 Emulator Runs 1 Instruction/S (Written in Markdown, Running in an LLM)

https://dunkels.com/adam/llm-6502-emulator/
1•adunk•6m ago•0 comments

Show HN: Peakedin – archiving LinkedIn's most unhinged posts as satire

https://peakedin.capyfind.com/
1•lirena00•7m ago•0 comments

The Genius of Spencer Pratt's Campaign – Part 1

https://twitter.com/AmericanDebunk/status/2056555463466967457
1•bilsbie•9m ago•0 comments

AI can chart a course to disaster faster than humans can notice

https://thebulletin.org/2026/05/ai-can-chart-a-course-to-disaster-faster-than-humans-can-notice/
2•SVI•10m ago•1 comments

NeuronGuard – A lock-free, pointerless spiking neural network in Rust

https://github.com/etoxin/NeuronGuard
1•etoxin•11m ago•0 comments

Adsense for LLMs

https://getadlib.live
1•AdetayoKale•14m ago•1 comments

Final Fantasy Creator Call AI-Generated Final Fantasy 6 Remake Video 'Amazing'

https://kotaku.com/final-fantasy-creator-says-viral-ai-generated-final-fantasy-6-remake-video-loo...
2•mchinen•16m ago•1 comments

The AI Era Is Creating a Bug Hunting Arms Race

https://www.wired.com/story/the-ai-era-is-creating-a-bug-hunting-arms-race/
1•joozio•17m ago•0 comments

Remaining Human in the Age of Algorithms

https://www.vaticannews.va/en/pope/news/2026-05/encyclical-magnifica-humanitas-editorial-torniell...
1•hkhn•17m ago•0 comments

Signal – Deleted, but Not Forgotten

https://sintonen.fi/advisories/signal-deleted-but-not-forgotten.txt
2•croes•18m ago•0 comments

Pope Leo Compares AI Threat to Biblical 'Tower of Babel'

https://www.wsj.com/world/pope-leo-ai-encyclical-c5e1af6c
2•alchemist1e9•20m ago•0 comments

EasyJet flight diverts to Rome over power bank in luggage

https://www.bbc.co.uk/news/articles/cg7p3pdl8m7o
1•petepete•21m ago•0 comments

AI now finds software vulnerabilities faster than they get patched

https://thenewguard.ai/features/faster-than-we-can-patch/
1•mattezell•23m ago•2 comments

Cate v1.0 is out: The Infinite canvas workspace for developers

https://github.com/0-AI-UG/cate
3•BlueBerry2001•23m ago•0 comments

Progression Without Progress

https://www.science.org/doi/10.1126/science.aeh8945
1•paraschopra•24m ago•0 comments

The Worst Mistake in the History of the Human Race [pdf]

https://web.cs.ucdavis.edu/~rogaway/classes/188/materials/Diamond-TheWorstMistakeInTheHistoryOfTh...
1•nhhvhy•24m ago•0 comments

Is there any offline meeting apps?

1•sanxxit•27m ago•0 comments

Show HN: How a Java program for finding a second Earth evolved over 23 years

https://www.planetensuche.de/en/entwicklerstory.html
1•Marfir•29m ago•0 comments

Three Years with Abstractionless C

https://replicated.wiki/blog/abc
1•gritzko•30m ago•0 comments

Brownshirt Princess

https://www.openbookpublishers.com/books/10.11647/obp.0003
2•jruohonen•31m ago•0 comments

Install Guide: KDE Neon for a professional digital painter workstation

https://www.davidrevoy.com/article1145/interim-install-guide-kde-neon-user-edition-for-a-professi...
1•majiy•32m ago•0 comments

Ask HN: Why do we snore? And what to do about it?

4•simonebrunozzi•42m ago•2 comments

Little Things Matter: The Lost Key of the Titanic

https://sm4.global-aero.com/articles/little-things-matter-the-lost-key-of-the-titanic/
1•teleforce•42m ago•0 comments

Why birth rates are falling everywhere all at once

https://www.ft.com/content/fba35eca-df3a-4ad6-b42d-eb08eb7c9ad3
2•Anon84•43m ago•1 comments

State of the Fin 2026-05-24

https://jellyfin.org/posts/state-of-the-fin-2026-05-24/
1•Rant423•44m ago•0 comments

The Bus from Izu

https://cinemasojourns.com/2026/05/24/the-bus-from-izu/
1•jjgreen•44m ago•0 comments

Poverty Changes Your Mind-Set (2018)

https://www.chicagobooth.edu/review/how-poverty-changes-your-mind-set
3•downbad_•45m ago•0 comments

The infamous 20 year old MySQL Bug #11472 has been fixed

https://bugs.mysql.com/bug.php?id=11472
1•theanonymousone•46m ago•0 comments