frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Midori Browser with 11.7 has arrived

https://astian.org/midori-en/awesome-midori-11-7-with-vpn-has-arrived/
1•ponchale•9m ago•0 comments

Conspiracy about missing/dead scientists from online forums to the White House

https://apnews.com/article/scientists-missing-dead-conspiracy-theories-c046ce6d0a004e6a3e1971ff76...
2•EA-3167•10m ago•1 comments

AI Socratic – The Latest in AI of April 2026

https://aisocratic.org/blog/ai-socratic-april-2026
1•feulf•10m ago•1 comments

Why the Washington Post Lost in Virginia

https://www.semafor.com/article/04/22/2026/why-the-washington-post-lost-in-virginia
1•lando2319•19m ago•0 comments

BugBash'26 Keynote

http://muratbuffalo.blogspot.com/2026/04/bugbash26-keynote.html
1•zdw•23m ago•0 comments

ICLR 2026 Outstanding Papers

https://blog.iclr.cc/2026/04/23/announcing-the-iclr-2026-outstanding-papers/
1•matt_d•24m ago•0 comments

Kapton Tape

https://en.wikipedia.org/wiki/Kapton
2•lorenzohess•25m ago•0 comments

I am trying to collect a robotics community to learn robotics from scratch

https://v0-haptal-zero-to-working-robot.vercel.app/
1•aaravbedi•25m ago•1 comments

Two Big Loan Defaults Add to Pain in Private-Credit Funds

https://www.wsj.com/finance/investing/two-big-loan-defaults-add-to-pain-in-private-credit-funds-b...
1•petethomas•27m ago•0 comments

The Coding Assistant Breakdown: More Tokens Please

https://newsletter.semianalysis.com/p/the-coding-assistant-breakdown-more
2•swolpers•27m ago•0 comments

The Last of the Lost Generation

https://porticoquarterly.com/book/the-last-of-the-lost-generation/
1•benbreen•28m ago•0 comments

AI written, edited or 'polished' books are being sold

https://theconversation.com/thousands-of-ai-written-edited-or-polished-books-are-being-sold-an-ee...
1•teleforce•28m ago•0 comments

Farewell ISDN, Ham Radio and Old Network Drivers: Linus Merges 138k LOC Removal

https://www.phoronix.com/news/Linux-7.1-Removes-Old-Net
3•voxadam•30m ago•0 comments

2 Men claimed a record by driving an old 3-wheel car length of Africa

https://apnews.com/article/adventure-car-africa-record-reliant-robin-0328f6a9dbcb407d539fe7aabf23...
1•gnabgib•30m ago•0 comments

Ask HN: Are We Destroying Earth?

2•vednig•32m ago•1 comments

Ancient DNA reveals pervasive directional selection across West Eurasia

https://www.nature.com/articles/s41586-026-10358-1
2•wslh•33m ago•0 comments

German cabinet approves 3-month IP address retention

https://europeanconservative.com/articles/news-corner/germany-merz-surveillance-ip-tracking-berli...
2•ExpertAdvisor01•44m ago•1 comments

GCC Establishes Working Group to Decide on AI/LLM Policy

https://www.phoronix.com/news/GCC-Working-Group-AI-Policy
1•breve•51m ago•0 comments

How Hard Is It to Open a File?

https://blog.sebastianwick.net/posts/how-hard-is-it-to-open-a-file/
1•vinhnx•51m ago•0 comments

Hotels are slashing World Cup rates as FIFA cancels rooms

https://moneywise.com/news/top-stories/fifa-world-cup-hotels-rates-tourism-slump
5•vrganj•1h ago•4 comments

Open VSX Sleeper Extensions Linked to GlassWorm Show New Malware Activations

https://socket.dev/blog/73-open-vsx-sleeper-extensions-glassworm
1•salkahfi•1h ago•0 comments

FilamentPHP MCP Server

https://github.com/suwi-lanji/filamentphp-mcp
1•bumi-kitan•1h ago•0 comments

Devin for Terminal

https://devin.ai/terminal
1•nickzana•1h ago•0 comments

The Bizarre Biology of Bdelloid Rotifers

https://biologicalrecording.co.uk/2025/09/23/bdelloid-rotifers/
2•thunderbong•1h ago•0 comments

SpaceX Starship Documentary – Test Like You Fly

https://www.spacex.com/content/starship/test-like-you-fly
1•d_silin•1h ago•0 comments

Xiaomi releases mimo-v2.5 model

https://mimo.xiaomi.com/mimo-v2-5
1•gainsurier•1h ago•0 comments

OpenAI's Sam Altman writes apology to community of Tumbler Ridge

https://www.cbc.ca/news/canada/british-columbia/sam-altman-tumbler-ridge-apology-9.7176482
4•acyou•1h ago•4 comments

Developing Trustworthy Information

https://jessicacanepa.com/blog/developing-trustworthy-information/
3•mooreds•1h ago•0 comments

YC as a Service

https://ycaas.lol
2•ipostragebait•1h ago•1 comments

Firefox Has Integrated Brave's Adblock Engine

https://itsfoss.com/news/firefox-ships-brave-adblock-engine/
29•nreece•1h ago•3 comments