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.

Show HN: Plethora – Social platform and SDK around games and interactive content

https://plethora.studio/
1•pratt3000•8m ago•0 comments

Ask HN: Just saw a YC25 job posting front page that looks like a 7-day work week

4•walrus01•12m ago•0 comments

HydroGym: A Reinforcement Learning Platform for Fluid Dynamics [video]

https://www.youtube.com/watch?v=SQrPBk6f0GY
1•abetusk•16m ago•1 comments

Nord Stream sabotage suspect arrested on set of film about the sabotage

https://www.telegraph.co.uk/world-news/2026/08/20/nord-stream-sabotage-suspect-arrested-film-set/
3•cwwc•18m ago•0 comments

America's Gerontocracy Needs a Public Physician

https://www.theatlantic.com/health/2026/08/office-of-medical-adviser/688372/
1•paulpauper•19m ago•0 comments

It's Low-Calorie and Packed with Protein. Is It Healthy?

https://www.theatlantic.com/health/2026/08/ultra-processed-food-healthy-ice-cream/688364/
1•paulpauper•19m ago•0 comments

US Debt-to-GDP Ratio

https://www.us-debt-clock.com/debt-to-gdp
2•NordStreamYacht•20m ago•0 comments

Show HN: Driftless – your Postgres, provably in sync with Stripe

https://github.com/quyumkehinde/driftless
1•quyum•21m ago•0 comments

20% price reduction for GPT 5.6 Sol

https://community.openai.com/t/20-price-reduction-for-gpt-5-6-sol-api-codex-credits-and-chatgpt-w...
2•soheilpro•23m ago•0 comments

OpenAI cuts developer pricing for frontier GPT-5.6 Sol model by more than 20%

https://www.reuters.com/technology/openai-cuts-developer-pricing-frontier-gpt-56-sol-model-by-mor...
5•joshuawright11•29m ago•0 comments

Show HN: Flight Ledger – Track your flights, what they cost, and United status

https://app.flightledger.net
1•voladd•29m ago•0 comments

There's no reason for software to be slow anymore

https://danluu.com/perf-opt/
3•Jach•32m ago•1 comments

Open-source Zen Engine is out of beta

https://docs.gorules.io/changelog
1•jawns•34m ago•0 comments

Initial focus for our partnership with Motorola is a regular non-folding device

https://grapheneos.social/@GrapheneOS/117136278553665985
3•Cider9986•36m ago•0 comments

In 1957, Disney Built Our Friend the Atom. We Now Need Our Friend the AI

https://medium.com/@chipmunkworks/the-disney-solution-why-silicon-valley-needs-to-build-our-frien...
1•treelover•37m ago•0 comments

Lie Detection and Language Models

https://philosophybear.substack.com/p/lie-detection-and-language-models
2•paulpauper•38m ago•0 comments

Plotter Related Algorithms

https://drawingbots.com/algorithms/
1•msurguy•42m ago•1 comments

Show HN: Pricefight – a website leaderboard where everyone starts at the bottom

https://pricefight.lol/
1•adjohu•44m ago•0 comments

Wind energy offers a lifeline for this Kansas rancher

https://vitalsigns.edf.org/story/wind-energy-offers-lifeline-kansas-rancher
1•thunderbong•44m ago•1 comments

Turn any REST API into the shape of another

https://bryanhu.com/shotgun/
1•thatxliner•49m ago•0 comments

Towards Physics of Multimodal Pretraining

https://arxiv.org/abs/2608.05000
1•gmays•50m ago•0 comments

Touareg CR after missile attack [video]

https://www.youtube.com/watch?v=AmCAjVvaqKE
1•debo_•55m ago•0 comments

Fighting Bot Traffic

https://markustenghamn.com/blog/fighting-bot-traffic
1•sukram•57m ago•0 comments

Whiteboard: Claim Your Pixels

https://whiteboard.lol
1•insomnie•59m ago•0 comments

Vibe coding personal apps in mid-2026

https://www.flourish.org/2026/08/personal-apps/
2•birdculture•59m ago•0 comments

The Horse and Sparrow Economic Theory

http://the13thtribe.blogspot.com/2025/03/the-horse-and-sparrow-economic-theory.html
1•thunderbong•1h ago•0 comments

Passive Investing and the Decline of Active Mutual Fund Alpha

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=6902444
1•toomuchtodo•1h ago•1 comments

Underrated Terminal App

https://apps.apple.com/fr/app/onepilot-ssh-shell-terminal/id6759485908
2•elearia•1h ago•0 comments

Show HN: Updating my wedding registry website with AI in 275 commits

https://benhoyt.com/writings/updating-gifty-with-ai/
1•benhoyt•1h ago•0 comments

5 years on, Afghan girls still barred from education under Taliban

https://apnews.com/article/afghanistan-girls-excluded-secondary-education-taliban-618c7f5c7659d43...
5•gmays•1h ago•0 comments