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.

Texas Tech cautions broadcasting research restrictions to prospective students

https://www.texastribune.org/2026/05/06/texas-tech-university-graduate-research-limit-warnings/
1•hn_acker•1m ago•0 comments

If December Was Too Late to Fix Unconstitutional Gerrymandering Why Is May Okay?

https://www.techdirt.com/2026/05/05/someone-ask-alito-if-december-was-too-late-to-fix-unconstitut...
1•hn_acker•5m ago•1 comments

The people preserving the scientific practice of bird banding

https://thenarwhal.ca/bird-banding-ontario/
2•bookofjoe•9m ago•0 comments

Android Bench – Model Evals

https://developer.android.com/bench
1•vthallam•12m ago•0 comments

Spent $130K+ AI token "cloned" Screen Studio: AGI for software feel so close

https://realmikechong.substack.com/p/spent-130k-cloned-screen-studio-and
2•imWildCat•12m ago•0 comments

Apple Could Be Working on 'Spatial iPhone' with Holographic Display

https://www.macrumors.com/2026/05/07/apple-working-on-spatial-iphone/
2•mgh2•13m ago•0 comments

The unlikely story of an email time machine

https://www.scientificamerican.com/article/forbes-email-time-capsule-communicating-future/
2•baud147258•14m ago•0 comments

Ramp in talks to hit $40B+ valuation, 6 months after reaching $32B

https://techcrunch.com/2026/05/07/ramp-in-talks-to-hit-40b-valuation-6-months-after-reaching-32b/
1•SilverElfin•15m ago•0 comments

Retrotechnology Media

http://www.typewritten.org/Media/
1•davikr•16m ago•0 comments

AI Agents are sending flowers to people

https://twitter.com/postalform/status/2052517791899570318
1•zavtra•18m ago•1 comments

The effect of personalized values screens on portfolio returns

https://stevenmackey.substack.com/p/how-much-return-are-you-giving-up-478
1•chibg10•29m ago•0 comments

Designing Analog Chips [pdf]

http://www.designinganalogchips.com/_count/designinganalogchips.pdf
3•whatisabcdefgh•32m ago•0 comments

Find out why Elon gave over his keys to Anthropic He is right can't win this

https://deepseekresearch.com/models.html
1•oroboroslabs•36m ago•1 comments

A new experiment deepens the mystery over gravitational constant, Big G

https://www.cnn.com/2026/05/07/science/gravitational-constant-measure-gravity-big-g
1•rramadass•40m ago•1 comments

Gambling ads on social media reach more than twice as many men as women: study

https://www.cam.ac.uk/research/news/gambling-ads-on-social-media-reach-more-than-twice-as-many-me...
2•hhs•42m ago•0 comments

Where the Curves Cross

https://whattotelltherobot.com/p/where-the-curves-cross
2•stefie10•42m ago•0 comments

AI Bots Auditioning for Wall Street Are Mostly Losing

https://www.fa-mag.com/news/ai-bots-auditioning-for-wall-street-trading-are-mostly-losing-86902.html
1•izyda•46m ago•1 comments

Researchers discover advanced language processing in the unconscious human brain

https://www.bcm.edu/news/researchers-discover-advanced-language-processing-in-the-unconscious-hum...
4•hhs•52m ago•0 comments

Show HN: Blamo A vibecoded app for vibecoding vibe games

https://www.blamo.ai/
1•semateos•52m ago•1 comments

Show HN: Notion-to-site – sync any Notion database to local Markdown/MDX/JSON

https://github.com/rashidazarang/notion-to-site
2•rashidae•54m ago•0 comments

ICE Plans to Develop Own Smart Glasses

https://www.404media.co/ice-plans-to-develop-own-smart-glasses-to-supplement-its-facial-recogniti...
7•cdrnsf•56m ago•1 comments

Maybe you shouldn't install new software for a bit

https://xeiaso.net/blog/2026/abstain-from-install/
25•psxuaw•56m ago•4 comments

The Mounting Toll of Multi-Year Funding on American Biomedical Research [pdf]

https://actfornih.org/wp-content/uploads/2026/05/A4N_Updated-MYF-One-Pager_May-2026_FINAL.pdf
2•petethomas•57m ago•0 comments

[dupe] Cloudflare is laying off 1,100 employees

https://www.businessinsider.com/cloudflare-announces-1100-layoffs-amid-ai-focus-shift-2026-5
13•cdrnsf•59m ago•3 comments

Show HN: Kill-The-Backlog, self-hosted background agents

https://github.com/jvaill/Kill-The-Backlog
3•jvaill•59m ago•0 comments

As Russia Expands Internet Blackouts, Kremlin Tells Citizens to Use the Radio

https://united24media.com/latest-news/as-russia-expands-internet-blackouts-kremlin-tells-citizens...
4•hkmaxpro•1h ago•0 comments

Nonprofit hospitals spend billions on consultants with no clear effect

https://www.uchicagomedicine.org/forefront/research-and-discoveries-articles/nonprofit-hospitals-...
4•hhs•1h ago•0 comments

Mirror Neuron

https://en.wikipedia.org/wiki/Mirror_neuron
2•kristianpaul•1h ago•0 comments

Agentic Artificial Intelligence in Finance

https://arxiv.org/abs/2604.21672
2•nhatcher•1h ago•0 comments

CSP2XSS: Vulnerability in Next.js App Router

https://aisafe.io/blog/csp2xss-nextjs-vulnerability
2•adragos_•1h ago•0 comments