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.

What Challenging a Bowling Monopoly Says About America

https://www.thebignewsletter.com/p/monopoly-round-up-what-challenging
1•connor11528•17s ago•0 comments

Counterfactual samples synthesizing for mitigating hallucination in LLMs

https://pubmed.ncbi.nlm.nih.gov/41729914/
1•fragmede•5m ago•1 comments

Tashk – a todo manager written in pure bash

https://github.com/agamoaltrove/tashk
1•agamoaltrove•8m ago•0 comments

The lab where Ford is trying to crack the code on cheap EVs

https://www.theverge.com/transportation/923704/ford-ev-skunkworks-lab-long-beach
2•dkobia•8m ago•0 comments

Elon Musk and Tim Cook among CEOs expected to accompany Trump on China trip

https://www.bbc.com/news/articles/c5yx757w048o
2•bigyabai•11m ago•0 comments

Tensor Shapes

https://pyrefly.org/en/docs/tensor-shapes/
1•rexledesma•11m ago•0 comments

French woman was told by doctors hantavirus symptoms were just anxiety

https://www.theguardian.com/world/2026/may/11/french-womandoctors-hantavirus-symptoms-hv-hondius
3•cdrnsf•12m ago•0 comments

OpenDyslexic: A Typeface for Dyslexia

https://opendyslexic.org/
1•oldfuture•12m ago•0 comments

Cloud Storage Rapid: Turbocharged object storage for AI and analytics

https://cloud.google.com/blog/products/storage-data-transfer/cloud-storage-rapid-turbocharges-obj...
1•leg•15m ago•0 comments

My First AI Project: An "Evidence-Based" System Architect. Looking for Feedback

https://github.com/maioio/genesis-architect
1•maioio•15m ago•0 comments

Surprise X-Ray Discovery May Explain Red Dots in Early Universe

https://www.sciencealert.com/surprise-x-ray-discovery-may-explain-strange-red-dots-in-early-universe
2•smooke•15m ago•0 comments

Swatch and Audemars Piguet Are Planning a Collaboration Watch

https://www.hodinkee.com/articles/its-official-swatch-and-audemars-piguet-are-planning-a-collabor...
1•nstj•19m ago•0 comments

Lawsuit accuses ChatGPT of helping gunman plan FSU mass shooting

https://www.pbs.org/newshour/nation/lawsuit-accuses-chatgpt-of-helping-gunman-plan-fsu-mass-shooting
5•anigbrowl•21m ago•0 comments

Childhood Computing

https://lilysthings.org/blog/childhood-computing/
1•birdculture•25m ago•0 comments

Olive CSS: Lisp powered vanilla CSS utility-class a la Tailwind

https://codeberg.org/jjba23/olive-css
2•PaulHoule•26m ago•0 comments

Gamingshuvo

https://blog.cloudflare.com/post-quantum-warp/
1•Gamingshuvo•27m ago•0 comments

Family of Florida mass shooting victim sues OpenAI in US court

https://www.reuters.com/legal/government/family-florida-mass-shooting-victim-sues-openai-us-court...
2•tartoran•30m ago•0 comments

Tech Companies Fail to Kill Colorado's 'Right to Repair' Law

https://www.techdirt.com/2026/05/11/tech-companies-fail-to-kill-colorados-right-to-repair-law/
3•cdrnsf•31m ago•0 comments

All Roads Lead to AI Rome

https://medium.com/towards-artificial-intelligence/all-roads-lead-to-ai-rome-1c601f1ec440
1•vektormemory•31m ago•1 comments

Tesla recalls every Cybertruck RWD ever sold because the wheels could fall off

https://finance.yahoo.com/markets/stocks/articles/tesla-recalls-every-cybertruck-rwd-140500699.html
5•cdrnsf•32m ago•0 comments

Day After Grizzly Attack, Dimwitted Tourist Takes Video of Bear from 5 Feet Away

https://cowboystatedaily.com/2026/05/08/day-after-grizzly-attack-dimwitted-tourist-takes-video-of...
3•Bender•32m ago•0 comments

People Who Don't Like People Are Making All of Our Decisions

https://www.theatlantic.com/ideas/2026/05/waymo-self-driving-cars/687119/
3•bentaber•33m ago•1 comments

Griffin PowerMate driver for modern macOS

https://github.com/jameslockman/Griffin-PowerMate-Driver
2•classichasclass•34m ago•0 comments

Making your developer platform agent-ready

https://hookdeck.com/blog/developer-platform-agent-ready
1•mooreds•34m ago•0 comments

Official PCIe 8.0 draft aims for 1 TB/s data rate

https://www.theregister.com/storage/2026/05/07/official-pcie-80-draft-aims-for-1-tb/s-data-rate/5...
1•Bender•36m ago•0 comments

What's in the 2026 Hugo Awards Voter Packet?

https://compellingsciencefiction.com/posts/what-s-in-the-2026-hugo-awards-voter-packet.html
1•mojoe•37m ago•0 comments

Essays on Dwarkesh's "Big Questions on AI"

https://12gramsofcarbon.com/p/why-is-ai-still-scaling-how-do-the
1•theahura•40m ago•0 comments

Sovereign cloud is only possible if you're Chinese or American: Gartner

https://www.theregister.com/off-prem/2026/05/11/sovereign-cloud-is-only-possible-if-youre-chinese...
2•Bender•41m ago•0 comments

Byron Allen to Buy a Controlling Stake in BuzzFeed

https://www.nytimes.com/2026/05/11/business/media/buzzfeed-byron-allen.html
2•jaredwiener•42m ago•0 comments

Coloradan can plug his brain into a computer

https://www.denverpost.com/2026/05/10/colorado-brain-computer-interface/
1•rolph•43m ago•0 comments