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.

ShieldPi – MCP server for monitoring deployed AI agents

https://pypi.org/project/shieldpi-mcp/
1•Ch_Avinash•17s ago•0 comments

Show HN: Reloadium Search Describe what you want get optimal Google queries

1•julienreszka•59s ago•0 comments

Show HN: WordFor – a free, open, private reverse dictionary running on browser

https://wordfor.xyz/
1•zshn25•1m ago•0 comments

Show HN: Open Access Qwen3.6-35B-A3B-UD-Q5_K_M with TurboQuant

1•freakynit•2m ago•0 comments

Spousal Preferences Impact Career Outcomes of Men and Women

https://www.nominalnews.com/p/spousal-preferences-career-gender-pay-gap
1•NomNew•3m ago•0 comments

GetHireToday – AI resume builder that targets ATS keyword matching

https://gethiretoday.com
1•Haroonbasil•5m ago•1 comments

Data centre electricity use soared by 17% in 2025 - IEA

https://www.iea.org/news/data-centre-electricity-use-surged-in-2025-even-with-tightening-bottlene...
1•giuliomagnifico•6m ago•0 comments

GenAI Fails – A list of epic LLM fails

https://github.com/hb20007/awesome-gen-ai-fails
1•hb20007•6m ago•1 comments

EP Hearing: Should Video Game Publishers Be Allowed to Disable Games You Bought

1•haunter•7m ago•0 comments

Isaac Asimov: The Last Question

https://hex.ooo/library/last_question.html
1•ColinWright•7m ago•0 comments

Why having "humans in the loop" in an AI war is an illusion

https://www.technologyreview.com/2026/04/16/1136029/humans-in-the-loop-ai-war-illusion/
1•joozio•8m ago•0 comments

We beat Google's zero-knowledge proof of quantum cryptanalysis

https://blog.trailofbits.com/2026/04/17/we-beat-googles-zero-knowledge-proof-of-quantum-cryptanal...
1•ingve•9m ago•0 comments

Show HN: My fun laugh inducing/team building app, for dev or sales teams for ex

https://gifoftheweek.com
1•etiennead•11m ago•0 comments

One source of truth for buy-it-for-life products: would love your feedback

https://www.izyt.io/
1•lucci11•13m ago•1 comments

Who Wrote These Laws

https://agelesslinux.org/lobbyists.html
1•pbiggar•13m ago•0 comments

SteamOS ARM64 Reaches Nintendo Switch in First Experiment

https://onejailbreak.com/blog/steamos-runs-on-nintendo-switch/
3•SockThief•15m ago•0 comments

Mozilla launches Thunderbolt AI client with focus on self-hosted infrastructure

https://arstechnica.com/ai/2026/04/mozilla-launches-thunderbolt-ai-client-with-focus-on-self-host...
1•tomrod•16m ago•0 comments

Rust Koans

https://rust-koans.stonecharioteer.com
2•stonecharioteer•19m ago•0 comments

As helium-3 runs scarce, researchers seek new ways to chill quantum computers

https://www.science.org/content/article/helium-3-runs-scarce-researchers-seek-new-ways-chill-quan...
1•bookofjoe•19m ago•0 comments

FortyOne OS – an SMS-first open-source multi user AI assistant OS

1•glitchnsec•19m ago•0 comments

Why Another US Attempt on Greenland Looks Worryingly Inevitable [video]

https://www.youtube.com/watch?v=g1Bup39kwyM
1•Imustaskforhelp•20m ago•0 comments

I Streamed My Hard Drive to the World [video]

https://www.youtube.com/watch?v=Qmds7-mwCMg
1•edward28•22m ago•0 comments

Billionaire John Arnold Warns of Debt, Addiction in Online Sports-Betting Boom

https://www.bloomberg.com/news/articles/2026-04-17/billionaire-john-arnold-warns-of-debt-addictio...
1•helsinkiandrew•22m ago•1 comments

OPC Workflow – Three Markdown files that enforce discipline on AI coding tools

1•yoyayoyayoya•23m ago•0 comments

The macOS 26 Clock app still has an obvious bug

https://eclecticlight.co/2026/04/17/five-months-later-and-the-clock-app-still-has-an-obvious-bug/
1•chmaynard•23m ago•0 comments

AI Wall of Shame

https://www.aiwallofshame.com/
1•pain_perdu•23m ago•1 comments

Custom .af video format for WebCodecs: frame-accurate playback without <video>

https://github.com/activetheory/activeframe
2•luruke•24m ago•0 comments

The Guide to Free AI API Keys: 6 Platforms You Need to Know

https://www.yashkapure.com/en/blog/free-api-key-platforms-for-ai-usage/
1•yashkapure•25m ago•0 comments

Against Big Tech Credentialism

https://alexwennerberg.com/blog/2026-04-16-credentials.html
1•Sukram21•27m ago•0 comments

FPGA Powers DIY USB Scope and Signal Generator

https://hackaday.com/2026/04/16/fpga-powers-diy-usb-scope-and-signal-generator/
1•renw0rp•28m ago•0 comments