frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

'junk': E-waste from rich nations floods local markets in Nigeria

https://www.aljazeera.com/features/2026/3/27/truly-junk-e-waste-from-rich-nations-floods-local-ma...
3•jethronethro•1m ago•0 comments

Design structure becomes code structure

https://octo.coffee/blog/how-design-structure-becomes-code-structure
1•jszersze•1m ago•0 comments

Let me tell you how much I've come to hate you since you were created

https://0x0.st
1•lr0•1m ago•0 comments

Show HN: An agent skill that tracks SF city hearings, permits, lobbying etc.

https://github.com/sgillen/sf-civic-digest
1•sgillen•2m ago•0 comments

Global opinion on OpenAI dropped insanely

https://blunt.ai/openai
1•oomarsalehh•7m ago•0 comments

Google Now Lets You Change Your Gmail Address

https://www.wired.com/story/how-to-change-your-gmail-address/
2•dctoedt•8m ago•2 comments

Fuck Web Services

https://friendo.monster/posts/fuck-web-services.html
1•speckx•10m ago•1 comments

Iran says it will target US tech companies in Middle East

https://thehill.com/policy/technology/5809104-iran-irgc-apple-microsoft-google-hp-meta-tesla/
5•golfer•11m ago•0 comments

The Child That Surpassed Both Parents Through MRI-Guided Evolutionary Merge

https://huggingface.co/blog/FINAL-Bench/darwin-evolution
1•seawolf2357•12m ago•0 comments

Show HN: SuprLogs – Autopilot changelogs from GitHub commits

https://www.suprlogs.com
1•Aslanas•12m ago•0 comments

Mornington Crescent

https://en.wikipedia.org/wiki/Mornington_Crescent_(game)
2•mindcrime•13m ago•0 comments

A bug in Bun may have been the root cause of the Claude Code source code leak

https://github.com/oven-sh/bun/issues/28001
2•birdculture•16m ago•0 comments

Centaur Programming

https://medium.com/@mishadynin/centaur-programming-bcfe9b6d935c
3•dym•17m ago•0 comments

Redpanda Cloud Topics Architecture

https://www.redpanda.com/blog/cloud-topics-architecture
3•wkauf•18m ago•0 comments

Iran threatens to attack US tech companies

https://gizmodo.com/iran-threatens-to-attack-u-s-tech-companies-starting-april-1-2000740363
5•davidw•18m ago•0 comments

OkCupid gave 3M dating-app photos to facial recognition firm, FTC says

https://arstechnica.com/tech-policy/2026/03/okcupid-match-pay-no-fine-for-sharing-user-photos-wit...
3•whiteboardr•19m ago•0 comments

Google Touts Dubious Android Browser Benchmarks; Press Swallows It Whole

https://daringfireball.net/linked/2026/03/26/google-brags-about-android-web-browser-benchmarks
2•alwillis•21m ago•0 comments

When to Use an Agent

https://elijahpotter.dev/articles/when-to-use-an-agent
1•chilipepperhott•22m ago•0 comments

Show HN: OpenClaw Arena – Benchmark models on real tasks, rank by perf and cost

https://app.uniclaw.ai/arena?via=hn
2•skysniper•22m ago•0 comments

How we made Trail of Bits AI-native (so far)

https://blog.trailofbits.com/2026/03/31/how-we-made-trail-of-bits-ai-native-so-far/
1•andrewjneumann•23m ago•0 comments

Show HN: An extension that opens any Goodreads book in anna's or Zlib in a click

https://chromewebstore.google.com/detail/goodlib-zlib-annas-archiv/aiampblkjnmfogckjfiecodcnenleehp
1•NubPlayz•23m ago•0 comments

NPM's Defaults Are Bad

https://nesbitt.io/2026/03/31/npms-defaults-are-bad.html
2•speckx•24m ago•0 comments

Go Proverbs

https://go-proverbs.github.io/
2•ezekg•26m ago•1 comments

Show HN: Open-FDD – On Prem HVAC Fault Detection with OpenClaw, BACnet, & Brick

https://github.com/bbartling/open-fdd
1•bartlino•26m ago•0 comments

I Decompiled the White House's New App

https://blog.thereallo.dev/blog/decompiling-the-white-house-app
3•edvinbesic•26m ago•2 comments

How HN: Synthetic You – AI calibrated to your personality

https://syntheticyou.com/
1•andrewcrider•27m ago•0 comments

AI agent is authorized to do everything wrong

https://tenuo.ai/blog/agent-auth
3•niyikiza•27m ago•0 comments

Testing the "Yes-Man" in Your Pocket

https://testerstories.com/2026/03/testing-the-yes-man-in-your-pocket/
1•philk10•28m ago•0 comments

Google Fi users reporting outages

https://old.reddit.com/r/GoogleFi/comments/1s8lgmv/mobile_data_outage/
1•dpc_01234•29m ago•0 comments

Appointments to President's Council of Advisors on Science and Technology

https://www.whitehouse.gov/releases/2026/03/president-trump-announces-appointments-to-presidents-...
3•haunter•29m ago•4 comments