frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Digital microwaves show an example of good UI doing what you wanted

https://utcc.utoronto.ca/~cks/space/blog/tech/MicrowaveGoodUIBehavior
1•leephillips•49s ago•0 comments

Monitor New NYC Mayor – Monitormamdani.com

https://monitormamdani.com
1•bolcoto•1m ago•0 comments

Trump Moves to Ban Investors from Buying Single-Family Homes

https://www.wsj.com/economy/housing/trump-moves-to-ban-investors-from-buying-single-family-homes-...
1•JumpCrisscross•1m ago•0 comments

Intel's 'Panther Lake' Core Ultra Laptop Chips Are Ready for Prime Time

https://www.pcmag.com/news/intel-panther-lake-core-ultra-laptop-chips-details-ces-2026
1•taubek•2m ago•0 comments

Give it Up, Turn it Loose

https://thinkhuman.com/give-it-up-turn-it-loose/
1•jamesgill•3m ago•0 comments

Triton Extensions: a framework for developing and building compiler extensions

https://github.com/triton-lang/triton-ext
1•matt_d•3m ago•0 comments

Seized by US: why so much interest in a rusty tanker in the Atlantic?

https://www.theguardian.com/world/2026/jan/07/marinera-seized-tanker-atlantic-us-uk-russia
1•n1b0m•4m ago•0 comments

Show HN: I built a tool to stop pretending I understood research papers

https://papersplain.com
1•jjoe•4m ago•0 comments

The Psychology of Stranger Things

https://allaboutpsychology.substack.com/p/the-psychology-of-stranger-things
1•rendx•4m ago•0 comments

Show HN: FightHOAFines – An AI agent that reads bylaws to dispute HOA violations

https://fighthoafines.com/
1•todaycompanies•6m ago•1 comments

The Giant Hoax of Shadow of the Colossus [video]

https://www.youtube.com/watch?v=NvGZLMUx7AM
1•crtasm•8m ago•0 comments

OpenCore Legacy Patcher – Experience macOS just like before

https://github.com/dortania/OpenCore-Legacy-Patcher
1•petethomas•8m ago•0 comments

NPM to Implement Staged Publishing After Turbulent Shift Off Classic Tokens

https://socket.dev/blog/npm-to-implement-staged-publishing
1•feross•9m ago•0 comments

WikiFlix

https://wikiflix.toolforge.org/#/
1•Tomte•9m ago•0 comments

Fun with Algebraic Effects – From Toy Examples to Hardcaml Simulations

https://blog.janestreet.com/fun-with-algebraic-effects-hardcaml/
1•i_don_t_know•10m ago•0 comments

Show HN: Clean normalised US equity fundamentals via API (free tier available)

https://finqual.app
2•myztika•12m ago•1 comments

Solving a snaky math problem with Mathematica

https://leancrew.com/all-this/2025/12/solving-a-snaky-math-problem-with-mathematica/
2•surprisetalk•13m ago•0 comments

Single Sign on for Furries

https://cendyne.dev/posts/2025-08-15-single-sign-on-for-furries.html
6•surprisetalk•13m ago•2 comments

Claude Opus 4.5 disappears suddenly from GitHub Copilot

https://github.com/orgs/community/discussions/181266
5•tantona•13m ago•1 comments

FAA signs radar deals to drag US air traffic control out of the 1980s

https://www.theregister.com/2026/01/07/faa_radar_atc_deals/
1•holysoles•14m ago•0 comments

Hardening eBPF for Runtime Security: Lessons from Datadog Workload Protection

https://www.datadoghq.com/blog/engineering/ebpf-workload-protection-lessons/
1•tanelpoder•15m ago•0 comments

DeepSeek-R1 paper updated from 22 pages to 86 with additional details

https://old.reddit.com/r/LocalLLaMA/comments/1q6c9wc/deepseekr1s_paper_was_updated_2_days_ago/
2•ksymph•16m ago•1 comments

Reflections on the Caplan-Bruenig Poverty Debate

https://www.betonit.ai/p/reflections-on-the-caplan-bruenig
1•paulpauper•16m ago•0 comments

Show HN: KyubiSweep – Fast, local secret scanner written in Go (visual reports)

https://github.com/tanmayshahane/kyubisweep
1•tanmay_shahane•17m ago•1 comments

Watch me run malware from NPM [video]

https://www.youtube.com/watch?v=GqnFNNcycxQ
1•todsacerdoti•19m ago•0 comments

Getting started with Claude for software development

https://steveklabnik.com/writing/getting-started-with-claude-for-software-development/
1•steveklabnik•20m ago•0 comments

NotepadNext – Cross-platform reimplementation of Notepad++

https://github.com/dail8859/NotepadNext
1•ethanpil•20m ago•0 comments

Kafka Inc

https://libertiesjournal.com/online-articles/kafkainc/
1•Caiero•21m ago•0 comments

FlashInfer-Bench: Building the Virtuous Cycle for AI-Driven LLM Systems

https://arxiv.org/abs/2601.00227
1•matt_d•23m ago•0 comments

A modular marketing command center built with autonomous workflows

https://flippa.com/12205760-vect-ai-is-an-autonomous-marketing-command-center-where-ai-agents-pla...
3•WoWSaaS•23m ago•0 comments