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.

Email-Like Inbox for Agentic Engineering

https://twitter.com/plannotator/status/2108589088139886769
1•ramoz•4m ago•0 comments

Scaling the Burden of Proof to the Cost a Claim Imposes

https://zenodo.org/records/23271710
1•skwasimin•4m ago•1 comments

Show HN: Clanking – Turn your coding agents' activity into a subtle soundscape

https://github.com/chrchr/clanking
1•chrchr•5m ago•0 comments

Anthropic and OpenAI wargaming public and political revolt after AI catastrophe

https://decrypt.co/380621/openai-anthropic-quietly-rehearsing-ai-catastrophe
1•thoughtpeddler•8m ago•0 comments

Faster Python startup with lazy imports

https://lemire.me/blog/2026/10/09/faster-python-startup-with-lazy-imports/
1•chmaynard•12m ago•0 comments

The logarithms of rational numbers have irrationality exponent 2 [pdf]

https://jdb19937.github.io/log-irrationality-measure/logmeasure.pdf
1•jdb1729•14m ago•1 comments

HostMath – Airbnb profit calculator with break-even occupancy and payback

https://hosttmath.ctonew.app/?src=hn
2•Hostmath•17m ago•0 comments

Compiling Rust to readable C with Eurydice

https://lwn.net/Articles/1055211/
1•peter_d_sherman•17m ago•0 comments

YouTube Video Shorts for PC

https://chromewebstore.google.com/detail/youtube-video-shorts-para/egnckcecfjkfefjbpdkhgjgggdhpgina
1•DevFunny•18m ago•0 comments

AI system submits false homicide tip to Philadelphia police

https://www.washingtonpost.com/technology/2026/10/09/ai-system-submits-false-homicide-tip-philade...
2•reaperducer•22m ago•0 comments

Argument from Ignorance

https://en.wikipedia.org/wiki/Argument_from_ignorance
2•suopspaces•22m ago•0 comments

Salesforce: AIForce Is Officially SIForce

https://twitter.com/Benioff/status/2108658674118111442
2•2OEH8eoCRo0•23m ago•1 comments

DNA Typewriter records cell lineage history of mouse: zygote–late organogenesis

https://www.science.org/doi/10.1126/science.ael0508
1•bookofjoe•23m ago•0 comments

The gene-regulatory evolution of the human skeleton

https://www.nature.com/articles/s41586-026-11053-x
1•wslh•26m ago•0 comments

Show HN: Programming Language Wars: a 2d game about language 0xbeef

http://github.avestura.dev/pl-wars/
1•avestura•26m ago•0 comments

A game where you rotate buildings to change gravity

https://store.steampowered.com/app/4590270/Fallgrade/
1•vladkudas•28m ago•0 comments

Jev Playground

https://jevplayground.com
1•RyanShook•31m ago•0 comments

Maybe the Rapture Is Here

https://www.noahpinion.blog/p/maybe-the-rapture-is-already-here
1•nozzlegear•32m ago•0 comments

Building an RL environment and dataset for legal

https://github.com/decover-tech/frontier-legal
1•ravitandon1990•33m ago•0 comments

Rewriting Prime Agent in Rust

https://www.primeintellect.ai/blog/prime-agent-rust
2•piotrgrabowski•40m ago•0 comments

I built a self-contained ship tracker for my girlfriend [video]

https://www.youtube.com/watch?v=lsG5w9Y8AuI
1•mkaic•41m ago•1 comments

How F1 cars collect data in real time [video]

https://www.youtube.com/watch?v=tosJ0j8K31c
2•vsskanth•42m ago•0 comments

Scottish artist marks 100 years of Schrödinger equation with new installation

https://physicsworld.com/a/scottish-artist-marks-100-years-of-schrodinger-equation-with-new-insta...
1•sohkamyung•44m ago•0 comments

Local LLM Inference at Scale with vLLM

https://data4sci.substack.com/p/local-llm-inference-at-scale-with
1•Anon84•46m ago•0 comments

Generalization Dynamics of LM Pre-Training

https://arxiviq.substack.com/p/generalization-dynamics-of-lm-pre
1•porridgeraisin•47m ago•0 comments

Boro: Nvidia's Open-Source Effort for AI-Assisted Linux Kernel Development

https://www.phoronix.com/news/NVIDIA-Boro-Linux-Kernel-AI
1•fratellobigio•47m ago•0 comments

Ask HN: Whats the first command you run on a new server?

1•fragmede•48m ago•0 comments

AI Could Allow for 3-Day Workweeks and Single-Income Households

https://www.forbes.com/sites/siladityaray/2026/10/08/jeff-bezos-claims-ai-could-allow-for-3-day-w...
1•oenton•49m ago•1 comments

Texas Man Sentenced to 30 Years for Moving Zines

https://newrepublic.com/article/213253/brother-sentenced-30-years-moving-box-zines-prairieland
1•convict•51m ago•0 comments

An experiment to glimpse the future Amazon rainforest

https://www.nature.com/immersive/d41586-026-02909-3/index.html
3•sohkamyung•51m ago•0 comments