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.

Ask HN: Is this type of writing is compelling, or over rated

1•Daniel-Pan•1m ago•0 comments

Fire and Coast Fire Tracker · Streamlit

1•Rossklot•2m ago•0 comments

U.S. Department of Energy Delivers Third Advanced Reactor Criticality

https://www.energy.gov/articles/us-department-energy-meets-president-trumps-goal-delivers-third-a...
1•simonpure•7m ago•0 comments

Moneyball for Physical AI

https://praxiscurrents.substack.com/p/moneyball-for-physical-ai
1•gmays•8m ago•0 comments

OpenUI: Open Standard for Generative UI

https://www.openui.com
1•handfuloflight•12m ago•0 comments

The delicious irony of Anthropic bemoaning distillation

https://twitter.com/ejzim/status/2072692694036660517
1•MrBuddyCasino•17m ago•0 comments

Lotus: Optimized Agentic and LLM Bulk Processing

https://github.com/lotus-data/lotus
1•handfuloflight•26m ago•0 comments

The end of paper retirement processing at the Office of Personnel Management

https://twitter.com/spikebrehm/status/2072422555101561154
2•MrBuddyCasino•32m ago•0 comments

Hackers On Planet Earth is back in Manhattan next month

https://www.hope.net/
2•fashiontechguru•34m ago•2 comments

GPT-5.5-Cyber built a zlib fuzzing lab in a day

https://blog.trailofbits.com/2026/07/02/field-reports-from-patch-the-planet/
1•wslh•36m ago•0 comments

Don't Train the Model, Evolve the Harness

https://huggingface.co/spaces/joelniklaus/harness-optimization
1•emersonmacro•38m ago•0 comments

Automated Accounting, Financial Reporting and Tax Management

https://maxint.com/blog/maxint-2-24-release
1•pcvetkovski•42m ago•0 comments

Trump's Windfall Has Few Known Global Precedents

https://www.nytimes.com/2026/07/02/world/europe/trump-world-leaders-corruption-wealth.html
2•Alien1Being•44m ago•0 comments

Ask HN: Once you make your money from vibe coding innumerable products, then?

1•keepamovin•49m ago•0 comments

Private Credit Keeps $14B Trapped in Bid to Outlast Storm

https://www.bloomberg.com/news/articles/2026-07-02/private-credit-keeps-14-billion-trapped-in-bid...
1•petethomas•49m ago•0 comments

Ask HN: How do you get your open-source product good traction?

1•akarshhegde18•49m ago•1 comments

Blue Owl hit by $4.7B of redemption requests as investor exodus persists

https://www.ft.com/content/b302a86d-f6eb-4d47-b90b-523c1c19b3fa
1•petethomas•50m ago•0 comments

'Humanity is a privilege': Umar Khalid on his six years in a jail without trial

https://www.theguardian.com/world/2026/jun/30/umar-khalid-interview-six-years-indian-jail-without...
2•thunderbong•51m ago•1 comments

Using precision editing to study human embryo development shows master gene

https://www.cam.ac.uk/research/news/first-use-of-precision-editing-to-study-human-embryo-developm...
2•gmays•57m ago•0 comments

Open letter to Anthropic: keep Claude Fable 5 in existing paid plans

https://keepfable.org
2•SpitSalute•1h ago•1 comments

FlickerScope – are your LEDs giving you a headache? stop guessing

https://github.com/snokamedia/flickerscope
1•snoka•1h ago•0 comments

Wikipedia Is Up (2001)

https://web.archive.org/web/20010506042824/www.nupedia.com/pipermail/nupedia-l/2001-January/00068...
2•downbad_•1h ago•0 comments

A few thought about snarky answers on StackOverflow (2019)

https://www.cargocultcode.com/solving-the-zalgo-regex/
1•downbad_•1h ago•0 comments

Tradeoffs in Complexity, Abstraction, and Generality

https://www.lesswrong.com/s/EL2YvcrPNHrGgzPnZ/p/HNJwteaxpRYfLaQt7
1•parksb•1h ago•0 comments

Google's exponential path to climate-wrecking digital bloat

https://ketanjoshi.co/2026/07/01/googles-exponential-path-to-climate-wrecking-digital-bloat/
2•colinprince•1h ago•2 comments

Why AI Is Collapsing: How China Is Winning. [video]

https://www.youtube.com/watch?v=JXJf7vL8k94
1•Bender•1h ago•1 comments

Thanks but No Thanks

https://twitter.com/xopzuey/status/2072838561275969717
2•znort_•1h ago•2 comments

How Many People Have Ever Lived in the United States?

https://danielfetz.io/p/how-many-people-have-ever-lived-in-the-united-states
1•paulpauper•1h ago•0 comments

AI is 'not smart' so what's next in artificial intelligence?

https://www.bbc.com/news/articles/cj6gr0xkyr3o
3•1vuio0pswjnm7•1h ago•0 comments

Core dump epidemiology: fixing an 18-year-old bug

https://openai.com/index/core-dump-epidemiology-data-infrastructure-bug/
2•stopachka•1h ago•0 comments