frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

I Built an AI Receptionist for a Luxury Mechanic Shop – Part 1

https://www.itsthatlady.dev/blog/building-an-ai-receptionist-for-my-brother/
1•mooreds•44s ago•0 comments

New SVD Algorithm

https://github.com/johsteffens/mocutsvd
1•johsteffens•3m ago•0 comments

Trivy report prior incident part of 'ongoing and evolving attack'

https://www.aquasec.com/blog/trivy-supply-chain-attack-what-you-need-to-know/
1•philipwhiuk•4m ago•1 comments

High-performance JSON library for Elixir via Rustler NIFs, powered by sonic-rs

https://github.com/lpgauth/torque
1•fidotron•4m ago•0 comments

Man designed cancer vaccine for his dog with aid from AI

https://news.unsw.edu.au/en/meet-the-man-who-designed-a-cancer-vaccine-for-his-dog
1•ndtm•6m ago•0 comments

Shadows Shouldn't Stack

https://gist.github.com/chadluo/d5d7782613dc864473f16fbc9cd8d1d4
1•yukirock•7m ago•0 comments

The Problem with Representative Democracy

https://foreignpolicy.com/2026/02/13/helene-landemore-representative-politics-democracy-politicia...
1•mooreds•10m ago•0 comments

Convergence of Wi-Fi and 5G

https://systemsapproach.org/2026/02/23/convergence-of-wi-fi-and-5g/
1•teleforce•11m ago•0 comments

Escaping Nulliverse

https://theelderscripts.com/escaping-nulliverse/
1•haarlemist•12m ago•1 comments

State of the Global Climate 2025

https://wmo.int/publication-series/state-of-global-climate/state-of-global-climate-2025
2•hkhn•14m ago•0 comments

EPP wants to overturn and force new vote for indiscriminate Chat Control

https://digitalcourage.social/@echo_pbreyer/116266597665909346
1•latexr•14m ago•0 comments

Convergence takes off in the datacenter

https://systemsapproach.org/2026/03/23/convergence-takes-off-in-the-datacenter/
1•teleforce•15m ago•0 comments

Westerners are fleeing their countries in record numbers

https://www.economist.com/finance-and-economics/2026/03/22/westerners-are-fleeing-their-countries...
1•cicko•15m ago•0 comments

Migrating to the EU

https://rz01.org/eu-migration/
1•exitnode•15m ago•0 comments

Trepan: Context Drift Catcher

https://github.com/dsadsadsadsadas/Trepan
1•slx44i•17m ago•0 comments

OS Kickstart

https://github.com/dpanic/os-kickstart
2•mandarino•20m ago•0 comments

Show HN: Agent-IM – IM, but for Agents

https://github.com/LinklyAI/agent-im
1•blueeon•21m ago•0 comments

Why using AI to modernize legacy mainframe hairballs is harder than you think

https://www.sage-tech.ai/blog/zsqdg1dddi33t1iq5gwckh2hdgwu4i
1•mooreds•21m ago•0 comments

The new BMW i3 is finally here and comes with 560 miles of range

https://driven.site/news/bmw-i3-second-neue-klasse-model-560-miles-range
3•doener•30m ago•0 comments

ICE Left Scrambling After Being Blindsided by Trump's Airport Decision

https://www.thedailybeast.com/ice-left-scrambling-after-being-blindsided-by-trumps-airport-decisi...
2•doener•35m ago•0 comments

Classical Chinese Jailbreak Prompt Optimization via Bio-Inspired Search

https://arxiv.org/abs/2602.22983
2•limoce•37m ago•0 comments

We Cut Our Deploy Time from 60 Minutes to 15 Minutes

https://getcoherence.io/blog/how-we-cut-deploy-time-75-percent
4•keithfawcett•39m ago•0 comments

AI cut our driver verification team from 20 to 5

https://verumastra.com/blog/ai-driver-verification-ops
2•vadimkoz•41m ago•0 comments

Dash0 raises $110M Series B at $1B valuation

https://www.dash0.com/blog/dash0-raises-usd110m-series-b
10•fred_•43m ago•0 comments

Show HN: AutoResearch with PromptFoo Is AutoPrompter

https://github.com/gauravvij/autoprompter
2•dakshjain_1616•43m ago•0 comments

Show HN: Self-hostable AI agents and internal software

https://github.com/RootCX/RootCX
2•seyz•45m ago•0 comments

Show HN: GladAITor – Judge AI Products for Free

https://glad-ia-tor.com/
3•Enjoyooor•51m ago•1 comments

Show HN: I made a tool for converting text snippets to shareable image

https://snip2img.com
1•wesammikhail•53m ago•0 comments

A terminal-style AI coding agent running on Cloudflare Workers

https://github.com/qaml-ai/pi-worker/tree/main/examples/terminal-agent
2•tosh•57m ago•0 comments

Is the Future of AI Local?

https://tombedor.dev/open-source-models/
1•mpweiher•58m ago•0 comments