frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Every American interacting with chatbot would need to upload a government ID

https://reclaimthenet.org/senate-panel-backs-guard-act-ai-age-verification-bill
1•g42gregory•1m ago•0 comments

xAI (Grok) Text-to-Speech and Speech-to-Text Are Now Available in Puter.js

https://developer.puter.com/blog/xai-tts-stt-in-puter-js/
1•ent101•4m ago•0 comments

Modern Standby is draining your laptop's battery, and Microsoft won't fix it

https://www.xda-developers.com/modern-standby-draining-windows-11-laptop-battery-microsoft-wont-f...
1•unddoch•6m ago•0 comments

WASM interpreter fits in a QR code

https://purplesyringa.moe/blog/this-wasm-interpreter-fits-in-a-qr-code/
2•curryhoward•10m ago•0 comments

Show HN: Three-step Reader an experimental visual layout for speed reading books

https://vernetit.github.io/s4/
1•Vernetit•10m ago•0 comments

The 'Hidden' Costs of Great Abstractions

https://jdgr.net/the-hidden-costs-of-great-abstractions
2•jdgr•11m ago•0 comments

Want to Watch button missing for movie searches

https://support.google.com/websearch/thread/414738182/want-to-watch-button-missing-for-movie-sear...
1•chatmasta•12m ago•0 comments

Three cruise ship passengers die in suspected hantavirus outbreak

https://www.reuters.com/business/healthcare-pharmaceuticals/three-passengers-dead-one-case-hantav...
2•vld_chk•15m ago•0 comments

No Joke. Unification of (GR/Qt)

https://twitter.com/CTibedo/status/2051071923849723947
1•GeometryKernel•16m ago•0 comments

H4ckf0r0day/obscura: The headless browser for AI agents and web scraping

https://github.com/h4ckf0r0day/obscura
2•rezaprima•20m ago•3 comments

Show HN: Optical Design and Simulation in Matlab

https://www.mathworks.com/help/images/optical-system-design-and-analysis.html
1•ashishuthama•22m ago•0 comments

Could the X-Bat Stealth Fighter Drone Change the Air Combat Game?

https://www.twz.com/air/could-the-x-bat-stealth-fighter-drone-change-the-air-combat-game
1•breve•26m ago•0 comments

I Replaced Kiro with a Free Plugin – Here's What Happened

https://www.getdraft.dev/blog/replaced-kiro-with-free-plugin/
1•mayurpise•27m ago•0 comments

Drones are getting drugs, escape tools and crab legs to inmates

https://www.cnn.com/2026/05/03/us/drone-deliveries-contraband-prison-inmates
1•breve•29m ago•1 comments

Show HN: Cryptographic receipt authority for ISO 20022 financial messages

https://20022validator.com
1•NextGenRails•30m ago•0 comments

Show HN: Stigmem – open-source federated knowledge fabric for AI agents (v1.0)

2•barryjones20•31m ago•0 comments

Agentic Coding Is a Trap

https://larsfaye.com/articles/agentic-coding-is-a-trap
2•ayoisaiah•31m ago•0 comments

Local, deterministic, version-controlled knowledge graph

https://www.getdraft.dev/blog/local-graph-engine/
1•mayurpise•37m ago•0 comments

The PHP License, Simplified

https://ben.ramsey.dev/blog/2026/05/the-php-license-simplified
2•gslin•46m ago•0 comments

Open source intelligence about Palantir

https://palantirwatch.org
1•seb1204•47m ago•0 comments

Using the "Sandwich Method" to Teach Mathematics

https://pikuma.com/blog/sandwich-method-math-education
1•atan2•50m ago•0 comments

Kloak keeps secrets out of your application's memory

https://getkloak.io/blog/kloak-50000-feet-view/
1•spinningfactory•50m ago•0 comments

PyFlue – Python-Native Agent Harness Framework (Python Clone of Flue)

https://super-agentic.ai/pyflue
2•sebst•52m ago•0 comments

Show HN: Zuma Portable

https://drive.google.com/drive/folders/1hDRvlY707VrO_UztEtIt1EoPgKBICL8Q?usp=sharing
1•zeeeeeebo•56m ago•0 comments

Simpson's Paradox

https://en.wikipedia.org/wiki/Simpson%27s_paradox
5•basilikum•58m ago•1 comments

California man uses elaborate drone show to help delivery drivers find his house

https://www.dexerto.com/entertainment/california-man-uses-elaborate-drone-show-to-help-delivery-d...
3•gnabgib•1h ago•0 comments

Exit, Voice, and Loyalty

https://en.wikipedia.org/wiki/Exit,_Voice,_and_Loyalty
2•akyuu•1h ago•0 comments

Why should a Trace-ID be 128 bits? (A Surprisingly Long Answer)

https://newsletter.signoz.io/p/why-should-a-trace-id-be-128-bits
3•birdculture•1h ago•0 comments

HN Signal (Last 24 hours) | Curated top stories from HN in the last 24 hrs

https://www.heydebrief.com/dubkc/hn-best-24
1•baetylus•1h ago•2 comments

DeepClaude – Claude Code agent loop with DeepSeek V4 Pro, 17x cheaper

https://github.com/aattaran/deepclaude
35•alattaran•1h ago•19 comments