frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

Open in hackernews

Ask HN: Java why put string in a constant?

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

Non-Contact Heart Rate Monitoring Method Based on Wi-Fi CSI Signal

https://www.mdpi.com/1424-8220/24/7/2111
1•ryan_j_naughton•1m ago•0 comments

letting go

https://benexdict.io/p/letting-go
1•exolymph•2m ago•0 comments

Windows seemingly lost 400M users in the past three years

https://www.tomshardware.com/software/windows/windows-seemingly-lost-400-million-users-in-the-past-three-years-official-microsoft-statements-show-hints-of-a-shrinking-user-base
1•miles•3m ago•0 comments

How to Borrow Memory (2021)

https://www.falsifian.org/blog/2021/06/04/catalytic/
1•btdmaster•4m ago•0 comments

Show HN: Git command for creating snapshot commits on a not checked-out branch

https://github.com/meribold/git-snap
1•meribold•7m ago•0 comments

Philip K Dick FBI file

https://www.spyculture.com/philip-k-dick-fbi-file/
2•m-hodges•7m ago•0 comments

TIME100 Most Influential Companies 2025

https://time.com/collections/time100-companies-2025/7289661/google-deepmind/
2•tzury•8m ago•1 comments

AMD bets on rack-scale compute to boost AI efficiency 20x by 2030

https://www.theregister.com/2025/06/12/amd_20x_30/
2•PaulHoule•8m ago•0 comments

GPT4Free: "educational project" for free LLM inference from various services

https://github.com/xtekky/gpt4free
2•bobbiechen•9m ago•0 comments

Entropy of a Mixture

https://cgad.ski/blog/entropy-of-a-mixture.html
3•cgadski•10m ago•0 comments

Embracing the power of the empty set in API design: Requesting zero items

https://devblogs.microsoft.com/oldnewthing/20250630-00/?p=111320
2•ingve•10m ago•0 comments

A Possible Connection Between Mental Illness and Diet

https://undark.org/2025/06/24/mental-illness-ketogenic-diet/
2•gmays•11m ago•0 comments

Embodied AI Agents: Modeling the World

https://arxiv.org/abs/2506.22355
2•lucaspauker•11m ago•0 comments

Show HN: C.O.R.E – Opensource, user owned, shareable memory for Claude, Cursor

https://github.com/RedPlanetHQ/core
2•Manik_agg•11m ago•0 comments

ChatGPT intercepts clear cache keyboard shortcut in Brave

2•VibeCobold•14m ago•0 comments

LLVM: InstCombine: A PR by Alex Gaynor and Claude Code

https://simonwillison.net/2025/Jun/30/llvm/
1•mseri•15m ago•0 comments

Skyrocketing HBM Will Push Micron Through $45B and Beyond

https://www.nextplatform.com/2025/06/30/skyrocketing-hbm-will-push-micron-through-10-billion-and-beyond/
1•rbanffy•15m ago•0 comments

Connected Gmail mcp to AI voice

https://harmony.com.ai
2•bigonion•16m ago•1 comments

Boeing to Replace CFO Brian West with Former Lockheed Finance Chief

https://www.bloomberg.com/news/articles/2025-06-30/boeing-to-replace-cfo-west-with-former-lockheed-finance-chief
1•Bluestein•16m ago•0 comments

Simple low-dimensional computations explain variability in neuronal activity

https://arxiv.org/abs/2504.08637
1•iNic•20m ago•0 comments

From Pokémon Red to Standardized Game-as-an-Eval

https://lmgame.org
3•Yuxuan_Zhang13•22m ago•1 comments

The Whole Country Is Starting to Look Like California

https://www.theatlantic.com/economy/archive/2025/06/zoning-sun-belt-housing-shortage/683352/
4•ryan_j_naughton•22m ago•0 comments

Eigenvalues of Generative Media

https://stackdiver.com/posts/eigenvalues-of-generative-media/
3•d0tn3t•22m ago•1 comments

Universities Are Making You Look Stupid

https://arealsociety.substack.com/p/universities-are-making-you-look
2•arealsociety•25m ago•0 comments

Brazil's Supreme Court clears way to hold social media liable for user content

https://apnews.com/article/brazil-supreme-court-social-media-ruling-324b9d79caa9f9e063da8a4993e382e1
2•rbanffy•25m ago•0 comments

The New Skill in AI Is Not Prompting, It's Context Engineering

https://www.philschmid.de/context-engineering
15•robotswantdata•27m ago•1 comments

Ask HN: When will YC do a batch in Europe and/or Asia?

3•HSO•27m ago•2 comments

Repurposed Materials

https://www.repurposedmaterialsinc.com/view-all-products/
1•bookofjoe•28m ago•0 comments

Liberals, you must reclaim Adam Smith

https://davidbrin.blogspot.com/2013/11/liberals-you-must-reclaim-adam-smith.html
3•matthest•29m ago•1 comments

Symbients on Stage Coming Soon: Autonomous AI Entrepreneurs

https://www.forbes.com/sites/robertwolcott/2025/06/30/symbients-on-stage-coming-soon-autonomous-ai-entrepreneurs/
1•Bluestein•30m ago•0 comments