frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Sequel-privacy • enforce privacy policies on objects, fields and associations

https://github.com/arbales/sequel-privacy
1•arbales•1m ago•1 comments

The New OpenRockets Forum, a Fork of Hacker News Is Coming Soon

https://forum.openrockets.com/
1•openrockets•2m ago•0 comments

Adobe Skills

https://github.com/adobe/skills
1•stephenhandley•3m ago•0 comments

Show HN: GeoTraceroute – Traceroutes on a 3D globe and submarine cables

https://geotraceroute.com
1•Himred•4m ago•0 comments

Texas Instruments made a new flagship graphing calculator

https://www.engadget.com/mobile/texas-instruments-made-a-new-flagship-graphing-calculator-the-ti-...
1•y1n0•9m ago•0 comments

AI Designs Thermoelectric Generators 10k Times Faster Than We Can

https://spectrum.ieee.org/ai-designed-thermoelectric-generator
1•pseudolus•9m ago•0 comments

The future of software development has less development

https://www.theregister.com/2026/04/28/software_development_ai_dev25xsf/
1•y1n0•11m ago•0 comments

Understanding how oxygen is delivered to tissues at the microscopic level

https://phys.org/news/2026-04-oxygen-tissues-microscopic.html
1•y1n0•12m ago•0 comments

When the Internet Was a Place

https://www.frontporchrepublic.com/2025/09/when-the-internet-was-a-place/
1•herbertl•12m ago•0 comments

U.S. Wants to Ban China's High-Tech Cars, but They're Already Here in El Paso

https://www.wsj.com/business/autos/chinese-cars-byd-geely-u-s-mexico-be0dea28
1•bcaulfield•13m ago•0 comments

CA will give DL info to a database that can be used for online age verification

https://www.latimes.com/california/story/2026-04-28/california-to-share-data-on-immigrant-drivers...
1•another_comment•15m ago•1 comments

Elephants, Goldfish and the New Golden Age of Software Engineering

https://drensin.medium.com/elephants-goldfish-and-the-new-golden-age-of-software-engineering-c336...
1•decimalenough•16m ago•0 comments

Naval Ravikant: A Return to Code [video]

https://www.youtube.com/watch?v=hTdSU7q5WCo
1•rglover•20m ago•0 comments

From Skills to Talent: Organising Heterogeneous Agents as a Company [pdf]

https://arxiv.org/abs/2604.22446
2•SerCe•23m ago•0 comments

Why Wealthy Elites Come to Regret Their Bargains with Authoritarians

https://www.foreignaffairs.com/united-states/disposable-oligarchs
1•pseudolus•26m ago•0 comments

Synthetic.new – Limits and Pricing

https://organic.eris.host/limits
1•sea-gold•30m ago•1 comments

Show HN: 20-endpoint image API (remove bg, upscale, face restore)

https://github.com/useknockout/api
1•tlorents•32m ago•0 comments

A Git merge with 100k parents

https://github.com/cirosantilli/test-octopus-100k/commit/07fdcceb20ac3626a07c08166d0c410707b1cb9b
1•nvahalik•36m ago•0 comments

How dating app algorithms (likely) work in 2026

https://nsokolsky.substack.com/p/how-dating-app-algorithms-likely
1•nsokolsky•41m ago•1 comments

Higher temperatures spur Alaska's invasive pike to eat more, bad sign for salmon

https://alaskabeacon.com/2026/04/28/higher-temperatures-spur-alaskas-invasive-pike-to-eat-more-a-...
1•rolph•41m ago•0 comments

Epigenetic fingerprints link early-onset colon&rectal cancer pesticide exposure

https://www.nature.com/articles/s41591-026-04342-5
4•bookofjoe•41m ago•0 comments

An Explicit Solution to Black-Scholes Implied Volatility

https://arxiv.org/abs/2604.24480
1•efavdb•42m ago•0 comments

OpenAI Wants Codex to Shut Up About Goblins

https://www.wired.com/story/openai-really-wants-codex-to-shut-up-about-goblins/
5•spenvo•44m ago•0 comments

Chinese SUVs at Beijing Auto Show [video]

https://www.youtube.com/watch?v=rFcVGeIZk3k
1•dzonga•45m ago•0 comments

Fuck Off AI Music

http://fuckoffaimusic.com/
6•marvinborner•45m ago•0 comments

The New Teams CLI

https://microsoft.github.io/teams-sdk/blog/teams-cli-preview/
2•umangsehgal93•45m ago•0 comments

From One AI to Any AI: JetBrains rethinks the approach to AI tooling [video]

https://www.youtube.com/watch?v=J8fHU4WFd_c
1•lemming•46m ago•0 comments

We decreased our LLM costs with Opus

https://www.mendral.com/blog/frontier-model-lower-costs
21•shad42•52m ago•0 comments

Agent, Know Thyself (and bid accordingly)

https://www.strangeloopcanon.com/p/agent-know-thyself-and-bid-accordingly
1•paulpauper•55m ago•0 comments

AI Killed the MVP. What's Next?

https://www.indiehackers.com/post/ai-killed-the-mvp-whats-next-JIWTsjBRFuC4K87voEFk
1•toddh•56m ago•0 comments