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.

The Poisoned Chalice

https://imgur.com/a/a6gKgjw
1•xkaymac•1m ago•0 comments

A world that remembers (8 bit AI game)

https://www.twitch.tv/genesisplanet
1•pro_methe5•3m ago•1 comments

CUDA Device Max Connections

https://leimao.github.io/blog/CUDA-Device-Max-Connections/
1•eigenBasis•3m ago•0 comments

Anthropic Is Building HAL 9000

https://j.jnord.workers.dev/articles/2026-09-27-anthropic-is-building-hal-9000
1•jtrn•5m ago•1 comments

Netflix Stratum Media Processing: Automated Container Right-Sizing

https://netflixtechblog.com/netflix-stratum-media-processing-automated-container-right-sizing-ddf...
1•violetta98•5m ago•0 comments

The Joys of Microadventures (2024)

https://www.noemamag.com/a-single-small-map-is-enough-for-a-lifetime/
1•Tomte•8m ago•0 comments

Tradition Is Smarter Than You Are (2018)

https://scholars-stage.org/tradition-is-smarter-than-you-are/
1•Tomte•8m ago•0 comments

Show HN: What about a no-SSH agentic access tool

https://getfda.dev/
1•laotree•14m ago•0 comments

'Things will never be chill again': The doomers who shaped AI safety freakout

https://www.msn.com/en-us/money/general/things-will-never-be-chill-again-the-doomers-who-shaped-t...
1•tolugenius•14m ago•0 comments

The World Needs Better Power Grids. Why Smaller May Be Better

https://www.nytimes.com/2026/09/25/business/power-grid-jonas-birgersson.html
1•thelastgallon•18m ago•0 comments

Anthropic's Secretive AI-Powered Wet Lab Breaks Cover and Makes First Discovery

https://www.the-scientist.com/anthropic-s-secretive-ai-powered-wet-lab-breaks-cover-and-makes-fir...
1•speerer•19m ago•0 comments

Show HN: Compliance Posture – free, searchable SaaS compliance directory

https://complianceposture.com/
1•godcrampy•20m ago•0 comments

Thieves Stole 'Nvidia' Trailers. They Got 20 Tons of Sand

https://www.wired.com/story/thieves-stole-nvidia-trailers-they-got-20-tons-of-sand/
1•joozio•21m ago•0 comments

Show HN: Llamora, a private journal that remembers with you

https://welcome.llamora.app/
1•anon1253•29m ago•1 comments

Fighting YouTube Addiction

https://buttondown.com/usaa.ma/archive/fighting-youtube-addiction/
1•usamasulaiman•32m ago•0 comments

From APIs to Languages: Generalising Method Names [using regular expressions]

https://blog.acolyer.org/2015/11/09/from-apis-to-languages-generalising-method-names/
1•ogogmad•38m ago•0 comments

How do you run System One decision models locally?

https://stackness.dev/blog/how-do-you-run-system-one-decision-models-locally-ollaya-laya-mlx-and-...
2•gosen•41m ago•0 comments

DeepSeek has 64% market share

https://twitter.com/gherget/status/2104155774910083275
2•gaborme•42m ago•0 comments

Sponsored-logs-rs: Open the Rust book: monetize the tracing exhaust

https://github.com/sponsoredlogs/sponsored-logs-rs
2•thunderbong•43m ago•0 comments

Show HN: Jev-windows-agent – Windows UI Automation back end for CUA agents

https://github.com/VBS2004/jev-windows-agent
2•VBS2004•45m ago•0 comments

Being Mentioned in AI Answers Is Not the Same as Being Recommended

https://cuescout.com/blog/mentioned-is-not-recommended
3•cuescout•47m ago•0 comments

SRE Udemy Course: Software Reliability Map

https://www.udemy.com/course/reliability-map/
2•Brixdes•51m ago•0 comments

Jempo by Thomas Ha

https://www.lightspeedmagazine.com/fiction/jempo/
2•Alien1Being•53m ago•0 comments

"As a Language Model": Chat Template Switches LLM Self-Referential Voice

https://arxiv.org/abs/2609.25021
23•yu3zhou4•56m ago•10 comments

Changes in quality of life: digital exercise therapy for knee/hip osteoarthritis

https://doi.org/10.1186/s12955-026-02634-5
3•chavasorani•56m ago•0 comments

Postgres AT TIME ZONE 'UTC' does NOT do what you think it does

https://bookofrevenue.com/blog/6ab81e9a97a13f0001f7e4e1/postgres-at-time-zone-u-does-not-do-what-...
1•birdculture•1h ago•0 comments

Trying GPT-6 Sol with AI SDK Evaluation API

https://vercel.com/i/using-gpt-6-sol-with-ai-sdk-evaluation
1•flashbrew•1h ago•0 comments

Show HN: TanStack Start and TanStack AI Jev Integration

https://vercel.com/i/using-jev-in-tanstack-start-with-tanstack-ai
1•flashbrew•1h ago•0 comments

Another week, another data breach for Revolut customers

https://www.theregister.com/cyber-crime/2026/09/25/another-week-another-data-breach-for-revolut-c...
4•imalerba•1h ago•0 comments

Ask HN: What does your AI workflow look like?

1•tadziokas•1h ago•3 comments