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.

Possible invasive species superspreader event near Strait of Hormuz

https://abcnews.com/International/scientists-warn-invasive-species-superspreader-event-due-stalli...
1•russfink•2m ago•0 comments

Image Processing Handbook

https://image-processing-handbook.github.io/
1•javatuts•6m ago•0 comments

Show HN: Axtary – Content Authorization for AI Agents

https://axtary.com
1•Axtary•13m ago•0 comments

What "privacy-first" means when you connect your bank account

https://www.aurumapp.org/blog/what-privacy-first-finance-means
1•jbrown7832•14m ago•0 comments

Show HN: Argus – VSCode Worktree Agent Session Manager

https://marketplace.visualstudio.com/items?itemName=petar-s-dimov.argus-worktree-agent-sessions
1•pdcd•17m ago•0 comments

I scanned my AI agent framework for destructive/consequential actions, and wow

https://www.actenon.com/
1•Bucko1•17m ago•0 comments

The South Carolina town in turmoil over a $545,000 email phishing scam

https://www.msn.com/en-us/news/other/the-south-carolina-town-in-turmoil-over-a-545-000-email-phis...
1•MilnerRoute•18m ago•0 comments

Google Maps Platform adds Isochrones API for travel-time reachability polygons

https://developers.google.com/maps/documentation/isochrones/overview
3•882542F3884314B•25m ago•0 comments

Collective Intelligence: The Next Frontier of AI

https://github.com/ailinone/collective-intelligence
1•alissonidalo•25m ago•0 comments

Show HN: QuickFlo – I got sick of exporting business workflows as a consultant

https://quickflo.app/
1•zajasa•29m ago•1 comments

Journal42: Journaling for Tech Workers

https://journal42.cloud/
1•rtpro•31m ago•0 comments

Phaistos Disc

https://en.wikipedia.org/wiki/Phaistos_Disc
4•hypertexthero•36m ago•0 comments

Git rebase -I is not that scary

https://cachebag.sh/journal/interactive-rebasing/
3•vinhnx•37m ago•0 comments

Everything Is a File

https://en.wikipedia.org/wiki/Everything_is_a_file
4•iacguy•37m ago•1 comments

Systems and Delays

https://martin.janiczek.cz/2026/07/24/systems-and-delays.html
3•vinhnx•38m ago•1 comments

Inflect-Micro-v2: complete voice in 9.36M parameters

https://huggingface.co/owensong/Inflect-Micro-v2
2•nateb2022•39m ago•0 comments

Zig by Example

https://zigbyexample.neocities.org/
2•vinhnx•39m ago•0 comments

Open-Weight Déjà Vu

https://peteridah.substack.com/p/open-weight-deja-vu
1•peteridah•41m ago•0 comments

SEC acquiring AI agents to monitor phone locations, social media, credit headers

https://jackpoulson.substack.com/p/securities-and-exchange-commission
2•iamnothere•43m ago•0 comments

PGSync adds semantic search without data leaving your infrastructure

https://pgsync.com/pro/
1•toluaina•44m ago•0 comments

George Hotz talk at AMD Advancing AI 2026 [video]

https://www.youtube.com/watch?v=QyrQeeDZUG4
3•chrsw•54m ago•0 comments

GNU Hurd gets 9pfs, OpenNTPD, dynamic /dev/ entries, and more

https://www.osnews.com/story/145603/the-hurd-gets-9pfs-openntpd-dynamic-dev-entries-and-more/
18•iamnothere•56m ago•4 comments

Personal AI Assistant, Anywhere

https://www.heysolin.com/
1•GreenKeewi•58m ago•0 comments

The real mathematics is the one that we dream – David Bessis

https://davidbessis.substack.com/p/the-real-mathematics-is-the-one-that
2•lioeters•1h ago•0 comments

Silicon Valley Splits over Closing the Borders to Chinese A.I

https://www.nytimes.com/2026/07/25/technology/open-source-silicon-valley-china.html
5•dkobia•1h ago•1 comments

Nepo – the largest database of nepotism and nepo babies

https://www.nepodb.com/
4•poppypetalmask•1h ago•1 comments

Triton: DirectX 11 driver for QEMU

https://blog.getutm.app/2026/introducing-triton-directx-11-driver-for-qemu/
1•generichuman•1h ago•0 comments

Show HN: Houhou – Resilience Policies Package for TypeScript Async Functions

https://hou2.vercel.app/
2•smokeeaasd•1h ago•0 comments

Stinkpot: SQLite-Backed Shell History

https://tangled.org/oppi.li/stinkpot
2•birdculture•1h ago•0 comments

Claude Usage Public Leaderboard

https://www.claudeusage.com/leaderboard
2•bazarkua•1h ago•0 comments