frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Proton is the Future of PC gaming. But how does it work? [video]

https://m.youtube.com/watch?v=uScsmjvdwyo
1•evo_9•1m ago•0 comments

The Comma Four

https://blog.comma.ai/comma-four/
1•benjaminclauss•1m ago•0 comments

Is This How the AI Bubble Pops?

https://ofdollarsanddata.com/is-this-how-the-ai-bubble-pops/
1•andrevoget•2m ago•0 comments

Show HN: Sparse Matrix-Vector Multiplication that works at 30–90% sparsity

https://github.com/vlejd/macko_spmv
1•vlejd•2m ago•0 comments

Show HN: I analyzed 5k comments to quantify the Jira vs. Linear sentiment gap

https://deltabrandcheck.com/battles/linear-vs-jira
1•13pixels•3m ago•1 comments

AIMusubi – Local-First Agentic Network Automation for Cisco, Arista, and VyOS

https://github.com/aimusubi/aimusubi
1•aimusubi•3m ago•1 comments

Large language mistake Cutting-edge research shows language is not intelligence

https://www.theverge.com/ai-artificial-intelligence/827820/large-language-models-ai-intelligence-...
1•DrierCycle•4m ago•1 comments

Dark Side of Bug Bounty Pogrammes

https://medium.com/@christoscoming/dark-side-of-bug-bounty-programs-stop-wasting-time-to-report-a...
1•kalkikriva•5m ago•0 comments

Spyware Allows Cyber Threat Actors to Target Users of Messaging Applications

https://www.cisa.gov/news-events/alerts/2025/11/24/spyware-allows-cyber-threat-actors-target-user...
1•HelloUsername•5m ago•0 comments

When Poetry Meets AI Safety: A Critical Look at "Universal" Jailbreaks

https://daridor.blog/2025/11/23/when-poetry-meets-ai-safety-a-critical-look-at-universal-jailbreaks/
1•beagle3•5m ago•0 comments

Arduino's new terms of service worries hobbyists ahead of Qualcomm acquisition

https://arstechnica.com/gadgets/2025/11/arduinos-new-terms-of-service-worries-hobbyists-ahead-of-...
1•donutshop•6m ago•0 comments

The Value of Turning Around

https://steplong.substack.com/p/the-backwardsness-of-ideas-part-1
2•p44v9n•8m ago•0 comments

Germany wakes up to US tech dominance

https://www.politico.eu/article/germany-france-us-tech-dominance/
1•aa_is_op•8m ago•0 comments

We built a AI system that scores RW SMBs in 24–48h (curious about feedback)

1•rivellium•8m ago•0 comments

What does it mean to be massively against AI?

https://pythonbynight.com/blog/massively-against-ai
1•todsacerdoti•9m ago•0 comments

Content Repurposing Tools (and What They Do)

https://aiforcontentmarketing.ai/the-best-content-repurposing-tools-and-what-they-actually-do/
1•pakostina•10m ago•0 comments

Starlink Direct to Cell Available in Ukraine

https://kyivstar.ua/starlink
1•defly•11m ago•0 comments

Cheetos and Doritos to offer "no orange dust" variations starting December 1

https://www.pepsico.com/newsroom/stories/2025/cheetos-and-doritos-are-getting-naked
1•bookofjoe•11m ago•0 comments

Building AI Agents for DevOps: From CI/CD Automation to Autonomous Deployments

https://muhammadraza.me/2025/building-ai-agents-devops-automation/
2•mr_o47•11m ago•0 comments

Age Verification, Estimation, Assurance, Oh My a Guide to the Terminology

https://www.techdirt.com/2025/11/24/age-verification-estimation-assurance-oh-my-a-guide-to-the-te...
2•speckx•11m ago•0 comments

Microsoft is speeding up the Teams desktop client for Windows

https://www.bleepingcomputer.com/news/microsoft/microsoft-to-boost-teams-performance-with-new-cal...
1•fleahunter•11m ago•0 comments

My New Yorker college roommate and I vibe-coded "Mamdani Run"

https://mamdanirun.cc/
3•gavrielamati•12m ago•1 comments

Authenticating AI Agents

https://fusionauth.io/blog/ai-agent-identity-overview
1•mooreds•12m ago•0 comments

Hotelling's Law

https://fffej.substack.com/p/hotellings-law
2•mooreds•13m ago•0 comments

Show HN: Deft-Intruder – Real-time malware detection daemon for Linux

https://github.com/539hex/deft-intruder
1•539hex•13m ago•0 comments

Lessons from two failed promotions and what changed after ZIRP

https://world.hey.com/joaoqalves/lessons-from-two-failed-promotions-and-what-changed-after-zirp-d...
1•joaoqalves•14m ago•0 comments

Show HN: Bindu – an auth, payment, and communication layer for AI agents

https://github.com/GetBindu/Bindu
1•raahul_rahl•15m ago•0 comments

Leveraging tech communities for your career

https://sia.codes/posts/career-and-community/
1•mooreds•15m ago•0 comments

Markdown Translator

https://github.com/fanzhidongyzby/markdown-translator
1•fanzhidongyzby•15m ago•0 comments

Launch HN: Onyx (YC W24) – The open-source chat UI

1•Weves•16m ago•0 comments