frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Ask HN: How are mobile apps able to track users without permission?

1•ratg13•52s ago•0 comments

Ask HN: I think I improved an Erdős problem how can I verify it?

1•donutthejedi•1m ago•0 comments

Walmart: ChatGPT checkout converted 3x worse than website

https://searchengineland.com/walmart-chatgpt-checkout-converted-worse-472071
1•speckx•3m ago•0 comments

Lenovo brings retro arcade cabinet gaming to high-end Android tablets

https://www.notebookcheck.net/Lenovo-brings-retro-arcade-cabinet-gaming-to-high-end-Android-table...
1•thunderbong•4m ago•0 comments

Show HN: llamafile 0.10.0 rebuilt, Qwen3.5, lfm2, Anthropic API

https://blog.mozilla.ai/llamafile-reloaded-whats-new-in-v0-10-0/
1•mzlaai•5m ago•0 comments

Aligning LLMs at inference time by suppressing internal concepts

https://www.guidelabs.ai/post/steerling-8b-alignment-without-retraining/
1•adebayoj•6m ago•0 comments

Anthropic takes legal action against OpenCode

https://github.com/anomalyco/opencode/pull/18186
2•_squared_•8m ago•0 comments

Constitution as Colimit: A coalgebraic model of emergence

https://github.com/helyn-research/constitution-as-colimit
1•cpobuda•9m ago•1 comments

Enisa Technical Advisory on Secure Use of Package Managers

https://socket.dev/blog/enisa-technical-advisory-on-secure-package-manager-use
1•pier25•9m ago•0 comments

Does deleting email help with climate change?

https://newslttrs.com/does-deleting-email-really-help-with-climate-change-part-one/
1•spzb•9m ago•0 comments

Security advisories for AI/ML infrastructure most scanners miss

https://raxe.ai/labs/advisories
2•raxe•10m ago•0 comments

No AI in Node.js Core

https://github.com/indutny/no-ai-in-nodejs-core
3•porsager•10m ago•0 comments

ICE officers are taking DNA samples from protesters they've arrested

https://www.npr.org/2026/03/18/nx-s1-5739257/ice-officers-dna-protesters-database
2•pseudolus•10m ago•0 comments

OpenAI accidentally built one of the richest charities. Now what?

https://www.vox.com/future-perfect/482653/openai-nonprofit-foundation-philanthropy
1•evolve2k•11m ago•0 comments

Europeans get a downgraded version of AI, which can only delay their adaptation

https://www.lemonde.fr/en/opinion/article/2026/03/17/europeans-get-a-downgraded-version-of-ai-whi...
1•alephnerd•11m ago•0 comments

Google reveals its solution for Android sideloading: a mandatory waiting period

https://www.theverge.com/tech/897420/android-sideloading-unverified-developers-process
1•josephcsible•14m ago•2 comments

We started Revenue Wizards after watching too many GTM teams fail

https://revenuewizards.com/
1•yauheniban•16m ago•0 comments

MAI-Image-2: for limitless creativity

https://microsoft.ai/news/introducing-mai-image-2/
1•mirzap•16m ago•0 comments

Google details new 24-hour process to sideload unverified Android apps

https://lwn.net/Articles/1063735/
1•speckx•16m ago•2 comments

Big Banks Score Win Under New Plan to Loosen Capital Rules

https://www.wsj.com/finance/regulation/u-s-regulators-propose-more-lenient-capital-rules-for-big-...
1•JumpCrisscross•17m ago•0 comments

Show HN: Institute of Idleness – The only screen time your therapist approves

https://instituteofidleness.org/
1•aned22•18m ago•1 comments

Show HN: Where was this picture taken?

https://github.com/Berlin-West/Topology
1•RHEFOR•19m ago•0 comments

Border0 Is Joining Tailscale

https://tailscale.com/blog/border0-joins-tailscale
1•hmokiguess•19m ago•0 comments

Show HN: OG images to every page with a Cloudflare Worker (no origin changes)

https://snapog.com/guides/cloudflare
2•earonesty•20m ago•0 comments

Figma's stock drops 11% in two days after Google releases 'vibe design' product

https://www.cnbc.com/2026/03/19/figma-stock-drops-11percent-after-google-releases-vibe-design-pro...
3•matthest•21m ago•0 comments

Mozilla Releases Llamafile 0.10

https://www.phoronix.com/news/Mozilla-AI-Llamafile-0.10
3•hochmartinez•21m ago•0 comments

Thymic Health Consequences in Adults

https://www.nature.com/articles/s41586-026-10242-y
1•bookofjoe•22m ago•1 comments

Online Censorship's Institutional Power

https://twitter.com/XJosh/status/1791197635472150636
1•dershing•22m ago•0 comments

Ask HN: Any interesting traffic patterns in your llms.txt?

2•jdauriemma•23m ago•2 comments

Addressing GitHub's recent availability issues

https://github.blog/news-insights/company-news/addressing-githubs-recent-availability-issues-2/
1•ptrhvns•23m ago•0 comments