frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Show HN: Free Wealth management in one app (gold, stock, MF, budget etc.)

https://icorpus.vercel.app/
1•mathan_karthik•1m ago•0 comments

"This Job Sucks": DOJ Attorney Asks Judge to Hold Her in Contempt

https://newrepublic.com/post/206115/this-job-sucks-doj-attorney-judge-contempt-ice-court-orders
1•petethomas•4m ago•0 comments

Show HN:OpenClaw Skills-Discover, compare skills with clear risk signals

https://openclaw-skills.pro
1•dond1986•6m ago•1 comments

Sudo maintainer is looking for help

https://www.theregister.com/2026/02/03/sudo_maintainer_asks_for_help/
2•random_duck•15m ago•0 comments

Catalina Island's private landowner moves forward on killing all its deer

https://www.sfgate.com/la/article/catalina-island-deer-21320437.php
1•c420•16m ago•1 comments

My Hardest Bug Ever (2013)

https://www.gamedeveloper.com/programming/my-hardest-bug-ever
1•vermilingua•17m ago•0 comments

A copyleft draft license for datasets. You take my data, you give your data

https://discuss.sflc.in/d/SBNXHa9k/viswam-ai-data-set-license-draft-for-discussion
1•nutanc•27m ago•1 comments

Transfeminine Science

https://transfemscience.org/
2•exvi•30m ago•0 comments

Tech Notes: The smallest build system

https://neugierig.org/software/blog/2026/01/smallest-build-system.html
2•vinhnx•31m ago•0 comments

Why charging overnight doesn't ruin the battery anymore

https://lowendmac.com/2026/why-charging-overnight-doesnt-ruin-the-battery-anymore/
1•jandeboevrie•32m ago•0 comments

Sinosauropteryx

https://en.wikipedia.org/wiki/Sinosauropteryx
1•exvi•35m ago•0 comments

Cyberpunk FAQ (1994)

http://www.textfiles.com/internet/FAQ/faqaltcp.txt
3•exvi•36m ago•0 comments

Show HN: Everymorning – Daily digest of top fresh STEM papers from ArXiv

https://stemem.info
1•xodn348•37m ago•0 comments

The Insidious Effects of Hurrying

https://hbr.org/2025/01/the-insidious-effects-of-hurrying
1•curmudgeon22•38m ago•0 comments

Vibe Coding Design Study: Tlparse

https://blog.ezyang.com/2026/02/vibe-coding-design-study-tlparse/
1•mfiguiere•38m ago•1 comments

Web archive is down (DDoS?)

https://web.archive.org/
1•remh•41m ago•1 comments

Lotus Health nabs $35M for AI doctor that sees patients for free

https://techcrunch.com/2026/02/03/lotus-health-nabs-35m-for-ai-doctor-that-sees-patients-for-free/
2•nnurmanov•43m ago•1 comments

Show HN: A T-Spline Library

https://github.com/DSchroer/t-spline
1•dschroer•43m ago•0 comments

AI Headshot Generator

https://aiheadshotgenerator.online/
1•jewelryde•44m ago•1 comments

Why Cold Drinks Were Lethal before 1914 [video]

https://www.youtube.com/watch?v=6HVYHNTDOFs
1•dataflow•44m ago•0 comments

Show HN: OG:IMAGE – Generate social preview images via URL

https://www.ogimage.art/
1•abhishekundalia•45m ago•1 comments

Explore the Stratosphere with a DIY Pico Balloon

https://spectrum.ieee.org/explore-stratosphere-diy-pico-balloon
2•lukeinator42•48m ago•0 comments

Attackers Are Turning Lovable into a Microsoft Phishing Machine

https://ravenmail.io/blog/lovable-vibecoding-phishing
3•insaaniManav•51m ago•0 comments

AI Data Centers Will Break America (Like 2008)

https://www.youtube.com/watch?v=NFP_Dwddcag
1•cable2600•53m ago•0 comments

I Tested the First Sodium-Ion Power Station [video]

https://www.youtube.com/watch?v=Z9k8dsApWIM
1•fenced_load•56m ago•0 comments

NASA hit by fuel leaks during a practice countdown of the moon rocket

https://apnews.com/article/nasa-moon-artemis-astronauts-space-479f13e074b3df8b9453230cb248c5bf
6•aao-fyi•1h ago•1 comments

Show HN: TIEP – Open Protocol for Cross-Platform Bot Detection

https://github.com/Jamessfks/Threat-Intelligence-Exchange-Protocol
1•Jamessfks123•1h ago•0 comments

Competence as Tragedy

https://crowprose.com/blog/competence-as-tragedy/
1•fs_software•1h ago•0 comments

Petition for Recognition of Work on Open-Source as Volunteering in Germany

https://www.openpetition.de/petition/online/recognition-of-work-on-open-source-as-volunteering-in...
17•numeri•1h ago•1 comments

What Is Border Patrol Doing in American Cities?

https://www.nationalreview.com/corner/what-is-border-patrol-doing-in-american-cities/
3•petethomas•1h ago•0 comments