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.

I'm building a drone for the DARPA lift challenge [video]

https://www.youtube.com/watch?v=tohImHa4f5U
1•surprisetalk•29s ago•0 comments

Traumatic brain injury and risk of attempted suicide and violent crime (2023)

https://www.cambridge.org/core/journals/psychological-medicine/article/abs/traumatic-brain-injury...
1•tokai•55s ago•0 comments

Show HN: Reverse engineering user queries for ChatGPT/Gemini

https://github.com/syntropicsignal-ai/ai-visibility-audit
1•biduskamil•2m ago•1 comments

AI therapy chatbots in 2026: what Replika, Woebot, and ChatGPT get right

https://medium.com/@6thMind/ai-therapy-chatbots-in-2026-what-replika-woebot-and-chatgpt-get-right...
1•smanuel•3m ago•0 comments

Emacs 31 Is Around the Corner: The Changes I'm Daily Driving

https://www.rahuljuliato.com/posts/emacs-31-around-the-corner
2•frou_dh•4m ago•0 comments

Show HN: Glassboard – Draw over everything on your Mac, Khan-academy style

https://stem.ps/glassboard/
1•stemps23•7m ago•0 comments

From pixels to planning: Earth AI for nature restoration

https://research.google/blog/from-pixels-to-planning-earth-ai-for-nature-restoration/
1•thm•7m ago•0 comments

Pyrefly v1.1 is here 27% faster, refactoring tools, tensor shapes

https://pyrefly.org/blog/v1.1/
1•ocamoss•7m ago•0 comments

Show HN: Circular Map with You in the Middle

https://hubofuniverse.com/
1•bitslayer•11m ago•0 comments

Goodbye RSS

https://basic.bearblog.dev/goodbye-rss/
1•speckx•11m ago•0 comments

Smashing the NIMBYs created modern capitalism

https://worksinprogress.co/issue/how-abolishing-the-stakeholder-state-caused-the-industrial-revol...
1•bensouthwood•11m ago•0 comments

What's Easy Now? What's Hard Now?: Software is easier for coding agents

https://brooker.co.za/blog/2026/05/18/whats-easy-whats-hard.html
1•xenophenes•12m ago•0 comments

Show HN: Glisker – First-impression feedback on your landing page (no signup)

https://www.glisker.com/
1•merveceylan•13m ago•0 comments

Apple Prepares Second-Generation iPhone Air for Spring 2027

https://www.bloomberg.com/news/articles/2026-06-17/apple-prepares-second-generation-iphone-air-fo...
1•nicksergeant•13m ago•0 comments

Show HN: Polyvia – Multimodal document retrieval over 100K+ files

https://github.com/polyvia-ai/polyvia
1•mgierlach-polyv•15m ago•0 comments

Show HN: Eggspression – shape any bird egg in 3D from four parameters

https://s13k.dev/eggspression/
2•s13k•16m ago•0 comments

Cotect – move around your repo with WASD

https://cotect.dev/
1•grzracz•20m ago•0 comments

World’s “most famous” tree dies after 1000 years in England

https://www.dexerto.com/entertainment/worlds-most-famous-tree-dies-after-1000-years-in-england-33...
1•thunderbong•22m ago•0 comments

Thomas Jefferson to John Norvell (1807)

https://press-pubs.uchicago.edu/founders/documents/amendI_speechs29.html
1•downbad_•23m ago•0 comments

Life: Nasty, Brutish, and Short (1987)

https://www.jsoftware.com/papers/eem/life.htm
3•tosh•25m ago•0 comments

Systems Get Gamed

https://arnoldkling.substack.com/p/systems-get-gamed
3•jger15•29m ago•0 comments

I found 10k GitHub repositories distributing Trojan malware

https://orchidfiles.com/github-repositories-distributing-malware/
4•theorchid•29m ago•0 comments

We blamed 112,000 commits to find out If AI-written code is buggier

https://www.repowise.dev/blog/engineering/is-ai-written-code-buggier-than-human-code
2•swatiahuja•30m ago•1 comments

AI will create more jobs for humans, not replace them says Jeff Bezos

https://www.bbc.co.uk/news/articles/ceqdrw2yy3vo
1•rvz•31m ago•1 comments

Adding a Town Square

https://kevquirk.com/town-square
2•whilenot-dev•33m ago•0 comments

Modos Color Monitor Pushes E-Paper Displays Further

https://spectrum.ieee.org/modos-e-paper-monitor
3•Vinnl•34m ago•1 comments

Prediction Markets' Next Major Bet: Wall St. Traders

https://www.nytimes.com/2026/06/17/business/dealbook/hedging-prediction-markets-kalshi.html
2•thm•34m ago•0 comments

Commodore's New Flip Phone Focuses on Digital Detox

https://www.cnet.com/tech/mobile/commodores-new-flip-phone-focuses-on-digital-detox-blocks-social...
1•daniel_iversen•35m ago•1 comments

We Did the Math on Why the iPhone 18 Pro Could Cost $1,299

https://www.wsj.com/tech/personal-tech/apple-iphone-price-increase-e846d737
1•thm•36m ago•0 comments

Rue: Higher level than Rust but lower level than Go

https://github.com/rue-language/rue
1•tosh•38m ago•0 comments