frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Reverse Engineering Bluetooth on Amazon Kindle EReaders

https://sighery.com/posts/reverse-engineering-bluetooth-on-kindle-ereaders/
1•mattmar96•1m ago•0 comments

Frontier Models are Capable of In-context Scheming

https://arxiv.org/abs/2412.04984
1•william-evans•1m ago•0 comments

The Golden Rule of Driving

https://leroy.works/articles/the-golden-rule-of-driving/
1•leroy-is-here•1m ago•0 comments

Where Are the Beautiful Cities?

https://twitter.com/david_perell/status/2005730447897055414
1•lleims•3m ago•0 comments

Google Home Users Are Trying to Hack Their Way to a Better Voice Assistant

https://gizmodo.com/google-home-users-are-trying-to-hack-their-way-to-a-better-voice-assistant-20...
1•gnabgib•4m ago•0 comments

Cyberattack disrupts France's postal service and banking during Christmas rush

https://apnews.com/article/france-postal-service-cyberattack-4ea0c3e3bcb8a87341de8aebc1dfc916
3•gnabgib•6m ago•0 comments

Sprites: Persistent, suspendable, Linux environments as a Service

https://sprites.dev
1•nateb2022•9m ago•0 comments

The former Chinese police officer bringing bubble tea to wartorn Ukraine

https://www.theguardian.com/world/2025/dec/30/former-chinese-police-officer-bringing-bubble-tea-t...
1•mykowebhn•11m ago•0 comments

Enterprises Can Navigate Geolocation, Storage, and Privacy Compliance

https://guptadeepak.com/the-global-data-residency-crisis-how-enterprises-can-navigate-geolocation...
1•guptadeepak•13m ago•1 comments

India has surpassed Japan to become the fourth-largest economy

https://www.dw.com/en/india-overtakes-japan-as-4th-largest-economy-report-says/a-75341063
5•guptadeepak•15m ago•1 comments

ESPectre Sensor: open-source motion detection system for ESP32

https://espectre.dev/
3•882542F3884314B•16m ago•0 comments

Tatiana Schlossberg Has Died

https://www.nytimes.com/2025/12/30/us/politics/tatiana-schlossberg-dead.html
1•HR01•18m ago•0 comments

Painting with light in WebGL: terrain builder in the browser

https://medium.com/@bartoszu/painting-with-light-building-a-3d-island-in-the-browser-with-three-j...
2•bartoszu_•18m ago•0 comments

New brain implant restores lost senses using light

https://newatlas.com/medical-devices/neuro-key-implant-restore-lost-senses/
2•thunderbong•19m ago•0 comments

Show HN: Line Weaver – Image to G-Code Conversion for Pen Plotters

https://github.com/straczowski/line-weaver
1•rstraczowski•20m ago•0 comments

Junkyard Nissan V8 Lays Down Nearly 700 WHP with a Turbo and Little Else

https://www.thedrive.com/news/junkyard-nissan-v8-lays-down-nearly-700-whp-with-a-turbo-and-little...
1•PaulHoule•20m ago•0 comments

Prof. Software Developers Don't Vibe, They Control: AI Agent Coding Use in 2025

https://arxiv.org/abs/2512.14012
5•dpflan•21m ago•0 comments

Everything as Code: How We Manage Our Company in One Monorepo

https://www.kasava.dev/blog/everything-as-code-monorepo
21•benbeingbin•22m ago•9 comments

Show HN: Git-aware File tree viewer for Jupyter [video]

https://www.youtube.com/watch?v=zaK-EZd0GCY
1•loa_observer•22m ago•0 comments

Billionaire Superyachts in St. Barts for New Year's Eve

https://www.forbes.com/sites/jimdobson/2025/12/16/inside-the-billionaire-superyacht-rush-whos-cru...
1•walterbell•23m ago•0 comments

Show HN: Emergence (I asked Claude to impress me)

https://dwyer.co.za/static/claude.html
1•sixhobbits•24m ago•0 comments

Social Media Posts Predict Unemployment Spikes Two Weeks Early

https://scienceblog.com/social-media-posts-predict-unemployment-spikes-two-weeks-early/
3•geox•25m ago•0 comments

The sword-wielding man hired to kick squatters out of empty Oakland homes

https://oaklandside.org/2025/09/30/asap-squatter-removal-oakland/
1•randycupertino•25m ago•0 comments

Flock and Urban Surveillance

https://computer.rip/2025-12-26-Flock-and-Urban-Surveillance.html
2•kelnos•27m ago•0 comments

Soldr, an open-source compact soldering station

https://www.soldr.pro/
1•unwind•28m ago•0 comments

Ask HN: What's your dock setup for your work laptop?

1•charliebwrites•29m ago•1 comments

Amtrak: A Year of Records

https://media.amtrak.com/2025/11/amtrak-a-year-of-records/
2•eatonphil•29m ago•0 comments

Zpdf: PDF text extraction in Zig – 5x faster than MuPDF

https://github.com/Lulzx/zpdf
2•lulzx•30m ago•1 comments

The $100M Mistake: Microsoft PhotoDraw

https://www.youtube.com/watch?v=bGmXCb-irJ0
1•bane•31m ago•0 comments

Desktop Classic System – Spacial computing hearkening back to classic Mac OS

https://mycophobia.org/dcs/
3•todsacerdoti•33m ago•0 comments