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.

Humanoid Robot Regulations have begun

https://www.youtube.com/watch?v=qoZ_EjWJtes
1•artur_makly•1m ago•0 comments

Turns out that "HDMI 2.1" ports don't need to support HDMI 2.1 features

https://arstechnica.com/gadgets/2021/12/the-hdmi-forum-follows-the-usb-if-in-using-new-version-nu...
1•haunter•5m ago•0 comments

Disney Hires Its First CTO: Karandeep Anand, Former CEO of AI Chatbot Startup

https://variety.com/2026/biz/news/disney-cto-karandeep-anand-character-ai-1236866528/
2•chauzer•6m ago•0 comments

Show HN: Keydris, Sudo for AI Agents

https://www.youtube.com/watch?v=Ouqwx7-gGYw
2•Osman_728•7m ago•0 comments

Better Than O1 Visa?

https://www.uscis.gov/working-in-the-united-states/international-entrepreneur-rule
1•namanbajpai•8m ago•0 comments

Claude Code Projects

https://claude.com/blog/projects-redesigned
1•smoser•8m ago•0 comments

US Military had close call after using AI for hallucinated intelligence report

https://www.cnn.com/2026/09/18/politics/us-military-ai-false-intelligence-china-ship
1•realsarm•9m ago•0 comments

Scam Spotting with ChatGPT

https://scam-spotting-with-chatgpt.openai.chatgpt.site/
1•armcat•10m ago•0 comments

Consuming extra Vitamin D is less good than it sounds

https://www.economist.com/science-and-technology/2026/09/18/consuming-extra-vitamin-d-is-less-goo...
1•dctoedt•10m ago•0 comments

Show HN: Draw Hola Cloud – A drawing tool for live technical explanations

https://draw.holacloud.app/
1•fulldump-net•10m ago•1 comments

Microsoft Confirms October 7 Windows and Surface Event Featuring RTX Spark

https://www.gadgetreview.com/microsoft-confirms-october-7-windows-and-surface-event-featuring-rtx...
1•peter_d_sherman•11m ago•0 comments

A Model for Winning Survivor

https://victoriaritvo.com/blog/predicting-survivor/
1•evakhoury•11m ago•0 comments

Ask HN: Familiar with California SB-122? (sales and use tax on digital products)

1•NetToolKit•11m ago•1 comments

Build and deploy long-horizon agent like Devin in 10-mins

https://cayu.dev/walkthrough/
2•zamir_akimbekov•13m ago•0 comments

Air-FI: Generating Covert Wi-Fi Signals from Air-Gapped Computers (2020)

https://arxiv.org/abs/2012.06884
2•ilamont•15m ago•0 comments

NanoJev

https://github.com/TianyuCodings/NanoJev
1•shock•16m ago•0 comments

VitePress

https://vitepress.dev/
1•jcbhmr•16m ago•0 comments

Uber ordered to pay $40M for woman killed after driver left her on highway

https://www.bbc.com/news/articles/cqp8d1gw6l8po
1•BeetleB•16m ago•1 comments

Game UI Database

https://www.gameuidatabase.com
1•bookofjoe•19m ago•0 comments

Cognitive resilience helps to predict Alzheimer's dementia

https://www.nature.com/articles/d41586-026-02897-4
1•Brajeshwar•20m ago•0 comments

I Hijacked a Real Artist's Spotify with AI Music. It Was Disturbingly Easy

https://www.404media.co/spotify-ai-music-scam/
1•Brajeshwar•20m ago•0 comments

Non-Obvious Nix Options

https://blog.matthewbrunelle.com/non-obvious-nix-options/
2•Brajeshwar•20m ago•0 comments

Show HN: Timeline, a team project planner with a fast canvas Gantt view

https://github.com/vakahnke/Timeline
2•vakahnke•21m ago•0 comments

The Last Lecture (Randy Pausch) [video]

https://www.youtube.com/watch?v=ji5_MqicxSo
1•deepwater212•22m ago•0 comments

Open-weight models take 56% of token volume, Astra doubles Fable 5.1 spend

https://vercel.com/blog/ai-gateway-production-index-september-2026
1•eyehurtsme•23m ago•0 comments

Meta glasses captured and shared intimate images without consent, lawsuit claims

https://www.latimes.com/business/story/2026-09-18/meta-glasses-captured-shared-intimate-images-wi...
2•cdrnsf•24m ago•0 comments

Tiny-Vedas: a generic accelerator interface for RISC-V, from PyTorch op to GDS

https://github.com/spzbrnmrc/Tiny-Vedas
1•marcux95•25m ago•0 comments

Adam Tooze on AI Regulation [video]

https://www.youtube.com/watch?v=EHU8vXCAsyo
1•verdverm•27m ago•0 comments

Sea lion found beheaded at San Francisco's Ocean Beach, prompting investigation

https://abc7news.com/post/sea-lion-found-beheaded-san-franciscos-ocean-beach-prompting-investigat...
4•mikhael•28m ago•0 comments

Time To Go – an alarm that shows how long your commute takes

https://apps.apple.com/us/app/time-to-go-departure-alarm/id6807446177
1•heejundid•29m ago•0 comments