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.

The Family Keeping Watch over a 52-Year-Old Pot of Soup

https://www.wsj.com/arts-culture/food-cooking/the-family-keeping-watch-over-a-52-year-old-pot-of-...
1•petethomas•39m ago•0 comments

The Department of Commerce has lifted export controls on Fable 5 and Mythos 5

https://twitter.com/i/status/2072106151890809341
1•mak8•46m ago•0 comments

An Uneasy Case for Project Management Tools

https://biggestfish.substack.com/p/an-uneasy-case-for-project-management
1•zeroonetwothree•46m ago•0 comments

SETI Institute Jobs

https://www.seti.org/career-center/
2•andsoitis•46m ago•0 comments

All Package Management Functionality Moved from Compiler to Build System

https://ziglang.org/devlog/2026/
2•surprisetalk•48m ago•0 comments

AI Adoption Across the United States

https://www.microsoft.com/en-us/corporate-responsibility/topics/ai-economy-institute/reports/us-a...
2•gmays•49m ago•1 comments

Americans see their country's past, present and future

https://www.economist.com/graphic-detail/2026/06/29/how-americans-see-their-countrys-past-present...
1•andsoitis•57m ago•0 comments

Forestiere Underground Gardens

https://en.wikipedia.org/wiki/Forestiere_Underground_Gardens
10•onemoresoop•58m ago•1 comments

Three deleted songs from G&S's "Iolanthe" found in the British Library

https://marc154391.substack.com/p/three-deleted-songs-from-iolanthe
1•quuxplusone•59m ago•1 comments

When Impressive Performance Gains Do Not Matter

https://blog.colinbreck.com/when-impressive-performance-gains-do-not-matter/
2•birdculture•1h ago•0 comments

1975 GE Fake LED Clock [video]

https://www.youtube.com/shorts/ylfVSMsTUqY
2•thunderbong•1h ago•0 comments

Show HN: Coding agent that compiles intent into deterministic DAG before running

https://github.com/arman-jalili/rigorix-oss
1•arman-w-jalili•1h ago•0 comments

Would you use Fable 5 by Anthropic or Replit

1•NishanStepak•1h ago•0 comments

San Francisco supervisors balk at 362-page, AI-assisted city code rewrite

https://missionlocal.org/2026/06/s-f-supervisors-balk-at-362-page-city-code-rewrite/
2•tastyface•1h ago•0 comments

Show HN: Agentic OS – the operating system for AI agents

https://aos.injecting.ai/
1•nickpismenkov•1h ago•0 comments

My Mac's Wi-Fi Was Crawling at 50 Mbps with a Perfect Signal

https://bugmedaily.substack.com/p/my-macs-wi-fi-was-crawling-at-50
2•enesunal•1h ago•0 comments

An opinionated (and mainly correct) guide to naming

https://adamtornhill.substack.com/p/an-opinionated-and-mainly-correct
3•gmays•1h ago•0 comments

Serving Local AI on My Jetson Through Durable Streams

https://s2.dev/blog/local-ai
1•shikhar•1h ago•0 comments

US lifts curbs on Anthropic's Fable, Mythos AI models

https://www.reuters.com/business/us-lift-export-controls-anthropics-fable-ai-model-tuesday-source...
2•lossolo•1h ago•1 comments

Anthropic launches Claude Science: an AI workbench for scientists (2026)

https://lucasaguiar.xyz/pt/posts/claude-science-ai-workbench-cientistas-2026/
3•isfttr•1h ago•0 comments

Discretizing Reward Models

https://arxiv.org/abs/2606.21795
1•gmays•1h ago•0 comments

Exploring The Spectrum -Dr. John Ott documentary on the health effects of light [video]

https://www.youtube.com/watch?v=BOUA8UAEAdY
1•Eaglo•1h ago•0 comments

Guardians of the Agents Formal verification of AI workflows. (Dec 2025)

https://cacm.acm.org/practice/guardians-of-the-agents/
1•steilpass•1h ago•0 comments

Oʻahu Needs More Solar, Not New Power Plants

https://civilbeat.org/2026/06/oahu-needs-more-solar-not-new-power-plants/
1•toomuchtodo•1h ago•1 comments

WhiteHouse lifts export control on Anthropic that froze its most advanced models

https://www.cnn.com/2026/06/30/tech/anthropic-export-control-ban-lifted-white-house
3•dstala•1h ago•2 comments

The Future of Software Engineering Is Here but Not Evenly Distributed (Updated)

https://twitter.com/jmugan/status/2065962078322438524
1•jmugan•1h ago•0 comments

Taiwan raids Super Micro in widening China chip smuggling probe

https://www.japantimes.co.jp/business/2026/06/30/taiwan-super-micro-china-chip-smuggling/
5•billybuckwheat•1h ago•0 comments

White House picks Avi Loeb with polarizing alien theories to lead UFO council

https://www.pbs.org/newshour/politics/white-house-picks-harvard-professor-with-polarizing-alien-t...
8•ceejayoz•1h ago•2 comments

The President Made More Than $1Billon in Crypto Deals

https://www.wsj.com/politics/policy/trump-made-more-than-1-billion-on-crypto-deals-part-of-2025-w...
66•fortran77•1h ago•10 comments

A Fusion First: Realta Demos Direct Energy Conversion

https://realtafusion.com/fusion-first-realta-demos-direct-energy-conversion/
1•sien•1h ago•0 comments