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.

Yarn 6 Preview

https://yarn6.netlify.app/blog/2026-01-28-yarn-6-preview/
1•vidyesh•6s ago•0 comments

Guthman Musical Instrument Competition (2026 Finalists)

https://guthman.gatech.edu/2026-finalists
1•latexr•2m ago•0 comments

The myth of the free market (2020)

https://www.nationofchange.org/2020/01/24/the-myth-of-the-free-market/
1•robtherobber•3m ago•0 comments

DayFlow: A Full Calendar Component for React

https://dayflow-js.github.io/calendar/
1•vidyesh•4m ago•0 comments

The Ghidra Book, 2nd edition

https://nostarch.com/ghidra-book-2e
1•0x54MUR41•4m ago•0 comments

China bans hidden car door handles, which can trap people after crashes

https://www.npr.org/2026/02/03/nx-s1-5698224/china-electric-car-door-handles
1•geox•4m ago•0 comments

Awesome Codex Automations

https://github.com/onurkanbakirci/awesome-codex-automations
1•onurkanbkrc•7m ago•0 comments

UN Open Sourcre Week 2026

https://www.unopensource.org/
2•jruohonen•9m ago•0 comments

The SpaceX mega merger boosts the Musk trade

https://finance.yahoo.com/news/the-spacex-mega-merger-boosts-the-musk-trade-110026592.html
1•01-_-•10m ago•0 comments

We Are the Art; Brandon Sanderson's Keynote [video]

https://www.youtube.com/watch?v=mb3uK-_QkOo
2•simplegeek•10m ago•0 comments

From Human Ergonomics to Agent Ergonomics

https://wesmckinney.com/blog/agent-ergonomics/
1•haraball•13m ago•0 comments

A Quick Introduction to OxCaml

https://noelwelsh.com/posts/a-quick-introduction-to-oxcaml/
1•noelwelsh•14m ago•0 comments

Bypassing Kernel32.dll for Fun and Nonprofit

https://ziglang.org/devlog/2026/#2026-02-03
1•Sh4pe•14m ago•0 comments

Show HN: Rust Monorepo Analyzer v0.16.0 and v0.17.0 faster scans and better TUI

https://github.com/bumahkib7/rust-monorepo-analyzer
1•bumahkib7•16m ago•0 comments

Elon Musk Has Grand Plans for Data Centers in Space. Experts Are Skeptical

https://uk.pcmag.com/networking/162953/elon-musk-has-grand-plans-for-data-centers-in-space-expert...
2•ironyman•17m ago•0 comments

Why San Andreas Feels Bigger Than GTA 5 (Map Analysis)

https://claritypotion.com/2022/08/15/san-andreas-feels-bigger-gta-v/
1•luskira•19m ago•0 comments

Show HN: Drago – Your Personal Cloud Postal Software

https://www.drago.co.ke/
1•lomolo•19m ago•0 comments

Your Job Isn't Disappearing. It's Shrinking Around You in Real Time

https://newsletter.jantegze.com/p/your-job-isnt-disappearing-its-shrinking
2•hunglee2•23m ago•0 comments

Show HN: Store Listing Canvas – Real screenshots and marketing frames

1•chapiware•23m ago•0 comments

RIP – Ken Boak a.k.a. Monsonite

https://retrocomputingforum.com/t/rip-ken-boak-aka-monsonite/5536
2•DamonHD•24m ago•0 comments

Claude Code can generate image now

https://clawhub.ai/junaid1460/masonry-generate-image-and-video
3•junaid1460•25m ago•1 comments

EU rejects 'nonsense' censorship charge from Trump allies

https://www.lemonde.fr/en/international/article/2026/02/03/eu-rejects-nonsense-censorship-charge-...
5•robtherobber•27m ago•1 comments

Software Peer Review

https://ropensci.org/software-review/
1•jruohonen•28m ago•0 comments

Privacy score that proves your image has no hidden metadata

https://exif-cleaner.com/
2•cope123•31m ago•1 comments

Requests for Startups

https://www.ycombinator.com/rfs
1•taubek•32m ago•0 comments

Anthropic Claude Max $200/mo: They claim 99% uptime, I calculated 84% Loss: $780

https://gist.github.com/LEX8888/0caac27b96fa164e2a8ac57e9a5f2365
2•Nerios•37m ago•1 comments

Why a Russian Startup Is Putting Brain Chips in Pigeons

https://www.bloomberg.com/news/newsletters/2025-12-18/remote-controlled-pigeons-what-we-know-abou...
3•pretext•38m ago•2 comments

GitHub Ponders Kill Switch for Pull Requests to Stop AI Slop

https://www.theregister.com/2026/02/03/github_kill_switch_pull_requests_ai/
5•jruohonen•38m ago•2 comments

Melinda French Gates reacts to new details about Bill Gates in the Epstein files

https://www.npr.org/2026/02/03/nx-s1-5697080/melinda-french-gates-reacts-to-ex-husband-bill-gates...
2•Qem•41m ago•2 comments

A manual workflow to fix the "muffled" audio of AI music models

1•JoyTxis•41m ago•0 comments