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.

A messenger where the operator has nothing to hand over

https://apps.apple.com/us/app/privamesh-messenger/id6785997584
1•privamesh•1m ago•0 comments

Nvidia dramatically reduces amount of OpenAI infra financing it may guarantee

https://www.reuters.com/business/nvidia-scales-back-250-billion-openai-data-center-guarantee-wsj-...
1•root-parent•2m ago•0 comments

Why is the GitHub trending page weirdly excluding DeepSeek projects?

1•tianyicui•3m ago•0 comments

Add JavaScript prototype methods with Symbols

https://symbola.github.io/symbola/
1•jcbhmr•3m ago•0 comments

Nixvim – Configure Neovim with Nix

https://github.com/nix-community/nixvim
1•peter_d_sherman•3m ago•0 comments

Show HN: FileIndexer (simple N-copies verification on ext USB drives)

https://www.thanassis.space/indexer.html
1•ttsiodras•4m ago•0 comments

AI Debt Failure Will Prompt Another Wave of Fed Bailouts [pdf]

https://www.myrmikan.com/pub/Myrmikan_Research_2026_08_14.pdf
1•timedude•8m ago•1 comments

Show HN: Conw.ai – Independent local AI platform and developer API

https://conw.ai
1•thomasconway01•9m ago•0 comments

Anthropic IPO valuation hinges on $190-200B 2028 revenue forecast

https://www.reuters.com/business/anthropic-ipo-valuation-hinges-190-200-billion-2028-revenue-fore...
1•root-parent•9m ago•0 comments

Laws of Robotics

https://github.com/Daniel-J-Mueller/Laws-of-Robotics/blob/main/Laws-of-Robotics.txt
2•daniel-j-muelle•10m ago•0 comments

The Best pattern is the one you can understand

https://hank.bond/posts/the-best-pattern-is-the-one-you-can-understand/
3•hankbond•17m ago•1 comments

Why compute might get 10x+ more expensive in coming years

https://www.dwarkesh.com/p/why-compute-might-get-10x-more-expensive
2•rzk•17m ago•0 comments

Congress Might Give ICE the Power to Go After Shoplifters

https://theintercept.com/2026/08/07/congress-ice-shoplifting-theft-surveillance/
2•Jimmc414•21m ago•1 comments

How wave overhangs work [video]

https://www.youtube.com/watch?v=IhAzIXTE-FE
2•hakonjdjohnsen•22m ago•0 comments

Do people care if articles are written by AI?

https://writifyai.com/blog/do-people-really-care-if-an-article-is-written-by-ai/
4•Wpnx330•22m ago•7 comments

Show HN: Interactive React component for fibre OTDR events

https://react-networks-lib.rackout.net/otdr-strip
4•matt-p•22m ago•0 comments

Show HN: OIDC auth done right in Sambee 0.9?

https://sambee.net/docs/release-info/news-and-changes/whats-new/
2•helgek•25m ago•0 comments

Beautiful UI for AI-native interfaces

https://www.beautifului.dev/
2•kodisha•26m ago•0 comments

How I developed an Am29000 C compiler and web browser

https://nanochess.org/am29000_c_compiler_web_browser.html
2•nanochess•27m ago•0 comments

Jellyfish-hit French nuclear plant shuts down three reactors

https://www.reuters.com/business/energy/jellyfish-hit-french-nuclear-plant-shuts-down-three-react...
1•choult•28m ago•0 comments

The Case Against Formal Verification, 50 Years Later

https://ivan-gavran.github.io/0-social-processes-paper
16•ghuntley•30m ago•5 comments

Anthropic sees AI risks rising, no plan to release stronger "Model 2"

https://www.axios.com/2026/08/14/anthropic-model-2-ai-risk
2•root-parent•31m ago•2 comments

Paper Tunes – Store Music on Paper and Stream Them Offline

https://www.hackster.io/makestreme/paper-tunes-store-music-on-paper-and-stream-them-offline-8d9099
1•theanonymousone•32m ago•0 comments

ScreenForm – circle anything on your screen, get an AI answer

https://screenform.app
1•Aarush_b•34m ago•0 comments

Rounds of AI telephone with an egg. The word "chicken" lost on round 10

https://twitter.com/geno_spot/status/2089086269087166694
1•goxion•34m ago•0 comments

The Bots Always Find Something

https://faingezicht.com/articles/2026/08/14/the-bots-always-find-something/
2•ohjeez•36m ago•0 comments

Stripe Clinches over $7B Deal to Buy AI Firm OpenRouter

https://www.bloomberg.com/news/articles/2026-08-16/stripe-nears-deal-to-buy-ai-firm-openrouter-fo...
14•zacharyozer•38m ago•7 comments

12-Factor Agents – Principles for building reliable LLM applications

https://github.com/humanlayer/12-factor-agents
2•rzk•39m ago•0 comments

Polyglot monorepos release orchestration: conv commits in, ordered release out

https://yohimik.github.io/dispat/
1•yohimik•41m ago•0 comments

Show HN: A world map where you pay to put your project's link on your country

https://themapchallenge.com/
1•themapchallenge•45m ago•0 comments