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.

Final run for the current LHC accelerator (but more to come)

https://www.nikhef.nl/en/news/final-run-for-the-current-lhc-accelerator-but-more-to-come/
2•elashri•10m ago•0 comments

Have politics come for the National Academies of Science?

https://arstechnica.com/science/2026/06/have-politics-finally-come-for-the-national-academies-of-...
1•rbanffy•12m ago•0 comments

Hacking Salesforce Sites with an LLM Agent

https://www.reco.ai/blog/hacking-salesforce-sites-with-an-llm-agent
2•llmacpu•16m ago•0 comments

Why the US economy keeps defying the odds

https://www.bbc.com/news/articles/cwy031el03po
1•ilreb•19m ago•0 comments

Anthropic Is Taking AI Welfare Seriously. I'm Not Sure It Knows What It's Measu

https://www.lesswrong.com/posts/gNtHHCh363xSGJyz3/anthropic-is-taking-ai-welfare-seriously-i-m-no...
2•joozio•29m ago•0 comments

I accidentally hit SOTA on agentic memory by using AI companions

https://graph.coder.company/
2•vignesh_146•30m ago•1 comments

Local Models in Mid-2026

https://coles.codes/posts/local-models-mid-2026
1•colescodes•33m ago•0 comments

Google's Pinpoint is the free research tool you should know about

https://www.fastcompany.com/91558438/googles-pinpoint-is-the-free-research-tool-you-should-know-a...
1•OutOfHere•34m ago•1 comments

Untrusted data in Linux – How Rust is going to save us

https://www.youtube.com/watch?v=Nzmj7K0FNRY
1•tux1968•35m ago•0 comments

One-click, production-like ATProto network for local development and E2E testing

https://github.com/eurosky-social/u-at-proto
1•doener•38m ago•0 comments

Levyer: The Platform. Designed for Freedom

https://levyer.com/
1•doener•39m ago•0 comments

HumanizeHub: A confidential marketplace for humanizing AI content

https://humanizehub.me
1•cocoglare•43m ago•0 comments

While Oracle Will Rake in Big Bucks on AI, Profits Are Hard to Predict

https://www.nextplatform.com/cloud/2026/06/12/while-oracle-will-rake-in-big-bucks-on-ai-profits-a...
1•rbanffy•46m ago•0 comments

Upscaling Space Quest 3 [video]

https://www.youtube.com/watch?v=Zozc1xGuO7Q
1•skibz•49m ago•0 comments

Network service termination for certain Sony Electronics products

https://www.sony.com/electronics/support/articles/00398725
2•croes•50m ago•0 comments

Sygnet – Hash any document with SHA3-512 and declare your AI usage level

https://mysygnet.com
1•Pistachero•52m ago•0 comments

Zuckerberg says Meta made 'mistakes' in AI workforce shift

https://www.reuters.com/business/metas-zuckerberg-admits-mistakes-made-ai-transformation-2026-06-12/
1•jgilias•53m ago•0 comments

Superpowers for Claude, Codex etc.

https://github.com/obra/superpowers
1•wood_spirit•53m ago•0 comments

Nockchain vs. Pearl: The Great Compute War

https://x.com/blocmates/article/2065357097353764868
1•MrBuddyCasino•56m ago•0 comments

A Rant about IPOs

https://dampedspring101.substack.com/p/ipo-rant-everybody-lies
1•rwmj•1h ago•0 comments

Trial of 12mph bike lane speed limit grinds gears of Dutch cyclists

https://www.theguardian.com/world/2026/jun/14/trial-of-12mph-bike-lane-speed-limit-grinds-gears-o...
9•defrost•1h ago•8 comments

Cursed Knowledge

https://obscura.com/cursed-knowledge/
2•Cider9986•1h ago•0 comments

An architectural blueprint to excise MS-DOS device debt (CON, NUL) from Windows

https://feedbackportal.microsoft.com/feedback/idea/dca14930-b767-f111-9b47-6045bdbd0989
1•breakthematrix•1h ago•0 comments

New Brunswick woman sues OpenAI, alleging ChatGPT led to daughter's death

https://www.cbc.ca/news/canada/new-brunswick/sue-open-ai-suicide-chat-gpt-9.7234630
1•uladzislau•1h ago•0 comments

Don't trust large context windows

https://garrit.xyz/posts/2026-05-06-dont-trust-large-context-windows
14•computersuck•1h ago•5 comments

China Is Propping Up the World Economy by Importing Less Oil

https://www.wsj.com/business/energy-oil/china-is-propping-up-the-world-economy-by-importing-a-lot...
6•PankajGhosh•1h ago•0 comments

Introduction to (Multimodal) LLM-as-a-Judge

https://yinghonglan.substack.com/p/introduction-to-multimodal-llm-as
4•rented_mule•1h ago•0 comments

Mining And refining: uranium and plutonium (2024)

https://hackaday.com/2024/04/24/mining-and-refining-uranium-and-plutonium/
2•leonidasrup•1h ago•0 comments

How an astrophysicist uses Codex to help simulate black holes

https://openai.com/index/using-codex-to-simulate-black-holes/
3•gmays•1h ago•0 comments

AI: Surgeon's Assistant or Commodity on a Meter?

https://replicated.wiki/blog/stream.html
2•gritzko•1h ago•0 comments