frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

LLMs versus the Halting Problem: Revisiting Program Termination Prediction

https://www.orensultan.com/llms_halting_problem/
1•matt_d•2m ago•0 comments

Wikipedia Annual Plan – Contribute to the Big Questions

https://meta.wikimedia.org/wiki/Talk:Wikimedia_Foundation_Annual_Plan/2026-2027
1•tonymet•6m ago•0 comments

Elon Musk Merges SpaceX with XAI

https://www.nytimes.com/2026/02/02/business/spacex-xai-deal.html
1•frenchman_in_ny•9m ago•1 comments

Teachers/tutors, how do you do remote coding lessons?

1•Jamaldeen•11m ago•0 comments

Adobe Animate will be discontinued effective March 1, 2026

https://helpx.adobe.com/uk/animate/kb/end-of-life.html
3•g0ld3nrati0•15m ago•2 comments

NYC's mayor will shut down 'gifted and talented' program

https://www.dailymail.co.uk/news/article-15509309/nyc-mayor-zohran-mamdani-axe-gifted-program.html
2•mhb•15m ago•2 comments

Axiom: Hi-perf. C++ tensor lib. NumPy/PyTorch-like API, SIMD,BLAS,Metal support

https://github.com/Frikallo/axiom
1•npalli•16m ago•0 comments

Autism vs. Dating Profile Optimizing. One Winner. Claude Skill

https://github.com/b1rdmania/hinge-profile-optimizer
1•birdmania•16m ago•1 comments

How LSP Works: Building a Grammar Checker LSP from Scratch in Rust

https://www.aroy.sh/posts/lsp-deep-dive/
1•trifiasco•19m ago•0 comments

Benchmarking LLMs for Voice Agent Use Cases

https://www.daily.co/blog/benchmarking-llms-for-voice-agent-use-cases/
1•benlower•21m ago•0 comments

Accelerating our global growth: Waymo raises $16B investment round

https://waymo.com/blog/2026/02/waymo-raises-usd16-billion-investment-round
2•boulos•21m ago•1 comments

Watching Insidious in VR in a small NYC theater

https://partiful.com/e/ydTGBueYzwm19nv4XYrq
1•danielgh7•21m ago•1 comments

We're helping preserve the genetic information of endangered species with AI

https://blog.google/innovation-and-ai/technology/ai/ai-to-preserve-endangered-species/
1•tzury•22m ago•0 comments

Porn stars can survive in the age of AI

https://www.economist.com/business/2026/01/27/how-porn-stars-can-survive-in-the-age-of-ai
2•jdkee•22m ago•1 comments

Msgvault: Fast Gmail search from the author of Pandas

https://wesmckinney.com/blog/announcing-msgvault/
1•wdaher•23m ago•0 comments

A sweat-based sensor may help improve sleep quality

https://medicalxpress.com/news/2026-01-based-sensor-quality.html
1•PaulHoule•24m ago•0 comments

The Tech Elites in the Epstein Files

https://www.wired.com/story/epstein-files-tech-elites-gates-thiel-musk/
3•spenvo•24m ago•0 comments

Turbopuffer TUI

1•ralaruri•25m ago•0 comments

Show HN: MoonLocator – Astronomical library for moon calculations in Java

https://github.com/dgrims3/MoonLocatorLibrary
1•tenahu•25m ago•0 comments

Hiring Head of Eng. For Funded Medicaid-Tech Startup

https://wellfound.com/l/2BZRsS
1•RocklandSystems•26m ago•1 comments

SpaceX acquires xAI, plans 1M satellite constellation to power it

https://arstechnica.com/space/2026/02/spacex-acquires-xai-plans-1-million-satellite-constellation...
3•LorenDB•29m ago•1 comments

GLM-OCR

https://docs.z.ai/guides/vlm/glm-ocr
1•wahnfrieden•30m ago•0 comments

Neil Gaiman Is Innocent – Technopathology

https://technopathology.substack.com/p/neil-gaiman-is-innocent-introduction
5•kreyenborgi•31m ago•0 comments

Musk Mars Merger, Maybe

https://www.bloomberg.com/opinion/newsletters/2026-02-02/musk-mars-merger-maybe
1•pinewurst•31m ago•2 comments

Elon Musk's SpaceX acquiring AI startup xAI ahead of potential IPO

https://www.cnbc.com/2026/02/02/elon-musk-spacex-xai-ipo.html
1•poniko•33m ago•1 comments

xAI Joins SpaceX

https://x.ai/news/xai-joins-spacex
83•beklein•34m ago•4 comments

Show HN: Polymcp and Ollama for Simple Local and Cloud LLM Execution

1•justvugg•36m ago•0 comments

King County, WA eviction levels hit an all-time high, again

https://www.seattletimes.com/business/real-estate/king-county-wa-eviction-levels-hit-an-all-time-...
2•petethomas•37m ago•0 comments

Mamdani Report

https://iamwillwang.com/mamdani-report/
1•wxw•37m ago•0 comments

xAI joins SpaceX

https://www.spacex.com/updates#xai-joins-spacex
62•g-mork•37m ago•180 comments