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.

Show HN: Mdc – Orchestrate multi-repo Docker environments with one command

https://github.com/tominaga-h/multi-docker-commander
1•tominaga-h•41s ago•0 comments

Human brain cells on a chip learned to play Doom in a week

https://www.newscientist.com/article/2517389-human-brain-cells-on-a-chip-learned-to-play-doom-in-...
1•urban_winter•1m ago•0 comments

Dario, come to Europe. Here you are allowed to do your job

1•Trickery5837•4m ago•0 comments

Show HN: Can we have Flutter-like portability without the bloated web binaries?

1•io_eric•4m ago•0 comments

Vinext: Vibe-Hacking Cloudflare's Vibe-Coded Next.js Replacement

https://www.hacktron.ai/blog/hacking-cloudflare-vinext
1•zeyu1337•5m ago•0 comments

The Day an AI Company Told The Pentagon to Go F*** Itself

https://defragzone.substack.com/p/the-day-an-ai-company-told-the-pentagon
1•frag•5m ago•0 comments

How to Cancel Claude Code

https://support.claude.com/en/articles/8325617-how-do-i-cancel-my-paid-claude-subscription
1•JasonHEIN•7m ago•0 comments

Alternative Privacy-respecting Web Front-ends to popular online platforms

https://github.com/duyfken/alternative-front-ends
1•Baljhin•11m ago•1 comments

Feature Flags: Power, Risk and Responsibility

http://emanueleminotto.github.io/blog/feature-flags-power-risk-responsabilities
1•EmanueleMinotto•17m ago•0 comments

Show HN: Open Vernacular AI Kit – preprocessing for Indian code-mixed text

https://github.com/SudhirGadhvi/open-vernacular-ai-kit
1•sudhirgadhvi•19m ago•0 comments

Monitor the Situation Dashboard

https://monitor-the-situation.com
1•aiddun•20m ago•0 comments

Show HN: FeatureDrop – Open-source product adoption toolkit

https://featuredrop.dev
1•thegdsks•22m ago•0 comments

Video Conferencing with Postgres

https://planetscale.com/blog/video-conferencing-with-postgres
2•tosh•24m ago•0 comments

Surge in giant sinkholes threatens Turkey's farmers

https://www.theguardian.com/world/2026/feb/28/i-live-in-constant-fear-surge-in-giant-sinkholes-th...
1•FailMore•26m ago•0 comments

Show HN: A VCluster in Docker with Terraform and Istio

https://github.com/madduci/vind-with-mesh
1•madduci•31m ago•0 comments

Show HN: Colored Title Bar – unique colors per VS Code workspace

https://github.com/wstuckey/colored-title-bar
2•wstuckey•32m ago•0 comments

Echoslate – Offline todo and Kanban tracker for programmers (MIT, .NET 8)

https://github.com/pjmavcom/echoslate
1•pjmavcom•33m ago•0 comments

Show HN: Bridge your Claude/OpenAI subs into a team API with per-key cost caps

https://github.com/Shreyas-Dayal/ai-cli-bridge
1•shreyas8•33m ago•1 comments

FounderSpace – AI-guided startup validation in 30 minutes

https://www.founderspace.work
1•VladCovaci•36m ago•1 comments

P5.lcd

https://p5.lcd.tulv.in/
1•atulvi•41m ago•0 comments

US and Israel carrying out strikes against Iran

https://www.cnn.com/world/live-news/israel-iran-attack-02-28-26-hnk-intl
75•simonebrunozzi•44m ago•57 comments

The March of Nines

https://kk.org/thetechnium/the-march-of-nines/
1•ahmaman•45m ago•0 comments

Song about afterlife from AI perspective [video]

https://www.youtube.com/shorts/DZGvrqNP7lg
1•modinfo•45m ago•0 comments

Show HN: TapPause – A dead-simple timer for intentional breaks

https://apps.apple.com/hk/app/tappause-pause-timer/id6758304453
1•louischen•46m ago•1 comments

I built a startup game because I wanted one to play

https://capturn.io
3•disobae•46m ago•1 comments

U.S. and Israel Conduct Strikes on Iran

https://www.nytimes.com/live/2026/02/28/world/iran-strikes-trump
60•gammarator•46m ago•6 comments

Japan aims for world first in space-based solar power

https://www.asahi.com/ajw/articles/16293144
2•helsinkiandrew•48m ago•0 comments

Simulation for Agentic Evaluation

https://yortuc.com/posts/simulation-for-agentic-evaluation/
2•xyortuc•49m ago•0 comments

Israel says it has attacked Iran, declares state of emergency

https://www.cnbc.com/2026/02/28/trump-us-military-iran-strikes-middle-east-oil.html
25•LordAtlas•52m ago•9 comments

Show HN: PicShift – Convert images in the browser using WebAssembly

https://picshift.app/
1•pod4g•53m ago•1 comments