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.

The song: "I've Been Everywhere", but with programming languages [video]

https://www.youtube.com/watch?v=to1F2N48kF8
1•jeremysarchet•24s ago•1 comments

Tube Search

https://tube.archivarix.net/
1•archivarix•34s ago•0 comments

Trellix source code breach claimed by RansomHouse hackers

https://www.bleepingcomputer.com/news/security/trellix-source-code-breach-claimed-by-ransomhouse-...
1•Brajeshwar•1m ago•0 comments

Analyse des DNS-Ausfalls vom 5. Mai 2026

https://blog.denic.de/analyse-des-dns-ausfalls-vom-5-mai-2026/
1•raffael_de•2m ago•1 comments

The Copyright Office wants to increase copyright registration fees

https://nppa.org/news/845
1•Tomte•4m ago•0 comments

AI or a Composite? An Award-Winning Owl 'Photo' Ruffled a Lot of Feathers

https://petapixel.com/2026/05/06/ai-or-a-composite-an-award-winning-owl-photo-ruffled-a-lot-of-fe...
1•Tomte•4m ago•0 comments

Build MCMC from Scratch in R: The 50-Line Algorithm Behind Brms and Stan

https://r-statistics.co/MCMC-in-R.html
1•selva86•4m ago•0 comments

Just Show Us the Spaceships

https://www.theatlantic.com/science/2026/05/alien-disclosures-real-evidence/687123/
1•rbanffy•5m ago•0 comments

PipeDream on the Acorn Archimedes

https://stonetools.ghost.io/pipedream-archimedes/
2•msephton•8m ago•0 comments

First, the FBI Searched Her Home. Then, She Won a Pulitzer.

https://www.nytimes.com/2026/05/05/business/media/hannah-natanson-washington-post-pulitzer.html
1•mitchbob•9m ago•1 comments

If AI made Cloudflare more productive, the layoffs are the wrong move

https://lord.technology/2026/05/08/if-ai-made-cloudflare-more-productive-the-layoffs-are-the-wron...
2•gpi•10m ago•0 comments

Kalshi valuation quadruples to $22B in less than a year

https://www.ft.com/content/76ba916e-0478-4556-a341-a1ddd83dda2a
2•bookofjoe•11m ago•1 comments

Nvidia Goes to Zero. – By JA Westenberg – Selfonomics

https://www.selfonomics.com/p/nvidia-goes-to-zero
2•tambourine_man•11m ago•0 comments

When an iPhone Backup is not a Backup

https://support.apple.com/en-us/108771
1•nacho-daddy•12m ago•1 comments

Using SwiftUI to Build a Mac-Assed App in 2026 – Paulo's Blog

https://pfandrade.me/blog/mac-assed-swiftui-app/
1•tambourine_man•14m ago•0 comments

Anthropic NLAs translate LLM activations to human-readable text for safety

https://presciente.com/edition/78
1•sebastianperezr•15m ago•0 comments

Nvidia Goes to Zero

https://www.selfonomics.com/p/nvidia-goes-to-zerok
1•ndr42•18m ago•0 comments

I Lower Toxin Exposure in AirBnBs

https://littlegreensteps.substack.com/p/how-i-lower-toxin-exposure-in-airbnbs
1•grapevine12•19m ago•0 comments

Walking Slower? Why Your Ears, Not Your Knees, Might Be the Problem

https://www.wsj.com/health/wellness/hearing-loss-walking-speed-iphone-study-c53c482a
1•marc__1•20m ago•0 comments

AI Can Hang Up Now, It Still Takes the Abuse

https://kuber.studio/blog/AI/AI-Can-Hang-Up-Now-It-Still-Takes-the-Abuse
2•kuberwastaken•20m ago•0 comments

An Algorithmic Investigation of the Highfalutin 'Poet Voice'

https://www.atlasobscura.com/articles/cultural-analysis-poet-voice
1•bookofjoe•20m ago•0 comments

Exploration Hacking: Can LLMs Learn to Resist RL Training?

https://www.alignmentforum.org/posts/eeFFpKCDWE9gjfzsk/exploration-hacking-can-llms-learn-to-resi...
1•Prof_Sigmund•21m ago•0 comments

Show HN: Real-workload SQLite benchmarks on Hetzner's cheapest VPS

https://s13k.dev/blog/real-workload-sqlite-bench-on-5-dollar-vps/
1•s13k•22m ago•0 comments

Prevent cross-site tracking in Safari breaks Cloudflare challenges

https://lapcatsoftware.com/articles/2026/5/1.html
3•HotGarbage•26m ago•0 comments

Norway doubles down on oil and gas production

https://www.theguardian.com/world/2026/may/09/norway-oil-and-gas-production-shortages-middle-east...
2•andsoitis•27m ago•0 comments

Stop MitM on the first SSH connection with cloud-init

https://www.joachimschipper.nl/Stop%20MITM%20on%20the%20first%20SSH%20connection,%20on%20any%20VP...
1•fanf2•27m ago•0 comments

Patchwork: AST-Native Editing for LLMs

https://github.com/ThatXliner/patchwork-cli
2•thatxliner•28m ago•0 comments

Apple is increasing my cortisol levels

https://blog.kronis.dev/blog/apple-is-increasing-my-cortisol-levels
2•LorenDB•28m ago•0 comments

Show HN: JSLike, a CSP-Safe Interpreter for JS, TS, JSX, TSX in JS

https://github.com/artpar/jslike
2•artpar•31m ago•0 comments

Create flashcards with Space CLI

https://getspace.app/cli
1•friebetill•31m ago•1 comments