frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•7mo 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•7mo 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•7mo 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: StretchBreak, a simple web app to solve planning time off

https://stretchbreak.netlify.app/
1•tha_infra_guy•3m ago•0 comments

California lawmaker wants to ban AI from children's toys

https://www.fastcompany.com/91468728/california-lawmaker-ban-ai-toys
3•geox•6m ago•0 comments

NY Fed cash transfers to banks increase dramatically in Q4 2025

https://www.dcreport.org/2025/12/29/ny-fed-unlimited-cash-infusions-bank-crisis/
1•scythe•7m ago•0 comments

Brand as Code

https://www.braingrid.ai/blog/brand-as-code
1•acossta•11m ago•2 comments

He Was a Supreme Court Lawyer. Then His Double Life Caught Up with Him

https://www.nytimes.com/2025/12/28/magazine/thomas-goldstein-supreme-court-gambling.html
1•ryan_j_naughton•13m ago•0 comments

LeCun calls Alex Wang inexperienced, predicts more Meta AI employee departure

https://www.businessinsider.com/yann-lecun-alexandr-wang-criticism-inexperienced-meta-ai-future-2...
5•_____k•14m ago•0 comments

The Lottery Ticket Hypothesis: finding sparse trainable NNs with 90% less params

https://arxiv.org/abs/1803.03635
1•felineflock•14m ago•0 comments

Economic inequality does not equate to poor well-being or mental health

https://www.nature.com/articles/d41586-025-03833-8
1•gmays•15m ago•0 comments

Glasses-free 3D display with ultrawide viewing range using deep learning

https://www.nature.com/articles/s41586-025-09752-y
1•PaulHoule•16m ago•0 comments

Jot: Offline, source available notetaking/assistant app

https://jot-ai.app/blog/welcome-to-jot
4•robust-cactus•16m ago•1 comments

An unsolved question in sleep science

https://www.autodidacts.io/long-sleep-duration-and-mortality/
1•Curiositry•19m ago•0 comments

Mastodon Server Covenant

https://joinmastodon.org/covenant
1•doener•19m ago•0 comments

Ask HN: Is the window for local-first AI closing?

1•zerocool86•21m ago•0 comments

Capital in the 22nd Century

https://philiptrammell.substack.com/p/capital-in-the-22nd-century
2•thundergolfer•21m ago•0 comments

Foldy Bird

https://lyra.horse/fun/foldy-bird/
2•Groxx•23m ago•1 comments

Displaying Interactive Maps over SSH

https://terminal-maps.henryn.ca
1•ng-henry•24m ago•1 comments

KdK part 2: a medical mystery from postwar Germany

https://nealstephenson.substack.com/p/kdk-part-2-a-medical-mystery-from
2•cl42•25m ago•0 comments

Chaos Chess Online

https://www.chaoschess.online/game?mode=classic
1•indigodaddy•25m ago•0 comments

Show HN: Go-Highway – Portable SIMD for Go

https://github.com/ajroetker/go-highway
1•kingcauchy•31m ago•0 comments

Crates.io Moving to Svelte

https://github.com/rust-lang/crates.io/issues/12515
1•Alupis•32m ago•0 comments

Grok Sexual Images Draw Rebuke, France Flags Content as Illegal

https://finance.yahoo.com/news/grok-sexual-images-draw-rebuke-180354505.html
9•akutlay•33m ago•5 comments

Pricing ideas to steal this week

https://newsletter.pricingsaas.com/p/5-pricing-ideas-to-steal-this-week
1•robbylit•33m ago•0 comments

Dimensions: Terminal Tab Manager

https://karlvmuller.com/posts/dimensions/
1•KarlVM12•34m ago•0 comments

Is This China's Truck-Mounted Electromagnetic Catapult?

https://www.twz.com/news-features/is-this-chinas-truck-mounted-drone-launching-electromagnetic-ca...
2•ano-ther•34m ago•0 comments

Yann LeCun: Intelligence is about learning

https://www.ft.com/content/e3c4c2f6-4ea7-4adf-b945-e58495f836c2
2•brandonb•34m ago•0 comments

1M for Non-Specialists: Introduction

https://pithlessly.github.io/1ml-intro
1•birdculture•36m ago•0 comments

Show HN: Semantic Search for Your GitHub Stars

https://github.com/HikmetCTK/Star_Seeker_mcp
1•bakigul•42m ago•0 comments

Zigzag Number Spiral

https://susam.net/zigzag-number-spiral.html
2•enz•43m ago•0 comments

The first really new compass since 1936 - I invented a new type of compass [video]

https://www.youtube.com/watch?v=eiDhbZ8-BZI
3•evanjrowley•46m ago•0 comments

Same AI agent, different prompts: 0% vs. 62% security pass rate

1•xsourcesec•47m ago•1 comments