frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Crypto Finds Its Ideal User: A Government with a Sanctions Problem

https://dsrupt.news/articles/crypto-finally-finds-its-ideal-user-a-government-with-a-sanctions-pr...
1•zippiest•2m ago•0 comments

Use Temporal Foundation Model to predict drawing

https://github.com/chuanqisun/timesdraw
1•low_tech_punk•2m ago•0 comments

MuscleMemory: Skills learned by Codex, reused by Claude Code

https://github.com/TheWariend/MuscleMemory
2•devthewariend•2m ago•0 comments

Whoops: Meta's Muse Share Links Disclosed All Sorts of Super-Sensitive User Data

https://www.techdirt.com/2026/10/26/whoops-metas-muse-share-links-disclosed-all-sorts-of-super-se...
1•cdrnsf•3m ago•0 comments

How I Work with Agents

https://x.com/thorstenball/article/2114386050638336082
1•iBelieve•3m ago•0 comments

More and more iOS apps are stuck In Review since weeks

https://developer.apple.com/forums/verify-human/verify.html?next=/forums/topics/app-store-distrib...
1•SunboX•4m ago•0 comments

Markdown Is the New Executable: Attackers Drop Agent Instructions into Downloads

https://forever.security/blog/hijacking-ai-agents-via-downloads-folder/
1•torutofu•5m ago•0 comments

LiberNT

https://libernt.com/
1•wicket•5m ago•0 comments

What's Next for Dora?

https://leaddev.com/reporting/whats-next-for-dora
1•mooreds•5m ago•0 comments

100 Levels of Agentic Engineering

https://virpo.io/levels/
2•virpo•6m ago•1 comments

Emulate Apple II on Raspberry Pi

https://www.raspberrypi.com/news/emulate-apple-ii-on-raspberry-pi/
1•Brajeshwar•7m ago•0 comments

AI coding agents hijacked using AGENTS.md file in Downloads folder

https://www.bleepingcomputer.com/news/security/ai-coding-agents-hijacked-using-agentsmd-file-in-d...
1•Brajeshwar•7m ago•0 comments

Nvidia RTX 20, 30, and 40-series GPUs hacked to remove a half-speed AI throttle

https://www.tomshardware.com/pc-components/gpus/nvidia-rtx-20-30-and-40-series-gpus-hacked-to-rem...
1•Brajeshwar•8m ago•0 comments

The Roots of the UK's New Censorship Agency

https://www.effort.news/ncid
1•barry-cotter•8m ago•0 comments

Werner Forssmann

https://en.wikipedia.org/wiki/Werner_Forssmann
1•benbreen•8m ago•0 comments

Anthropic S-1

https://www.sec.gov/Archives/edgar/data/2130357/000162828026067168/anthropicpbc-sx1.htm#ica694c85...
1•stosssik•9m ago•1 comments

I Regret to Say That Gmail Is Now a Spam Farm

https://whatever.scalzi.com/2026/07/13/i-regret-to-say-that-gmail-is-now-a-spam-farm-or-why-you-s...
1•surprisetalk•9m ago•0 comments

Show HN: Building the European Company Graph one website at a time

https://firmendaten.researchly.at/netzwerk
1•leo_researchly•11m ago•0 comments

To detect deepfake videos, researchers turn to the physics of light

https://physicsworld.com/a/to-detect-deepfake-videos-researchers-turn-to-the-physics-of-light/
1•santiago-pl•12m ago•0 comments

Show HN: HealthViewer – A local-first desktop app for tracking lab results

https://healthviewer.net/
1•ChickadeeBandit•13m ago•0 comments

Enabling Researchers Is a Moral Imperative

https://www.yorba.co/internet-less/enabling-researchers-is-a-moral-imperative
1•schmudde•14m ago•0 comments

DOJ says presidents should be able to sell secrets on eBay

https://freedom.press/the-classifieds/doj-says-presidents-should-be-able-to-sell-secrets-on-ebay/
4•_____k•15m ago•0 comments

Marginalia Search, Money, C++, Rust, AI, Stuff

https://www.marginalia.nu/log/a_141_marginalia_update/
4•marginalia_nu•15m ago•0 comments

Getting debug access on a Brother printer over Telnet

https://inventor7777.github.io/blog/2026/10/22/debug-access-on-brother/
1•inventor7777•15m ago•0 comments

The N64's GPU Is a Technical Marvel [video]

https://www.youtube.com/watch?v=WEoRgQH08MA
1•wicket•15m ago•0 comments

Agentic AI has overtaken generative AI on S&P 500 earnings calls: 5,305 calls

https://equibles.com/research/agentic-ai-has-overtaken-generative-ai-on-earnings-calls-evidence-f...
1•daniel3303•15m ago•0 comments

Anthropic's Seven Founders

https://www.nytimes.com/2026/10/26/technology/anthropic-founders.html
1•donohoe•21m ago•0 comments

Show HN: TablePro – open-source native database client for macOS

https://github.com/TableProApp/TablePro
1•ngoquocdat•22m ago•0 comments

OLED iPad Mini

https://www.apple.com/shop/buy-ipad/ipad-mini
27•etothet•22m ago•7 comments

Connect your AI agents to your database without them having to write SQL

https://github.com/claix-dev/claix-for-devs
1•gael_dev•22m ago•0 comments