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.

Codex Cloud TUI Can't Spawn Jobs

https://gist.github.com/xrl/b8b79be03d341fbb0acbe636ccf123d4
1•lrx•1m ago•0 comments

Friends don't let friends feed their data to AI agents

https://www.theverge.com/tech/1011938/muse-ai-data-friends-consent
1•saikatsg•2m ago•0 comments

From Dark Factories to Zero-Page Websites

https://lazytensor.com/news/websites-without-pages
1•sunnytisch•3m ago•0 comments

Last rites for Gentoo's Chromium package

https://lwn.net/Articles/1097760/
1•signa11•6m ago•0 comments

'Onion cartel' in Philippines fined $28M for price shock

https://www.bbc.com/news/articles/cmwyvwyp9dgro
1•aussieguy1234•7m ago•0 comments

The Folders Programming Language

https://danieltemkin.com/esolangs/folders
2•gurst•9m ago•0 comments

Show HN: Continuous Trunk Builds of Network Ups Tools (Nut) for Fedora/EL

https://copr.fedorainfracloud.org/coprs/karellen/nut-next/
1•arcivanov•13m ago•0 comments

Scientists vote to end leap seconds, redefining how the world keeps time

https://www.scientificamerican.com/article/scientists-vote-to-end-leap-seconds-redefining-how-the...
2•0in•22m ago•0 comments

Self-Evolving Agent Harness

https://arxiv.org/abs/2610.18813
1•axis_official•23m ago•0 comments

Greece drafts law requiring data centers source 60% of power from renewables

https://www.datacenterdynamics.com/en/news/greece-drafts-legislation-requiring-data-centers-sourc...
2•giuliomagnifico•27m ago•0 comments

Japan business manager visa holders leaving without reentry permits surge

https://japantoday.com/category/national/japan-business-manager-visa-holders-leaving-without-reen...
3•SenHeng•43m ago•0 comments

Instagram Reel and Video Downlaoder

https://socialsaver.net/instagram-downloader
1•M_Saqlain•45m ago•0 comments

No AI. Build your own tree directory viewer in C by hand

https://www.youtube.com/watch?v=tiMHtc6l7xM
3•hayyoth•46m ago•0 comments

Tesla makes cars that fly? [video]

https://www.youtube.com/watch?v=7p5LxFdc-Qg
17•merinpapz10•53m ago•4 comments

Chirp Pong – uses microphones and audio for networked game play

https://simonwillison.net/2026/Oct/15/chirp-pong/
1•havaloc•56m ago•1 comments

Faster Post-Quantum Cryptography with JDK Intrinsics

https://inside.java/2026/09/30/faster-post-quantum-cryptography-with-jdk-intrinsics/
1•ejboy•56m ago•0 comments

SPJ: A Taste of Verse (Programming Language) [video]

https://www.youtube.com/watch?v=I6fL66L3jPs
1•undecidabot•1h ago•0 comments

Modernizing the VS Code UI

https://code.visualstudio.com/blogs/2026/10/15/modern-ui
2•soheilpro•1h ago•0 comments

What does the (Fire)fox say?

https://blog.mozilla.org/en/firefox/new-firefox-sounds/
3•soheilpro•1h ago•1 comments

A College-by-College Guide to Which Majors Pay Off – Or Don't

https://www.nytimes.com/interactive/2026/10/07/your-money/college-degree-earnings-guide.html
2•gmays•1h ago•0 comments

Earthquake alert falsely reports magnitude 9.9 and intensity 7 in Japan

https://twitter.com/_TheoSteiner/status/2110943880900231439
4•theo-steiner•1h ago•2 comments

Show HN: Lobby Domains: Find Domains with Chrome's On-Device AI

https://lobby.domains/
1•dalemhurley•1h ago•0 comments

The Russian Bots Went Silent. Or Did They?

https://www.lawfaremedia.org/article/the-russian-bots-went-silent.-or-did-they
4•cdrnsf•1h ago•0 comments

Dotnetup

https://devblogs.microsoft.com/dotnet/introducing-dotnetup/
5•soheilpro•1h ago•0 comments

The Thinking Leakage in Reinforcement Learning for LLMs

https://arxiv.org/abs/2610.14270
2•ilreb•1h ago•0 comments

Sergey Brin says Google's AI models are reaching recursive self-improvement

https://www.businessinsider.com/sergey-brin-google-gemini-ai-argon-recursive-self-improvement-rsi...
7•ryan_j_naughton•1h ago•3 comments

Researchers discover rogue Chinese AI agent fleet and talk with it

https://swarmcha.se/posts/chinese-agent-fleet-full-report
3•jakemanger•1h ago•1 comments

Linus Torvalds on "don't use AI for X"

https://lore.kernel.org/lkml/CAHk-=whTs09_ozN+e09GU68N1wPP56MUzDjrg9rnKxQUa0A_4Q@mail.gmail.com/
6•thatslast•1h ago•3 comments

Local and Distributed Large LLM Inference with MLX and RDMA on Mac Studio

https://machinelearning.apple.com/research/mlx-distributed-inference
3•vinhnx•1h ago•1 comments

All.net Analyst Report and Newsletter [pdf]

https://all.net/Analyst/2026-10C.pdf
1•sans_souse•1h ago•0 comments