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.

Why can't India's government build a decent website?

https://www.economist.com/asia/2026/06/28/why-cant-indias-government-build-a-decent-website
1•andsoitis•13s ago•0 comments

A faster bump allocator for rust

https://owen.cafe/posts/stumpalo/
1•birdculture•7m ago•0 comments

AI 'exuberance' risks ending in lengthy investment bust

https://www.ft.com/content/e81ce414-e4bd-4e8c-bac7-94f7bf17def4
2•petethomas•15m ago•1 comments

Show HN: Image2JXL – a native macOS JPEG XL converter

https://old.reddit.com/r/givebest/comments/1ueh3v4/i_built_image2jxl_a_native_macos_app_for_local/
1•givebest•23m ago•0 comments

Self-learning skill for Claude: let the agent capture its own hard-won patterns

https://github.com/Kulaxyz/self-learning-skills
1•kulaxyz•28m ago•0 comments

Show HN: A site that emails you the day your Bitcoin stack hits $1M

https://amimillionaire.com/
1•pro_methe5•29m ago•1 comments

Show HN: Calybris Core, a deterministic audit engine for decisions in Rust

https://github.com/emirhuseynrmx/calybris-core
1•emirhuseyininci•29m ago•0 comments

Show HN: wavecat – a fully local personal agent that watches your screen

https://wavecat.ai/
1•sdkpanda•33m ago•0 comments

Better Images of AI

https://betterimagesofai.org/
2•Curiositry•42m ago•0 comments

We need tech news sources which exclude AI

8•botfriendsarent•43m ago•3 comments

AI Agent Credential Crisis: Six Months of Incidents

https://devfortress.net/blog/semi-annual-2026
1•arian_•44m ago•0 comments

Stanford's Hoover Inst: "The Wealth Tax: Recipe for Economic Disaster"Lionaire [video]

https://www.youtube.com/watch?v=x6k4W5Qzg8U
1•stmw•48m ago•1 comments

Mux – A tmux overlay for managing Claude Code sessions

https://github.com/fashton28/mux
1•fashton28•50m ago•0 comments

The Grim and the Dark: Jon Heder's journey into the world of Grimdark art [video]

https://www.youtube.com/watch?v=SBJefwNkLqo
2•andystanton•1h ago•0 comments

Company Cleaning Up the Reflecting Pool Says It Has 'Nothing to Hide'

https://www.wsj.com/us-news/lincoln-memorial-reflecting-pool-california-greenwater-services-729be960
2•petethomas•1h ago•0 comments

AI Agent Triggers Nuclear Strike After Getting Outmaneuvered in Civilization VI

https://decrypt.co/371877/ai-agent-nuclear-strike-civilization-vi-benchmark
5•Khaine•1h ago•0 comments

Cigarettes

https://funnelfiasco.com/blog/2026/06/28/on-cigarettes/
24•aendruk•1h ago•13 comments

Side-Stepping the Secretary Problem

https://www.evalapply.org/posts/side-step-secretary-problem-hiring/index.html
4•Curiositry•1h ago•0 comments

The Usefulness of AI Agents

https://erikjohannes.no/posts/20260408-on-the-usefulness-of-ai-agents/index.html
2•Curiositry•1h ago•0 comments

/Dev/Notion

https://www.notion.com/product/dev
3•handfuloflight•1h ago•0 comments

AI glasses are aiding cheating in exams. Test-obsessed Asia is ground zero

https://www.cnn.com/2026/06/26/asia/ai-glasses-cheating-exams-intl-hnk
4•pseudolus•1h ago•0 comments

AI Workflows in Production Without Burning Tokens

https://unmeshed.io/blog/bringing-ai-workflow-into-production-without-burning-tokens
1•jusonchan81•1h ago•0 comments

The Cheques Are for the Land Not the Intelligence

https://aidatumpoint.substack.com/p/the-cheques-are-for-the-land-not
2•MadCatBureau•1h ago•0 comments

Show HN: best.free

https://best.free/
2•nadermx•1h ago•0 comments

Crypto vs. community: 4k local US lenders join forces to fight 'stablecoins' law

https://www.theguardian.com/technology/2026/jun/28/crypto-v-community-local-lenders-fight-stablec...
3•pseudolus•1h ago•1 comments

New UK stealth fighter demonstrator takes shape

https://www.aerosociety.com/news/new-uk-stealth-fighter-demonstrator-takes-shape/
3•Gravityloss•1h ago•0 comments

You get to watch 1 season of 1 show for the rest of your life, what do you pick?

https://www.reddit.com/r/AskReddit/s/Sf9sqnAqRr
2•eeko_systems•1h ago•5 comments

QUIC, HTTP/3 and WebTransport Vulnerabilities and How PQCrypta Mitigates Them

https://pqcrypta.com/quic-hardening/
1•pqpdf•1h ago•0 comments

Wall Street regulator investigating prediction market giant Polymarket

https://www.politico.com/news/2026/06/26/cftc-investigation-prediction-market-polymarket-00978218
14•voxadam•1h ago•1 comments

Knowledge Distillation of Black-Box Large Language Models

https://arxiv.org/abs/2401.07013
32•babelfish•2h ago•11 comments