frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Scientists are unsure how many exist or how rare they are

https://www.discoverwildlife.com/animal-facts/marine-animals/omuras-whale-feeding-raja-ampat
1•bigwheels•1m ago•0 comments

Bitmaps in the Linux Kernel

https://0xax.dev/books/linux-inside/linux-datastructures-2
1•matt_d•1m ago•0 comments

Why Capitalism Is Doomed According to Joseph A. Schumpeter

https://jonasanksher.substack.com/p/why-capitalism-is-doomed
1•paulpauper•2m ago•0 comments

When the White Collars Feel Threatened – A Reply to Terence Tao

https://komando1.substack.com/p/when-the-white-collars-feel-threatened
1•paulpauper•2m ago•0 comments

AI and the Death of the Author

https://thedeepdish.substack.com/p/ai-and-the-death-of-the-author
1•paulpauper•2m ago•0 comments

Show HN: Page.json – An open standard for representing UI

https://wuwei.us/pages/page-json
1•laybak•4m ago•0 comments

Pentagon to execute convicted Fort Hood mass shooter Nidal Hasan by firing squad

https://abcnews.com/US/pentagon-execute-convicted-fort-hood-mass-shooter-nidal/story?id=137019865
1•embedding-shape•4m ago•0 comments

Equality Saturation: An "Incomplete" Project

https://blog.sigplan.org/2026/10/01/equality-saturation-an-incomplete-project/
1•matt_d•5m ago•0 comments

https://surf-alert.net - Simplified Surf Forecasts

1•rmarques7•6m ago•0 comments

Planning system ensures a robot's flight path will remain collision-free

https://news.mit.edu/2026/planning-system-ensures-robots-flight-path-will-remain-collision-free-1007
1•gmays•7m ago•0 comments

Firmus pulls IPO amid investor doubt about datacentre company

https://www.theguardian.com/australia-news/2026/oct/09/firmus-pulls-asx-float-datacentre-investor...
1•aussieguy1234•7m ago•0 comments

AI and Chiral Fermions

https://www.math.columbia.edu/~woit/wordpress/?p=15915
1•jjgreen•9m ago•0 comments

Apple will reportedly debut its first touchscreen MacBook in three weeks

https://www.theverge.com/tech/1008422/apple-macbook-pro-touchscreen-ipad-mini-rumor
1•tolugenius•9m ago•0 comments

Why 'vertical farming' is not a fix for our food system

https://theconversation.com/why-vertical-farming-is-not-a-fix-for-our-food-system-293201
1•rdmuser•12m ago•0 comments

A quiet pixel-art city where strangers listen to music together

https://roomcity.live/
1•louislefebvreqc•13m ago•0 comments

Data needs its DevOps moment

https://render.com/blog/data-needs-its-devops-moment
1•dm03514•14m ago•1 comments

Fort Hood attacker's execution by firing squad will be livestreamed

https://www.bbc.com/news/articles/cmy0r96xygx6o
5•mo7061•15m ago•0 comments

Load TBs into Postgres in Minutes

https://www.databricks.com/blog/load-terabytes-data-minutes-lakebase-postgres
1•moonikakiss•17m ago•0 comments

Bevy 0.20

https://bevy.org/news/bevy-0-20/
1•Philpax•19m ago•0 comments

Show HN: Wick, a daily word game where you guess by meaning, not spelling

https://guesswick.com/
1•jpage2•20m ago•1 comments

Lego Mindstorms' future predictions from 1998

https://www.youtube.com/playlist?list=PL6Tt4uBL-6Pw7kGiKjSDds7A3xhxOG2hq&si=ztOhEYq6JK3-V68Q
2•soundworlds•21m ago•0 comments

The Subreddit Where Men Share Their Dinners–and Their Most Intimate Feelings

https://www.gq.com/story/boydinnerdiaries-subreddit-community
1•rdmuser•22m ago•0 comments

Top Reasons AI Will Never Become Superintelligence: Won't Destroy nor Save Us

https://www.wmbriggs.com/post/62236/
2•B1FF_PSUVM•23m ago•0 comments

ICE Emails Discuss Using Palantir-Supported Tool to Investigate Voter Fraud

https://www.wired.com/story/ice-emails-discuss-using-palantir-supported-tool-to-investigate-voter...
2•mikelgan•25m ago•0 comments

Scientists invent new kind of clock

https://www.the-independent.com/tech/clock-new-atomic-nuclear-time-b3062910.html
1•gmays•25m ago•0 comments

'Breathtaking,' 'Devastating': Mathematics Reels After New OpenAI Release

https://www.nytimes.com/2026/10/08/science/mathematicians-respond-openai-release.html
2•furcyd•26m ago•0 comments

Highly multiplexed mammalian metabolic engineering with a shotgun approach

https://www.nature.com/articles/s41587-026-03318-7
1•bookofjoe•27m ago•0 comments

Spinal: A near-instant, predictive surface for any codebase

https://spinal.sh/
2•handfuloflight•28m ago•0 comments

Google Is Pushing Scam Videos on Elderly and Cognitively Impaired People

https://www.currentaffairs.org/news/how-google-is-pushing-scam-videos-on-elderly-and-cognitively-...
5•rexpop•29m ago•1 comments

What Are the Remaining Millennium Prize Problems?

https://www.nytimes.com/2026/10/08/science/millennium-prize-problems-explainer.html
2•furcyd•29m ago•0 comments