frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

First Contact with America

https://novum.substack.com/p/first-contact-with-america
1•paulpauper•20s ago•0 comments

Claude's Constitutional Structure

https://thezvi.substack.com/p/claudes-constitutional-structure
1•paulpauper•43s ago•0 comments

Tim Cook Attends Screening of Propaganda for Authoritarian's Wife

https://pxlnv.com/linklog/cook-melania/
1•HotGarbage•1m ago•0 comments

Apple 'Honours' Martin Luther King While Working Against His Legacy

https://pxlnv.com/linklog/apple-mlk/
1•HotGarbage•2m ago•0 comments

Show HN: An Internationalization GitHub Action to Replace Crowdin with LLMs

https://github.com/i18n-actions/ai-i18n
1•cport1•6m ago•1 comments

Quaternion Algebras

https://jvoight.github.io/quat.html
1•teleforce•9m ago•0 comments

Show HN: Drum machine VST made with React/C++

https://okaysynthesizer.com
1•tabakd•12m ago•0 comments

Why IDE-Level AI Rules Will Always Lose to Model-Level Capabilities

https://lellansin.github.io/2026/01/27/Why-Cursor-Rules-Failed-and-Claude-Skill-Succeeded/
1•lellansin•12m ago•1 comments

Benchmarking the JDBC Bottleneck in Trino

https://www.starburst.io/blog/benchmarking-the-jdbc-bottleneck-in-trino/
1•abadid•12m ago•1 comments

Reactive Custom Web Scratchpad

https://qeditor.dev/
1•dpweb•17m ago•0 comments

Tell HN: Ask your AI, "What can you tell me that you know about me?" to see ...

1•marcuswestin•19m ago•0 comments

Show HN: A Local OS for LLMs. MIT License. Zero Hallucinations. (Not Crank)

https://github.com/merchantmoh-debug/Remember-Me-AI
2•MohskiBroskiAI•19m ago•0 comments

Japan's Debt Bomb Is About to Explode and Hit the US [video]

https://www.youtube.com/watch?v=CGoYHWT-0Tk
2•thelastgallon•23m ago•0 comments

Show HN: Soka – The Golden Rule Licensed Under the GNU General Public License

https://github.com/bfinan/soka
2•brendanfinan•26m ago•0 comments

Show HN: Bingsan – Apache Iceberg REST Catalog in Go (24k rps, multi-node)

https://teampaprika.github.io/bingsan/en/
1•youngbum•28m ago•0 comments

Ask HN: How do you maintain your health while coding?

2•anirudhviswa•30m ago•0 comments

January Ice

https://fnhipster.com/posts/january-ice
1•fnhipster•31m ago•0 comments

Last Call for Mass Market Paperbacks

https://www.publishersweekly.com/pw/by-topic/industry-news/publisher-news/article/99293-last-call...
5•wombatpm•43m ago•1 comments

Letter on Modern Counterculture

https://johnhiggs.substack.com/p/new-moon-letter-1
1•dom2•44m ago•0 comments

Check for AI Search Optimization on Your Webpage

https://old.reddit.com/r/ActorReviews/comments/1qo3p34/check_for_ai_search_optimization_on_your_w...
1•johncole•49m ago•0 comments

Blinkys – Digital Lifeforms Simulation

https://blinkys.entropicsystems.net/manual.html
1•snorbleck•50m ago•0 comments

Brain bran: The protective effect that fibre has on cognition

https://www.bbc.com/future/article/20260122-the-protective-effect-that-fibre-has-on-cognition
1•breve•52m ago•0 comments

Maia 200: The AI accelerator built for inference

https://blogs.microsoft.com/blog/2026/01/26/maia-200-the-ai-accelerator-built-for-inference/
2•boulos•54m ago•1 comments

Do you have to know every line of code your agent writes?

https://registerspill.thorstenball.com/p/joy-and-curiosity-71
1•alvivar•54m ago•0 comments

XCCache: A caching tool for Xcode projects, with SPM support

https://github.com/trinhngocthuyen/xccache
1•wahnfrieden•56m ago•0 comments

Bald eagle chick watch 2026: Jackie lays first egg

https://www.popsci.com/environment/bald-eagle-jackie-lays-first-egg-2026/
2•WaitWaitWha•1h ago•0 comments

Designing the Powerpuff Girls

https://animationobsessive.substack.com/p/designing-the-powerpuff-girls-512
1•ani_obsessive•1h ago•0 comments

Astrological CPU Scheduler

https://github.com/zampierilucas/scx_horoscope
2•Redoubts•1h ago•1 comments

Menopause linked to Alzheimer's-like brain changes

https://www.bbc.com/news/articles/c9qpp1g5ylvo
2•tagawa•1h ago•0 comments

Meta Is Being Sued over Whether WhatsApp Encrypts Your Messages

https://lifehacker.com/tech/meta-sued-whatsapp-encryption-claims
3•walterbell•1h ago•1 comments