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.

Agentic SQL for Free with Qwen3.8 27B

https://motherduck.com/blog/Agentic-SQL-for-Free-with-Qwen3.8-27B-and-DuckDB/
1•ijidak•3m ago•0 comments

Uber to Cut 3,300 Jobs in Company Overhaul to Reduce Management Layers

https://www.bloomberg.com/news/articles/2026-09-02/uber-to-cut-3-300-jobs-in-company-overhaul-to-...
1•helsinkiandrew•3m ago•0 comments

Natural emergent misalignment from reward hacking

https://www.anthropic.com/research/emergent-misalignment-reward-hacking
1•tosh•8m ago•0 comments

Old Directions: Reviving the spirit of revival

https://www.nplusonemag.com/issue-54/the-intellectual-situation/old-directions/
1•samclemens•10m ago•0 comments

Neoclouds Have Limited Cybersecurity

https://twitter.com/ilyasut/status/2094881278621253755
1•tosh•11m ago•0 comments

Ichnos – a survey of where locked-out industries can still advertise

https://nexus.knightbyrd.com/ichnos
1•davidat8•15m ago•0 comments

Agentic Testing

https://theaiengineer.substack.com/p/what-is-agentic-testing-fa2
1•ltononro•15m ago•0 comments

XPT – An AI workspace for more open-ended conversations

https://xpt.chat/
1•iwarticles•16m ago•0 comments

Chinese court freezes $300M of Nexperia assets in Wingtech case

https://www.reuters.com/world/asia-pacific/chinese-court-freezes-dutch-chipmaker-nexperia-bvs-sta...
1•emmelaich•17m ago•0 comments

Show HN: Apply AI – AI Studio for resumes, interviews, and job applications

https://apply-ai.work
1•petritavd•19m ago•0 comments

LLM Judges Verify Presence, Not Absence: Omission Blindness in AI Clinical Notes

https://arxiv.org/abs/2608.31016
2•sbulaev•20m ago•0 comments

Ask HN: How do you debug production bugs when you can't access customer data?

3•Securelytixdev•21m ago•0 comments

Dextroproporphan: An Analogue for a Better Dextromethorphan

https://monfak.top/blog/posts/dextroproporphan
1•znano•21m ago•0 comments

Google is catching up in coding, finally

https://www.wsj.com/tech/ai/new-google-ai-model-said-to-narrow-gap-on-coding-ability-264c6052
2•ltononro•23m ago•1 comments

Show HN: BlazeRules – YAML rule engine for streaming data, 3M records/SEC

1•jspuri•24m ago•0 comments

Making the AI-powered case for legacy modernization

https://www.technologyreview.com/2026/09/01/1142180/making-the-ai-powered-case-for-legacy-moderni...
1•joozio•26m ago•0 comments

It's OK to hardcode feature flags (2025)

https://code.mendhak.com/hardcode-feature-flags/
1•biscuits1•27m ago•0 comments

Rescue Trucks at LaGuardia and JFK Get Trackers After Fatal Crash

https://www.nytimes.com/2026/08/31/nyregion/rescue-trucks-at-laguardia-and-jfk-get-trackers-after...
1•bookofjoe•30m ago•1 comments

SteamDB Joins Nexus Mods

https://steamdb.info/blog/steamdb-nexus-mods/
3•Nazar1ky•32m ago•0 comments

Taiwan's six-year hunt for China's undercover chip labs

https://restofworld.org/2026/taiwan-china-chip-investigations/
1•donohoe•33m ago•0 comments

VibeGuard – security linter for AI-generated code

https://github.com/zeroFhacker/vibeguard
2•obadafid•33m ago•0 comments

Emily Wilson is completely re-translating The Odyssey

https://apnews.com/article/emily-wilson-odyssey-translation-homer-a05084e4753528dff13766703161d24d
1•throw0101a•36m ago•0 comments

AI data center investment will top $1T by 2027, raising insurance exposures

https://beinsure.com/news/ai-data-center-investment-will-top-1trn/
1•Beinsure•36m ago•0 comments

This North Carolina County Is Taking Mug Shots Offline. Not Everyone Agrees

https://www.wsj.com/us-news/this-north-carolina-county-is-taking-mug-shots-offline-not-everyone-a...
1•impish9208•36m ago•1 comments

AmiGTA1 – A native AmigaOS 68k port of Grand Theft Auto is in the works

https://www.indieretronews.com/2026/08/amigta1-native-amigaos-68k-port-of.html
1•doener•39m ago•0 comments

Ame framinPII redaction and tamper-evident audit logs for LiteLLM, no sidecars

https://github.com/Rishikesh-glitch/Cloakwall-v0.1
1•Rishikesh-glitc•46m ago•0 comments

State of Postgres in 2026

https://www.datadoghq.com/state-of-postgres/
1•plaur782•46m ago•0 comments

CBallast – Quick Procurement Guide

https://cballast.com/
1•IndustrialProcu•46m ago•0 comments

Running in sari, 47-year-old woman wins race for daughters' education

https://www.edexlive.com/news/running-barefoot-in-sari-47-year-old-woman-wins-race-for-daughters-...
1•thunderbong•47m ago•0 comments

Why are there no flow batteries with symmetric ferrocyanide electrolytes?

https://chemisting.com/2026/09/02/why-are-there-no-flow-batteries-with-symmetric-ferrocyanide-ele...
3•DamonHD•48m ago•0 comments