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.

I Put a Full JVM Inside a Browser Tab. It "Works". Technically. Eventually

https://bmarti44.substack.com/p/i-put-a-full-jvm-inside-a-browser
1•PaulHoule•1m ago•0 comments

Supreme Court Sides with Cox in Copyright Fight over Pirated Music

https://www.nytimes.com/2026/03/25/us/politics/supreme-court-cox-music-copyright.html
1•oj2828•1m ago•0 comments

I built a game where an AI judges whether things deserve each other

https://yoursoulmateis.wtf
1•edgardou•2m ago•1 comments

Malicious Litellm 1.82.8: Credential Theft and Persistent Backdoor

https://safedep.io/malicious-litellm-1-82-8-analysis/
1•alokDT•2m ago•0 comments

I built a YAML DSL for Temporal workflows

https://zigflow.dev/articles/why-i-built-a-yaml-dsl-for-temporal-workflows/
1•mrsimonemms•2m ago•0 comments

A Rare Blog

https://andys.blog/rare/
1•andytratt•2m ago•0 comments

Comprehensive C++ Hashmap Benchmarks (2022)

https://martin.ankerl.com/2022/08/27/hashmap-bench-01/
1•klaussilveira•2m ago•0 comments

I made a college punching bag for rejected highschoolers

https://re.ject.ing
1•skillseeddev•3m ago•1 comments

Elon Musk demands judge's recusal over LinkedIn post after $2B verdict

https://nypost.com/2026/03/25/business/elon-musk-seeks-recusal-of-delaware-judge-over-linkedin-su...
1•1vuio0pswjnm7•3m ago•0 comments

Quantum frontiers may be closer than they appear

https://blog.google/innovation-and-ai/technology/safety-security/cryptography-migration-timeline/
1•jgrahamc•3m ago•0 comments

So Much for O(1)

https://yasint.dev/hash-maps-under-the-hood/
2•sn0wflak3s•3m ago•0 comments

Profitable cash crop trend in Bangladesh's hills affects regional ecology

https://news.mongabay.com/2026/02/profitable-cash-crop-trend-in-bangladeshs-hills-affects-regiona...
1•PaulHoule•4m ago•0 comments

Choosing an LLM Framework in 2026

https://modelriver.com/blog/llm-frameworks
1•vishaal_007•4m ago•0 comments

They're Rich but Not Famous–and They're Suddenly Everywhere

https://www.wsj.com/economy/wealthy-americans-us-economy-dba0d26a
1•JumpCrisscross•5m ago•0 comments

Fooling Go's X.509 Certificate Verification

https://danielmangum.com/posts/fooling-go-x509-certificate-verification/
1•hasheddan•6m ago•0 comments

NASA's Hubble unexpectedly catches comet breaking up

https://phys.org/news/2026-03-nasa-hubble-unexpectedly-comet.html
1•bookofjoe•6m ago•0 comments

Replacing cloud LLM APIs with local, domain-specific models

https://github.com/eullm/eullm
1•primoco•7m ago•0 comments

New Brunswick unveils new AI chatbot for tourists, but not without errors

https://www.cbc.ca/news/canada/new-brunswick/new-brunswick-tourism-ai-chatbot-explora-9.7139199
1•ChrisArchitect•7m ago•0 comments

Agent sandboxing tools that mount projects R/W have gaping exploits

1•rsyring•7m ago•0 comments

Antimatter has been transported for the first time

https://www.nature.com/articles/d41586-026-00950-w
1•leephillips•7m ago•0 comments

Show HN: GetPeople=Terminal-style social OS with Big Five(OCEAN) personality

https://getpeople.vercel.app/
1•RioCherry•8m ago•1 comments

How to Keep ICE Agents Out of Your Devices at Airports

https://theintercept.com/2026/03/25/ice-airports-phone-security-privacy-safety/
7•cdrnsf•11m ago•0 comments

Show HN: Gencove low-pass sequencing genetic test with raw data access

https://www.gencove.com
3•mliezun•11m ago•0 comments

AI agents are now deciding what's safe to run (Claude Auto Mode)

https://grith.ai/blog/claude-auto-mode-removes-prompts-not-risk
3•edf13•12m ago•0 comments

Building CodeAtlas: A Public Dev Journey (Hosting, Apps, and Scaling Challenges)

https://github.com/lucyb0207/CodeAtlas
1•lucyb0207•12m ago•0 comments

Board Games Give You a False Sense of Socialising

https://lovkush.substack.com/p/why-i-hardly-play-board-games-anymore
1•eatitraw•12m ago•0 comments

How Auto-Research built the SOTA browser agent

https://browser-use.com/posts/online-mind2web-benchmark
1•MagMueller•12m ago•0 comments

Bill to prohibit members of Congress, president from prediction market trading

https://www.politico.com/live-updates/2026/03/25/congress/lawmakers-introduce-bill-to-prohibit-me...
1•JumpCrisscross•13m ago•0 comments

Cancer-causing chemical found to be leaking from gas cookers

https://www.newscientist.com/article/2520639-cancer-causing-chemical-found-to-be-leaking-from-gas...
1•Brajeshwar•14m ago•0 comments

How AI Kills at Scale

https://msukhareva.substack.com/p/how-ai-kills-at-scale
1•petethomas•15m ago•0 comments