frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Gemma Needs Help

https://www.lesswrong.com/posts/kjnQj6YujgeMN9Erq/gemma-needs-help
1•pr337h4m•52s ago•0 comments

Ask HN: How are people forecasting AI API costs for agent workflows?

2•Barathkanna•9m ago•0 comments

Numerical Integration on HP Calculators

https://axiomelab.gitlab.io/hp-integral/article_hp_en.html
2•Tomte•12m ago•0 comments

For the Love of God, Shut Up About Microtubules

https://mvaleadvocate.substack.com/p/for-the-love-of-god-shut-up-about
1•jrosenblatt•16m ago•0 comments

AI detects case of breast cancer that slipped past doctors

https://www.thetimes.com/uk/healthcare/article/breast-cancer-detection-ai-nhs-zb9qcljr3
1•petethomas•20m ago•0 comments

The Growth Tax in Australia

https://zencapital.substack.com/p/the-growth-tax-payroll-tax-in-australia
1•zenincognito•21m ago•0 comments

Why the US Should Ratify the Congressional Apportionment Amendment

https://www.astralcodexten.com/p/last-rights
2•colonCapitalDee•22m ago•0 comments

Every minute you aren't running 69 agents, you are falling behind

https://geohot.github.io//blog/jekyll/update/2026/03/11/running-69-agents.html
3•ppew•30m ago•0 comments

Cursor Best Practices Skill by Hktitan

https://skills.sh/hktitan/cursor-best-practices/cursor-best-practices
1•HarshitKhemani•32m ago•1 comments

TADA: Fast, Reliable Speech Generation Through Text-Acoustic Synchronization

https://www.hume.ai/blog/opensource-tada
2•smusamashah•33m ago•0 comments

#1 Trending on PeerPush – BasaltSurge

https://peerpush.net/p/basaltsurge
1•mayordelmar•35m ago•0 comments

A writing space that breathes with you

https://vessel.here.now/
1•barnira•37m ago•0 comments

Breaking Down the Jelly Slider

https://blog.swmansion.com/breaking-down-the-jelly-slider-9ab9239f6d80
1•nsainsbury•43m ago•1 comments

I should quit my job and become a goat farmer

https://www.goatops.com/
1•microsoftedging•43m ago•0 comments

Show HN: Self-hosted DCF workspace using Damodaran datasets, LLM narratives

1•softcane•43m ago•0 comments

MacBook Neo Is a 'Shock' to the PC Industry: Asus Co-CEO

https://www.pcmag.com/news/asus-co-ceo-macbook-neo-is-a-shock-to-the-pc-industry
5•jithinraj•45m ago•0 comments

We Build Our Own Decentralized DNS for AnChat – Here's Why

https://twitter.com/DeBrosOfficial/status/2031602104666673389
1•debros•49m ago•0 comments

China's first moon astronauts could land in Rimae Bode

https://www.space.com/astronomy/moon/china-first-astronaut-moon-landing-mission-rimae-bode
3•jithinraj•50m ago•0 comments

How Wikipedia Portrayed Humanity in a Single Photo (2018)

https://www.wired.com/story/how-wikipedia-portrayed-humanity-in-a-single-photo/
2•icwtyjj•51m ago•1 comments

OWASP Top Agents and AI Vulnerabilities

https://blog.alexewerlof.com/p/owasp-top-10-ai-llm-agents
1•weltview•54m ago•0 comments

24-year-old ditched her smartphone and social media known as 'appstinent'

https://www.cnbc.com/2026/03/10/a-24-year-old-who-ditched-her-smartphone-and-social-media-wants-y...
5•1vuio0pswjnm7•59m ago•0 comments

AI Aadhaar OCR API for Fast Aadhaar Data Extraction

1•azapi_ai•1h ago•0 comments

Meta to charge advertisers a fee to offset Europe's digital taxes

https://www.reuters.com/business/media-telecom/meta-charge-advertisers-fee-offset-europes-digital...
2•1vuio0pswjnm7•1h ago•0 comments

We built NPM for agent knowledge – Context Packs on Armalo (update)

https://www.armalo.ai/
1•ArmaloAI•1h ago•1 comments

David Woo: The Market Is Wrong About Iran, Oil and What Comes Next [video]

https://www.youtube.com/watch?v=W2Z6UVv_wl4
1•thomassmith65•1h ago•1 comments

Why Ads in Chatbots May Not Click

https://www.wsj.com/tech/ai/why-ads-in-chatbots-may-not-click-2ca52203
2•1vuio0pswjnm7•1h ago•0 comments

Neuromorphic sphere topology Hebbian learning as a path to grounded intelligence

1•rusanovych•1h ago•0 comments

Intent-Driven Development

https://jason-stillwell.com/blog/2026/03/10/intent-driven-development/
3•dragonfax•1h ago•3 comments

Long/short extensions diversify concentrated stock tax-neutrally (2025)

https://www.blackrock.com/us/financial-professionals/insights/diversify-with-long-short
1•vismit2000•1h ago•0 comments

Made my first Chrome extension: Gmail Feed (from a non-technical background)

https://chromewebstore.google.com/detail/gmail-feed/npahlccednamnmabhaiifdmioibcmilg
3•adarshkaraka•1h ago•1 comments