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.

Taxpayers to spend $477B on tax season

https://www.thecentersquare.com/national/article_27a78f27-ea2a-4adc-809a-aa77befdb3ff.html
1•speckx•1m ago•0 comments

Allbirds shares jump over 400% on plans to pivot to AI from sneakers

https://www.reuters.com/business/allbirds-shares-jump-over-400-plans-pivot-ai-sneakers-2026-04-15/
1•sph•1m ago•0 comments

Escaping the Fork: How Meta Modernized WebRTC Across 50 Use Cases

https://engineering.fb.com/2026/04/09/developer-tools/escaping-the-fork-how-meta-modernized-webrt...
1•ashvardanian•2m ago•0 comments

Empedocles fragments found in Egypt

https://metro.co.uk/2026/04/14/work-lost-philosopher-inspired-plato-aristotle-uncovered-egypt-279...
1•Iuz•5m ago•0 comments

Tesla 'Full Self-Driving' crashed through railroad gate seconds before train

https://electrek.co/2026/04/15/tesla-fsd-railroad-gate-oncoming-train-texas-owner/
1•Bender•5m ago•0 comments

Thomson Reuters Fired Worker for Speaking Out About ICE, Former Employee Says

https://www.404media.co/thomson-reuters-fired-worker-for-speaking-out-about-ice-former-employee-s...
8•Brajeshwar•6m ago•0 comments

Simdutf Can Now Be Used Without Libc++ or Libc++ABI

https://mitchellh.com/writing/simdutf-no-libcxx
1•cdrnsf•7m ago•0 comments

Show HN: Playbach.io, browser rhythm game (desktop)

https://playbach.io/
1•tsterin•8m ago•0 comments

Arm comes to the Framework 13 [video]

https://www.youtube.com/watch?v=CIb87SJ4xwo
3•znpy•9m ago•0 comments

Comparing Compression Tools

https://www.arp242.net/cmp-compress.html
1•ingve•10m ago•0 comments

Startups Are Context Arbitrages

https://www.alessiofanelli.com/posts/startups-are-context-arbitrages
1•FanaHOVA•11m ago•0 comments

Coq theorem prover is now called Rocq

https://rocq-prover.org/about
1•rwmj•11m ago•0 comments

Printing real headline news on the Commodore 64 with The Newsroom's Wire Service

http://oldvcr.blogspot.com/2023/03/printing-real-headline-news-on.html
1•superultra•11m ago•1 comments

Space Force looks at moving "significant number" of launches from ULA to SpaceX

https://arstechnica.com/space/2026/04/space-force-looks-at-moving-significant-number-of-launches-...
3•Bender•12m ago•0 comments

Opting out of cookies no guarantee

https://globalprivacyaudit.org/2026/california
1•HelloUsername•13m ago•0 comments

How Accurate Are Google's A.I. Overviews?

https://www.nytimes.com/2026/04/07/technology/google-ai-overviews-accuracy.html
1•bookofjoe•14m ago•2 comments

Lowdefy v5: The Config Webstack

https://lowdefy.com/articles/lowdefy-5-whats-new/
1•gervwyk•14m ago•0 comments

I made Agentation for vanilla JavaScript

https://github.com/mearnest-dev/agentation-vanilla
1•mearnest•14m ago•1 comments

Project Glasswing Has a Blind Spot. It's You

https://quodeq.ai/blog/glasswing-blind-spot/
3•vikDPG•15m ago•0 comments

Users lose $9.5M to fake Ledger wallet app on the Apple App Store

https://www.web3isgoinggreat.com/?id=fake-ledger-app
3•CharlesW•15m ago•1 comments

Jane Street Signs $6B AI Cloud Agreement with CoreWeave

https://www.coreweave.com/news/jane-street-signs-6-billion-ai-cloud-agreement-with-coreweave
1•moelf•16m ago•0 comments

Linux 7.1 Is a Big Win for Intel Panther Lake with Fred Now Enabled by Default

https://www.phoronix.com/news/Linux-7.1-Enabled-Intel-FRED
2•mikece•17m ago•0 comments

Keyword Scout

https://keywordscout.app
1•DailyGeo•18m ago•0 comments

ChatGPT, Is This Real?

https://arxiv.org/abs/2604.09316
2•runningmike•20m ago•0 comments

Show HN: EmbedIQ – Claude Code Compliance Config for HIPAA/PCI-DSS/SOC2

https://github.com/asq-sheriff/embediq
1•asqpl•20m ago•0 comments

We Built Hanker in 14 Days with Claude

https://hanker.app/blog/we-built-hanker-in-14-days-with-claude-heres-the-slightly-unhinged-techni...
1•whatsupdog•21m ago•0 comments

Fiverr Denies Report of Data Leak

https://www.pymnts.com/cybersecurity/2026/fiverr-denies-report-of-data-leak/
1•shooker435•22m ago•1 comments

AI papers published in 2026 worth reading

https://www.chapterpal.com/curriculum/a0/papers-published-in-2026-worth-reading
1•roody_wurlitzer•25m ago•1 comments

Claude Cowork found me a flat to rent in London in just 5 days

https://old.reddit.com/r/ClaudeAI/comments/1smay7l/claude_cowork_found_me_a_flat_to_rent_in_london/
2•mikepapadim•25m ago•1 comments

Project Maven Put A.I. Into the Kill Chain

https://www.newyorker.com/books/under-review/how-project-maven-put-ai-into-the-kill-chain
2•littlexsparkee•25m ago•0 comments