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.

OpenAI $500 ProMax plan listed in API

1•GustavHartz•38s ago•0 comments

Musk – Official Trailer – Bleecker Street [video]

https://www.youtube.com/watch?v=eY_8SyndC10
1•vintagedave•2m ago•0 comments

Steppy Fox Browser Game

https://steppyfox.com/
1•fadilbeg•3m ago•0 comments

Legal AI Harvey's margins went from +50% to -50% in 6 months on frontier costs

https://finance.yahoo.com/technology/ai/articles/openai-anthropic-costs-push-more-160523322.html
1•joennlae•4m ago•1 comments

Show HN: IGViewer – Browse public Instagram profiles and stories anonymously

https://www.igrecent.com
1•sx2026•4m ago•0 comments

Real-Time AI Simplification (A2, B1, B2) of BBC World News Live

https://voicefrom.ai/restate/
1•BrandiATMuhkuh•6m ago•0 comments

My First Language Front End with MLIR

https://whereisalan.dev/blog/kaleidoscope-mlir-tutorial/
1•alankarmisra•6m ago•0 comments

Dutch governments builds alternative for Microsoft based on NixOS

https://www.dawo.community/en/
1•fjfaase•6m ago•0 comments

You Cry [video]

https://www.youtube.com/watch?v=rLvUztp7Iog
1•mrroryflint•9m ago•0 comments

Your Language Model Is Already a Decision Model

https://github.com/ntlm1686/Your-language-model-is-already-a-decision-model/tree/master
1•ntlm1686•10m ago•0 comments

Dating a takeoff photo to the second using runway markings as a ruler

https://blog.spuria.app/writeups/angle-of-ascent-billund-klm-takeoff/
1•lost_•17m ago•0 comments

Show HN: Can an AI agent bypass a post-quantum signed authorization policy?

https://github.com/max-russo-com/MAX_AUTHORIZATION_SANDBOX
1•max-russo•18m ago•0 comments

Coming to terms with the end of the Falcon 9

https://www.thespacereview.com/article/5319/1
1•ssernikk•19m ago•0 comments

OpenAI prepares new $500/month Pro Max plan for ChatGPT

https://www.testingcatalog.com/openai-prepares-new-500-month-pro-max-plan-for-chatgpt/
3•yzydserd•21m ago•0 comments

Show HN: Foveon Lab – Compare Sigma Photo Pro Presets on Foveon X3F Files

https://github.com/biopoetic/foveon-lab
1•netless•24m ago•0 comments

Ask HN: Migrating personal email domain from Gmail to iCloud?

1•osener•25m ago•0 comments

Deep Fission ($FISN): A Money Pit

https://iceberg-research.com/2026/09/24/deep-fission-fisn-a-money-pit/
1•ViewTrick1002•26m ago•0 comments

Show HN: Tasket++ Simulate your Windows actions automatically-free and OS

https://apps.microsoft.com/detail/xp9cjlhwvxs49p?hl=en-US&gl=US
1•AmirHammoutene•27m ago•0 comments

Tesla 'FSD' sped in 55% of Brussels 30 km/h zones ahead of EU vote

https://electrek.co/2026/09/24/tesla-fsd-belgium-test-speeding-30-kmh-eu-vote/
4•BaudouinVH•32m ago•0 comments

Driver Misconduct in Last-Mile Delivery

https://arxiv.org/abs/2609.29080
1•50kIters•34m ago•0 comments

Quiet-guardrails: Claude Code manual mode, minus the nagging

https://github.com/SeanMcMillan/quiet-guardrails
1•northbridgedev•37m ago•0 comments

YC rejected it, so we're open-sourcing it

https://github.com/orangewood-co/inboundr
3•tushgaurav•39m ago•3 comments

The Subtle Art of Normalising Dystopia [video]

https://www.youtube.com/watch?v=aLPrNSFLb2U
2•dvrp•39m ago•0 comments

Amiga Screens: A Primer

https://www.datagubbe.se/amscr/
4•msephton•41m ago•0 comments

Implicit Neural Representations with Periodic Activation Functions (2020)

https://arxiv.org/abs/2006.09661
2•peter_d_sherman•43m ago•1 comments

F1 Start Timer

https://f1-start.jakearchibald.com/
1•tosh•43m ago•0 comments

Pentium II at 600Mhz with Voodoo 3 Emulated on 86Box with M6 Mac Mini

https://nyaa.sh/reviews/mac-mini-m6-emulation
2•hugh4life•45m ago•0 comments

Show HN: Entail – what your model files declare vs. what the engine runs

https://github.com/wwoosshh/Entail
1•wwoosshh•46m ago•0 comments

Pico_exporter: System metrics exporter that runs in about 120 kiB of memory

https://github.com/dpaneda/pico_exporter
1•elenathor•46m ago•1 comments

Show HN: TiddlyInstall – A single HTML file that makes cross-platform installers

https://robertsdotpm.github.io/_static/tiddlyinstall.html
1•Uptrenda•48m ago•0 comments