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.

Attention span is cooked – I can't even read articles or papers in one sitting

1•JonnaMat•5m ago•0 comments

Physicists observe the 'time reflection' of optical light for the first time

https://phys.org/news/2026-12-physicists-optical-lab.html
1•lucamark•9m ago•0 comments

The relationship between NSA and quantum computers

https://cryptologie.net/posts/nsa-and-quantum/
1•subset•10m ago•0 comments

Checking LemmaScript Contracts Against Quint Protocols

https://midspiral.com/blog/lemmascript-quint-checking-lemmascript-contracts-against-quint-protocols/
1•alex_hirner•12m ago•0 comments

100 years of Bauhaus Dessau – Free printable calendar

https://bauhaus.nirmata.studio/
1•kaizenb•12m ago•0 comments

OpenHands Cloud Security Incident (Dec 3, 2026)

1•tobwen•12m ago•0 comments

NSA, NIST, and post-quantum cryptography

https://blog.cr.yp.to/20220805-nsa.html
1•frostbyte64•12m ago•0 comments

Curl_cffi Tutorial: Impersonate Chrome in Python

https://scrapfly.io/blog/posts/curl-cffi-web-scraping
1•FinnAnderson•13m ago•0 comments

Major Gas Retailers Facing Class Action over AI-Based Price-Fixing Scheme

https://www.classaction.org/news/kalibrate-lawsuit-major-gas-retailers-facing-class-action-over-a...
2•DeepLogin•15m ago•0 comments

OpenAI Lied about ad revenue lol

2•askadityapandey•17m ago•2 comments

Inference Engineering, from Scratch

https://ai-inference-engineering-from-scrat.vercel.app/
1•jonbaer•19m ago•0 comments

Qwen 4 Is Looped, 18.3 pts on LiveCodeBench, 6.7 pts on AIME, Zero Regressions

https://blog.tiltastech.com/projects/looped-qwen4-35b-recurrent-depth/
2•nateb2022•23m ago•0 comments

Deterministic ~0.04ms pre-execution payout firewall for AI agents (MIT)

1•owais_globoo•29m ago•1 comments

GTA VI's lessons for the Chinese gaming industry

https://www.scmp.com/tech/tech-trends/article/3376871/grand-theft-auto-vis-lessons-chinese-gaming...
4•fowler64•33m ago•0 comments

Ask HN: Is there is any offline password manager that use PQC?

2•DeepLogin•38m ago•1 comments

SlimPeak Review: Benefits, Features, Ingredients and Results

https://open.spotify.com/episode/3UsNk9fkFVjRbQp1Hen4Dp
1•Tylerwalter•41m ago•1 comments

Cory Doctorow: Regulating, fast and slow

https://pluralistic.net/2026/12/04/quick-fixes/
4•catab•44m ago•0 comments

Will AI Kill SaaS Like Streaming Disrupted Vinyl Records?

https://neatread.net/will-ai-kill-saas-like-streaming-disrupted-vinyl-records
2•piter691777•47m ago•1 comments

Former St. Helena photographer who took iconic Windows XP photo dies at 84

https://napavalleyregister.com/star/lifestyles/st-helena-charles-chuck-orear-obituary-photography...
1•toomuchtodo•49m ago•1 comments

Congress nears ban on cheap Chinese cars

https://www.usatoday.com/story/cars/technology/2026/12/04/congress-cheap-chinese-car-ban/93073829...
7•thereader12•54m ago•4 comments

I Met Your Change: read a pull request as a story

https://github.com/skynyrd/how-I-met-your-change
2•anilsurmeli•55m ago•0 comments

Pipedream Cybersecurity Incident

https://status.perplexity.com/incidents/01M915PACNW98P5015NZ4NTQHE
5•xishi•58m ago•1 comments

OpenAI hasn't proved P=BQP

https://twitter.com/markchen90/status/2129032404861907422
7•espeed•58m ago•0 comments

Show HN: Roomzine – turn a photo of your room into a redesign magazine

https://roomzine.com/
2•ayarmots•59m ago•0 comments

Show HN: Uikits.tech – 84 open-source UI Skill Packs for AI coding agents

https://uikits.tech
2•vietlinhh02•1h ago•0 comments

Show HN: Pulse – Give your AI harnesses subscription usage context

https://github.com/ashwnn/pulse
2•axa14•1h ago•0 comments

Enable 10Gbp/s on ezcap401 GameDock Extreme Pro capture card on macOS

https://github.com/alex0com/ezcap401-mac
2•alex0com•1h ago•1 comments

SF supe weighing limits on AI labs in indu. districts after new Anthropic lease

https://sfist.com/2026/12/04/sf-leaders-push-to-restrict-ai-labs-in-industrial-districts-after-an...
2•littlexsparkee•1h ago•0 comments

A Poem

https://welcometoannexia.net/a-poem/
1•aprentic•1h ago•0 comments

FreeHand

https://microsoft.github.io/FreeHand/
2•jhack•1h ago•0 comments