frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

AI #165: In Our Image

https://thezvi.substack.com/p/ai-165-in-our-image
1•paulpauper•37s ago•0 comments

Theft Is Now Progressive Chic

https://www.theatlantic.com/ideas/2026/04/hasan-piker-jia-tolentino-microlooting/686919/
1•paulpauper•1m ago•0 comments

Redis-objects 2.0: 17 years, 2k stars, and 16M downloads later

https://nateware.com/2026/04/23/redis-objects-2-0-17-years-2k-stars-and-16m-downloads-later/
1•nateware•2m ago•0 comments

Surely there must be a way to make container secrets less dangerous?

https://dalmatian.life/2026/04/11/surely-there-must-be-a-way-to-make-container-secrets-less-dange...
3•birdculture•12m ago•0 comments

Show HN: Payoff calculator: Should you pay off debt or invest?

https://www.planwell.ai/paydown
1•arundhati2000•15m ago•1 comments

The Hypochondriac's Complaint

https://www.publicbooks.org/the-hypochondriacs-complaint/
1•lermontov•16m ago•0 comments

The Happy Path Doesn't Exist: Notes on Software Fluidity [video]

https://www.youtube.com/watch?v=VzjvBYaGHgA
1•stephenlf•17m ago•1 comments

Romancy – a lightweight durable execution framework

https://github.com/i2y/romancy
1•gavmor•18m ago•0 comments

Show HN: I built a toy that plays grandma's stories when my daughter hugs it

https://www.storyfriend.co.uk/
2•samaralihussain•19m ago•0 comments

Magic: The Gathering Took Me from N2 to Japanese Fluency

https://www.tokyodev.com/articles/how-magic-the-gathering-took-me-from-n2-to-japanese-fluency
1•pwim•22m ago•1 comments

A Dab of DuckDB

https://peterdohertys.website/blog-posts/dab-of-duck.html
1•ethagnawl•24m ago•0 comments

I Built a Receipt Splitter for Group Dinners

https://tabchop.app/blog/built-a-receipt-splitter-for-group-dinners
2•mishang•27m ago•0 comments

Soldier Used Classified Information to Bet on Maduro's Ouster

https://www.nytimes.com/2026/04/23/nyregion/polymarket-maduro-indictment-soldier.html
5•mizzao•29m ago•2 comments

Amazon U.S. Bans Raspail's Bestseller the Camp of the Saints

https://europeanconservative.com/articles/news/amazon-u-s-bans-raspails-bestseller-the-camp-of-th...
1•qwertyuiop_•30m ago•0 comments

It Ain't Broke: Why Software Fundamentals Matter More

https://www.youtube.com/watch?v=v4F1gFy-hqg
1•whizusukite•33m ago•0 comments

Job Satisfaction Declines; Likelihood of Moving to a New Job Lowest Since 2021

https://www.newyorkfed.org/microeconomics/sce/labor
2•randycupertino•35m ago•0 comments

A McDonald's cheeseburger is a better deal today than it was in 1948

https://www.wsj.com/opinion/in-defense-of-the-mcdonalds-cheeseburger-20d2f0ef
2•bookofjoe•39m ago•2 comments

Show HN: SQL Protocol – learn SQL by running real queries, with 1v1 PvP

https://sqlprotocol.com
1•ItaiZeilig•41m ago•0 comments

NixPaint – Paint with Lambda's

https://nixpaint.extranix.com
2•mipselaer•43m ago•1 comments

HX Is the New UX: Designing for the Harness Experience

https://neurometric.substack.com/p/hx-is-the-new-ux-what-you-need-to
1•robmay•46m ago•0 comments

The Switch 2s Best New Feature Is a Battery Power Hog but Still Worth It

https://kotaku.com/switch-2-handheld-boost-mode-battery-life-xenoblade-2000680392
1•PaulHoule•48m ago•0 comments

Wildest mlb play ever – 108mph line drive disappears into pitcher's shirt

https://www.msn.com/en-us/sports/mlb/this-might-be-the-wildest-mlb-play-you-ll-ever-see-108-mph-l...
1•bookmtn•48m ago•0 comments

Why Everything's a Subscription – and Why That May Be Good for You

https://www.magiclasso.co/insights/why-everythings-a-subscription/
2•bentocorp•48m ago•0 comments

Bitwarden engineers who had the compromised Checkmarx VSCode extension got hit

https://old.reddit.com/r/selfhosted/comments/1stjtay/comment/ohvbj63/
1•milkglass•50m ago•1 comments

Microsoft Targets About 7% of Its U.S. Workers with Buyout Offer

https://www.nytimes.com/2026/04/23/business/microsoft-layoffs-artificial-intelligence.html
1•rmason•54m ago•2 comments

Ghost 6.0

https://ghost.org/6
6•thunderbong•1h ago•1 comments

Dev targeted by sophisticated job scam

https://www.theregister.com/2026/04/23/job_scam_targeted_developer/
2•defrost•1h ago•0 comments

Former MrBeast employee accuses YouTube giant of wrongful termination

https://courthousenews.com/former-mrbeast-employee-accuses-youtube-giant-of-wrongful-termination/
1•toomanyrichies•1h ago•0 comments

Community Votes to Deny Water to Nuclear Weapons Data Center

https://www.404media.co/community-votes-to-deny-water-to-nuclear-weapons-data-center/
2•cdrnsf•1h ago•0 comments

Show HN: GeoValida – Spatial Intelligence reality check to find the best land

https://geovalida.com
1•vellal•1h ago•0 comments