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.

AI Rot Management

https://jw.hn/ai-code-hygiene
1•jwpapi•3m ago•0 comments

Big tech stocks lose billions as AI spending fears hit valuations

https://www.reuters.com/business/retail-consumer/global-markets-marketcap-2026-02-16/
3•billybuckwheat•4m ago•0 comments

Robert Duvall, a Chameleon of an Actor Onscreen and Onstage, Dies at 95

https://www.nytimes.com/2026/02/16/movies/robert-duvall-dead.html
3•bookofjoe•4m ago•1 comments

Vulkan Easy => Reactor (Like React-App)

https://github.com/AndreeSalazar/REACTOR-Framework-for-Vulkan-
2•QDanteX•4m ago•0 comments

Anthropic got an 11% user boost from its OpenAI-bashing Super Bowl ad

https://www.cnbc.com/2026/02/13/anthropic-open-ai-super-bowl-ads.html
1•general_reveal•4m ago•0 comments

Anthropic Raised $30B. Where Does It Go?

https://fromtheprism.com/anthropic-30-billion
1•heavymemory•6m ago•0 comments

Show HN: VoteShip – Feature request platform built for AI agents

https://voteship.app/
1•madcash•6m ago•0 comments

Porous material uses green and blue light to repeatedly store and release CO₂

https://phys.org/news/2026-02-porous-material-green-blue.html
1•westurner•7m ago•0 comments

Jon Favreau Confirms Lucasfilm Exit Amid 'The Mandalorian and Grogu' Uncertainty

https://insidethemagic.net/2026/02/jon-favreau-lucasfilm-star-wars-mandalorian-grogu-exit-cj1/
2•iancmceachern•7m ago•1 comments

AI Slopageddon and the OSS Maintainers

https://redmonk.com/kholterhoff/2026/02/03/ai-slopageddon-and-the-oss-maintainers/
1•mooreds•8m ago•0 comments

Peon Training feature piggybacks on AI coding session

https://github.com/PeonPing/peon-ping
1•mthwsjc_•8m ago•0 comments

Governor: Extensible CLI for security-auditing AI-generated applications

https://github.com/ulsc/governor
1•ulsc•10m ago•0 comments

Suicide Linux (2009)

https://qntm.org/suicide
2•icwtyjj•10m ago•0 comments

Unreal Tournament 2004 is now available for free thanks to its fan community

https://www.pcgamer.com/games/fps/unreal-tournament-2004-is-now-available-for-free-thanks-to-its-...
1•donutshop•10m ago•0 comments

The Three Juggling Acts (Strategic, Lazy, and Survival)

https://cutlefish.substack.com/p/tbm-407-the-three-juggling-acts-strategic
1•mooreds•11m ago•0 comments

Discovering Domains via NS Correlation

https://interrupt.sh/blog/discovering-domains-via-ns-correlation/
1•arwt•11m ago•0 comments

App builder market trends and statistics

https://getmocha.com/blog/ai-app-builder-statistics/
3•nichochar•13m ago•0 comments

Glimpse – A Clean Architecture Pattern for Django and Coding Agents

https://medium.com/@radoslaw_jan/a-glimpse-of-cleaner-architecture-for-django-projects-c31295529eb5
1•novagalileo•18m ago•0 comments

Show HN: PicoGPT v2 – GPT in <40 lines of vanilla JS running from a QR code

https://github.com/Kuberwastaken/picogpt
2•kuberwastaken•19m ago•1 comments

Programming in Prison: My Redemption Arc

https://www.ck-7vn.dev/blog/Home
4•CK-7vn•19m ago•1 comments

Show HN: RuneScapeCN (open source) – UI kit with authentic OSRS styling

https://github.com/alns0dev/runescapecn
1•alns0•21m ago•0 comments

Testing Postgres race conditions with synchronization barriers

https://www.lirbank.com/harnessing-postgres-race-conditions
2•lirbank•21m ago•0 comments

Let's Practice (Bsd.rd)

https://openbsdjumpstart.org/bsd.rd/
1•todsacerdoti•21m ago•0 comments

Outcome Engineering: The O16g Manifesto

https://o16g.com/
1•mooreds•22m ago•0 comments

The Internet Is Dead

https://joinkith.com/#the-internet-is-dead
1•elliotbnvl•22m ago•1 comments

Zero Knowledge (About) Encryption: Security Analysis of Cloud Password Managers

https://eprint.iacr.org/2026/058
2•gnabgib•22m ago•0 comments

Ask HN: Companies that advertise being a "best place to work", is it a red flag?

3•jrs235•25m ago•2 comments

Simple non-hype agentic coding workflow for well-established codebases

https://alyosha.net/posts/simple-non-hype-agentic-coding-workflow-that-works-for-well-established...
1•nkzd•26m ago•0 comments

Show HN: SafeClaw – Sleep-by-default AI assistant with runtime tool permissions

https://github.com/rawalrahul/safeclaw
1•rawaldelhi•28m ago•0 comments

"King Me": A Defense of King-Making in Board Game Design [video]

https://www.youtube.com/watch?v=UraJElx1ebg
1•euthymiclabs•28m ago•0 comments