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.

The Cloudflare for Autonomous AI Agents

https://github.com/tkngate/tkngate
1•kilopalisme•26s ago•0 comments

Anthropic Dispatches Staff to D.C., Racing to Resolve AI Export Restrictions

https://www.wsj.com/tech/ai/anthropic-dispatches-staff-to-d-c-racing-to-resolve-ai-export-restric...
1•thm•2m ago•0 comments

I know I can, but should I? Capability vs. Intent in the AI Goldrush

https://aibuilderseries.substack.com/p/i-know-i-can-but-should-i-capability
1•seantheviking•2m ago•0 comments

Asterinas: A production-grade Linux-compatible alternative kernel

https://asterinas.github.io/
1•yankcrime•3m ago•0 comments

UK Brings in Full Social Media Ban for Under-16s

https://deadline.com/2026/06/uk-social-media-ban-under-16s-x-youtube-tiktok-reddit-1236956163/
2•01-_-•5m ago•0 comments

Inside tech elites’ madcap war against the California billionaire tax

https://sfstandard.com/2026/06/13/billionaire-tax-fight-sergey-brin-chris-larsen-mike-moritz-ron-...
1•thm•5m ago•0 comments

Derbyshire officer investigated for using AI to create evidence in cases

https://news.sky.com/story/derbyshire-police-officer-investigated-for-using-ai-to-create-evidence...
2•01-_-•6m ago•0 comments

Do-the-work instead of proof-of-work, for Git hosting

https://blog.legoktm.com/2026/06/14/do-the-work-instead-of-proof-of-work-for-git-hosting.html
1•rapnie•6m ago•0 comments

Ukraine's Zelenskiy discusses peace talks in call with Trump

https://www.reuters.com/world/europe/ukraines-zelenskiy-discussed-war-diplomacy-call-with-trump-a...
1•Soumya_Max•9m ago•0 comments

Show HN: Canopy – parallel, sandboxed Claude Code sessions on macOS

https://github.com/juliensimon/canopy
1•julsimon•9m ago•0 comments

Proof

https://en.wikipedia.org/wiki/Proof_(play)
1•tosh•10m ago•0 comments

Apple PowerMac G4 – Weapon (1999) [video]

https://www.youtube.com/watch?v=lb7EhYy-2RE
1•DesaiAshu•10m ago•0 comments

You don't need React: creating a minimal UI library

https://pedroth.github.io/?p=post/NoNeedReact
1•pedro_movai•11m ago•0 comments

Building a personal meeting assistant that routes through your existing audio

https://techstackups.com/articles/deepgram-personal-meeting-assistant/
1•ritzaco•11m ago•0 comments

A spy in your pocket? How the UK's on-device nude image blocking could work

https://www.techradar.com/vpn/vpn-privacy-security/a-spy-in-your-pocket-how-the-uks-proposed-on-d...
1•nickslaughter02•11m ago•1 comments

Employees are checking out of AI

https://www.asymptotes.ai/notes/the-self-replacement-crisis
1•spinmaster•12m ago•0 comments

Show HN: TimeZoria – fast way to check and compare time zones

https://timezoria.org/
1•TimeZoriaAI•14m ago•0 comments

Abandoning the indie hacking dream for now (2022)

https://www.tinystruggles.com/posts/abandoning_indie_hacking/
1•mmarian•14m ago•0 comments

Shutting down my startup (2025)

https://lachlangreen.substack.com/p/shutting-down-my-startup
1•mmarian•16m ago•0 comments

Oracle Cuts Free Tier Ampere A1 Resources in Half

https://linuxiac.com/oracle-quietly-cuts-free-tier-ampere-a1-resources-in-half/
1•hyruo•16m ago•0 comments

Tell HN: Listening to blog posts is a nice form of self-therapy

1•mmarian•18m ago•0 comments

What the Fuck Happened to Nerds

https://mrmarket.lol/what-the-fuck-happened-to-nerds/
2•vrnvu•18m ago•1 comments

Airis – A zero-install, local AI ecosystem with autonomous PC control

https://github.com/Samael-1976/Airis
1•Samael1976•18m ago•0 comments

Berkadia Data Breach

https://haveibeenpwned.com/Breach/Berkadia
1•rexthonyy•19m ago•0 comments

Show HN: Instant live endpoints your front end needs, from natural language

https://github.com/thomscoder/zero-1
1•thomscoder•20m ago•0 comments

Relativity for Retired Engineers

https://arxiv.org/abs/2605.21660
1•calligram•21m ago•0 comments

Show HN: X – A programming language with switchable memory modes

https://github.com/xdotxxx/x
1•x-xxx•23m ago•0 comments

Nightly Rust Development with Devenv

https://paperless.blog/nightly-rust-devenv
1•l0b0•24m ago•0 comments

Britain announces social media ban for under-16s

https://www.reuters.com/business/media-telecom/britain-expected-set-out-under-16s-social-media-re...
2•austinallegro•24m ago•0 comments

Research Replication Attempt

https://paperless.blog/research-replication-attempt
1•l0b0•24m ago•0 comments