frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

UMass Amherst Researchers Create Nanoparticle Vaccine That Prevents Cancer /Mice

https://www.umass.edu/news/article/umass-amherst-researchers-create-nanoparticle-vaccine-prevents...
1•1xer•1m ago•0 comments

Show HN: AI Art Platform: Video and Image Creator – VGenie

https://vgenie.ai/home
1•funny_aiadsa•4m ago•1 comments

NYC congestion pricing cuts air pollution by 22% in six months

https://airqualitynews.com/cars-freight-transport/nyc-congestion-pricing-cuts-air-pollution-by-22...
1•pseudolus•5m ago•0 comments

Show HN: AI Interview-to-Offer Agent Network

https://meet.readymojo.com/
1•snasan•9m ago•0 comments

Ukraine's Daring Operation Spiderweb' Attack on Russia

https://www.wsj.com/world/europe/ukraine-russia-drone-attack-operation-spiderweb-24d821ab
1•pinewurst•19m ago•0 comments

Atomic time source failure at NIST Gaithersburg campus

https://groups.google.com/a/list.nist.gov/g/internet-time-service/c/Zd7VaR-vqV4?pli=1
5•dpcx•23m ago•0 comments

Nothing in standard Ubuntu app set relies on curl?

1•rikeanimer•25m ago•0 comments

Making macOS Bearable

https://seg6.space/posts/making-macos-bearable/
4•seg6•26m ago•1 comments

POC for CVE-2025-55182 that works on Next.js 16.0.6

https://gist.github.com/maple3142/48bc9393f45e068cf8c90ab865c0f5f3
1•maxloh•27m ago•0 comments

Will Calibri leave Pakistan sans Sharif? (2017)

https://www.thomasphinney.com/2017/07/pakistan-calibri-forgery/
4•joecool1029•27m ago•0 comments

Bacterially grown living materials with resistant and on-demand functionality

https://www.science.org/doi/10.1126/sciadv.adw8278
1•PaulHoule•30m ago•0 comments

American Science, Shattered: Special report on research funding cuts

https://www.statnews.com/american-science-shattered/
2•Anon84•31m ago•0 comments

Racial Views of Donald Trump

https://en.wikipedia.org/wiki/Racial_views_of_Donald_Trump
6•lehi•32m ago•0 comments

Canonical Extends Kubernetes Long-Term Support to 15 Years

https://thenewstack.io/canonical-extends-kubernetes-long-term-support-to-15-years/
1•CrankyBear•33m ago•0 comments

Lessons from Building (and Closing) Double Finance (YC W24)

https://jjmaxwell4.com/writing/shutting-down-double/
3•jjmaxwell4•34m ago•1 comments

You Have Billions Invested in Generative AI

https://woe-industries.itch.io/you-have-billions-invested-in-generative-ai
4•HotGarbage•36m ago•0 comments

Z-Band: On-World Interactions Using Wrist-Based Electrical Impedance Sensing

https://dl.acm.org/doi/pdf/10.1145/3770637
1•momoschili•37m ago•0 comments

Tech In the D: MOT MOT (3 min video)

https://www.blacktechsaturdays.com/tech-in-the-d/motmot
1•rmason•39m ago•0 comments

Show HN: Inferbench, collect/share datapoints on GPU's inference performance

https://www.inferbench.com/
3•binsquare•44m ago•1 comments

Show all your application error using Cloudflare Error Page

https://github.com/donlon/cloudflare-error-page
3•sawirricardo•46m ago•0 comments

The Greenhushing Trap

https://sloanreview.mit.edu/article/the-greenhushing-trap/
1•gnabgib•48m ago•0 comments

Scheduler Woes: Bisecting Early Performance Regressions Found in Linux 6.19

https://www.phoronix.com/review/linux-619-sched-regress
1•dabinat•53m ago•0 comments

Fear of the Walking Zig: The Security Audit Gap

https://generativeai.pub/fear-of-the-walking-zig-the-security-audit-gap-707aec6ceb92
2•todsacerdoti•55m ago•0 comments

Show HN: Qyavix – A 155-byte DOM runtime inspired by React hooks

https://github.com/Yinhao-c/Qyavix
3•yihac1•56m ago•0 comments

iPadOS 26.2 will restore Split View and Slide Over multitasking

https://9to5mac.com/2025/12/07/apple-fixed-ipad-software-gripe-ipados-26-2-slide-over/
2•walterbell•57m ago•0 comments

Wyoming Cowboys Are Breaking Down Barriers

https://www.nytimes.com/2025/12/09/climate/wyoming-virtual-fences-livestock.html
2•fleahunter•1h ago•0 comments

Window Maker Live 13.2 brings 32-bit life to Debian 13

https://www.theregister.com/2025/12/09/microsoft_windows_maker_live_132/
2•wmlive•1h ago•1 comments

Google kills Gemini Cloud Services (2035)

https://sw.vtom.net/hn35/item.html?id=90099555
5•throwaway-0001•1h ago•0 comments

Utah Tries Relocating Beavers to Save Them, and Remake the Landscape

https://www.nytimes.com/2025/12/09/climate/utah-beaver-rescue.html
1•quapster•1h ago•0 comments

The Seven-Minute Doctor Visit Cannot Understand a Human Body like AI

https://markatwood.substack.com/p/the-seven-minute-visit-cannot-understand
2•walterbell•1h ago•0 comments