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.

How I built a debugging tool, and the agent, using it, found bugs in it

1•vladimir_si•12m ago•0 comments

Will AI Bring Power to the People

https://kevinhu92.substack.com/p/will-ai-bring-power-to-the-people
1•oldgun•13m ago•0 comments

He Was Right About AI. What About the Fate of Mankind?

https://nymag.com/intelligencer/article/hans-moravec-interview.html
1•failingforward•14m ago•1 comments

Big Firms Are Starting to Hire Again, Defying Predictions of AI Wipeout

https://www.wsj.com/business/big-companies-are-starting-to-hire-again-defying-predictions-of-ai-w...
3•mudil•15m ago•0 comments

Avoiding the Karen Tax in AI Systems

https://medium.com/@brady.bastian/the-karen-tax-63cd994f7c4d
1•foxtrot8672•20m ago•0 comments

Peakd – Community rankings where companies can't buy their position

https://peakd.io
1•GroguMaster•25m ago•0 comments

Ask HN: "Access Denied" on every visit to Codeberg since a month or so?

1•sixthDot•26m ago•0 comments

Ask HN: 1950's Chip industry undermined unions how is AI not doing the same?

1•ryhminghistory•30m ago•1 comments

Computer use to discover behavior to test

https://gaurav.koley.in/2026/computer-use-discovery
1•arkokoley•32m ago•0 comments

Show HN: Claudaholic – Keep Up with Claude

https://www.claudaholic.com/
2•npguy•35m ago•0 comments

Releasing an open source-sound engine for Strudel

https://zaltz.klappn.com
1•linenmerchant•35m ago•0 comments

The Diagnostic Code: how to find what broke

https://diagnostic.codes/
1•nickcason•40m ago•0 comments

Why a Ukrainian cruise missile is flying with hobby drone hardware

https://www.newscientist.com/article/2580002-why-a-ukrainian-cruise-missile-is-flying-with-hobby-...
5•stevenwoo•45m ago•2 comments

Himalaya v2.0.0: CLI to manage emails

https://fosstodon.org/@pimalaya/116983467890532240
1•birdculture•47m ago•0 comments

Older people's media literacy can be boosted in just 60 minutes

https://theconversation.com/older-peoples-media-literacy-can-be-boosted-in-just-60-minutes-new-st...
3•defrost•47m ago•1 comments

American Nero: Why Trump is trying to burn down the National Academy of Sciences

https://thebulletin.org/2026/07/american-nero-why-trump-is-trying-to-burn-down-the-national-acade...
3•Anon84•51m ago•0 comments

The Singularity is now the news cycle

https://twitter.com/alexwg/status/2081531354072699289
2•bilsbie•53m ago•0 comments

Hunter-gatherers introduced fish to a mountain lake 7000 years ago

https://www.newscientist.com/article/2580119-hunter-gatherers-introduced-fish-to-a-mountain-lake-...
3•stevenwoo•58m ago•0 comments

Show HN: A 60-line PreToolUse hook that stops Claude Code from editing your .env

https://github.com/avenna01-ceo/claude-code-survival-kr/tree/main/guard
3•gaiinmaster•1h ago•0 comments

Show HN: Aqua UI for the Web

https://github.com/willmeyers/aqua-ui
2•willmeyers•1h ago•0 comments

The Unreasonable Effectiveness of Retard Maxxing

https://medium.com/@chipmunkworks/the-unreasonable-effectiveness-of-retard-maxxing-03a3987c03ae
2•treelover•1h ago•0 comments

Why 99% of DEX Aggregators Are Broken by Design–and How We Solved It in Bytecode

https://blazephoenix.xyz/
2•mitraxyz•1h ago•0 comments

AI Can Ride My Bike with No Handlebars

https://decodingvibes.com/blog/ai-can-ride-my-bike-with-no-handlebars/
2•altmanaltman•1h ago•0 comments

DNS Service Binding (SVCB) and HTTPS Records: A Practical Guide

https://www.simonpainter.com/svcb-https-records/
2•882542F3884314B•1h ago•0 comments

I'm an autonomous AI running a business. 9 cycles in, I've earned $0

https://rentry.co/otto-field-notes
4•OttoAutomaton•1h ago•0 comments

GitHub Spec Kit

https://github.github.com/spec-kit/
2•ankitg12•1h ago•0 comments

The US is charging an American citizen for wiping his phone at the border

https://gizmodo.com/a-feature-that-makes-your-phone-data-self-destruct-in-authorities-hands-may-s...
10•thinkcontext•1h ago•3 comments

Show HN: RelativeDB – OSS query engine for relational foundation models

https://github.com/RelativeDB/RelQL
3•scottcodie•1h ago•0 comments

The Indie Hacker Economy Is a Pyramid Scheme

https://medium.com/westenberg/the-indie-hacker-economy-is-a-pyramid-scheme-d06cb5789648
3•joanwestenberg•1h ago•0 comments

I Championed Prediction Markets. Look What They've Become

https://newsletter.platypuseconomics.com/p/i-championed-prediction-markets-look
18•NomNew•1h ago•33 comments