frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

I built an AI tool that designs kitchen layouts

https://aikitchendesign.io/
1•cby821555203•9m ago•0 comments

Show HN: AutoTable – One-Click Spreadsheet Cleaner Built with Gemini

https://www.auto-table.com/
1•voxdroid•10m ago•0 comments

Pathways to a fair technological future [pdf]

https://storage02.forbrukerradet.no/media/2026/02/breaking-free-pathways-to-a-fair-technological-...
2•jmartinpetersen•15m ago•0 comments

How SiriusXM Ignored Pandora's Innovation and Is Killing Itself

https://pandora-sxm-news.blogspot.com/2026/01/how-siriusxm-ignored-pandoras.html
3•Aloha•16m ago•0 comments

Update: Making VeriContext Enforce Citations Across Sub-Agents

https://github.com/amsminn/vericontext/blob/main/CHANGELOG.md
1•amsminn•18m ago•1 comments

Switch to Claude Without Starting Over

https://claude.com/import-memory
2•doener•18m ago•0 comments

Show HN: React-Kino – Cinematic scroll storytelling for React (1KB core)

https://github.com/btahir/react-kino
1•bilater•18m ago•0 comments

10-202: Introduction to Modern AI (CMU)

https://modernaicourse.org
3•vismit2000•19m ago•0 comments

You're Not Addicted to Porn. You're Addicted to Staying Smaller Than You Could B

https://cpleveragingai.substack.com/p/youre-not-addicted-to-porn
2•cp18101985•20m ago•0 comments

Worlds First AI-OS

https://github.com/siresorose/ai-os
1•siresorose•20m ago•0 comments

Peergos: An EE2E P2P solution for sync and storage

https://peergos.org/
1•volemo•20m ago•1 comments

Is Rust Still Surging in 2025? Usage and Ecosystem Insights

https://medium.com/@datajournal/is-rust-still-surging-in-2025-49bfc6d1ce5d
1•adgnaf•22m ago•0 comments

Show HN: MemLineage: governed writes for AI agents

https://github.com/zhuamber370/memlineage
1•celastin•27m ago•1 comments

Do you validate before building or build and validate after?

https://www.founderspace.work
1•VladCovaci•28m ago•1 comments

Elevator Saga: The elevator programming game (2015)

https://play.elevatorsaga.com/index.html
1•xmprt•29m ago•0 comments

From 19k to 4.2M events/SEC: story of a SQLite query optimisation

https://mnt.io/articles/from-19k-to-4-2m-events-per-sec-story-of-a-sqlite-query-optimisation/
2•vinhnx•30m ago•0 comments

Show HN: FeatureDrop – Open-source client-side product adoption engine (3 kB)

https://github.com/GLINCKER/featuredrop
1•thegdsks•30m ago•1 comments

WiFi DensePose turns commodity WiFi signals into real-time human pose estimation

https://twitter.com/bowang87/status/2027941789848514643
1•taubek•34m ago•0 comments

Proton/SimpleLogin disabled my paid account for using my own email

2•alexlyee•36m ago•0 comments

Buzzy UCSD Math Readiness Report Failed to Mention Calculator Ban

https://www.insidehighered.com/news/students/academics/2026/02/26/buzzy-ucsd-math-readiness-repor...
1•loandbehold•43m ago•0 comments

My Thoughts on the Current State and Future Development of Bun

https://github.com/oven-sh/bun/issues/27664
1•narukeu•43m ago•0 comments

About memory pressure, lock contention, and Data-oriented Design

https://mnt.io/articles/about-memory-pressure-lock-contention-and-data-oriented-design/
1•vinhnx•44m ago•0 comments

390TB video game archive being taken offline due to skyrocketing RAM, SSD

https://www.tomshardware.com/video-games/390tb-video-game-archive-being-taken-offline-due-to-skyr...
2•neilfrndes•45m ago•0 comments

Show HN: ExamJungle – AI-driven exam simulators for IT certifications

https://examjungle.com/
1•Alex_Weinberg•46m ago•0 comments

Ask HN: Long-term recoverable digital vault without a master key?

1•YuriiDev•46m ago•0 comments

FYI ChatGPT Plus credit if you delete before term ends

1•pbnjay•52m ago•0 comments

Show HN: External Threat Protection in GitHub Agentic Workflow

https://safedep.io/safedep-mcp-in-github-agentic-workflow/
1•knlsn•56m ago•0 comments

Show HN: The Dot

https://jadsaklawi.github.io/The%20Dot/The%20Dot.html
1•_jad_•1h ago•2 comments

Ask HN: Is there something like Google style guide for AI-only coded apps?

1•robrenaud•1h ago•1 comments

Observability Theatre

https://rnjn.in/articles/observability-theatre/
2•rnjn•1h ago•1 comments