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.

What life is like when you have an unfortunate surname

https://www.bbc.co.uk/news/articles/cql7eennngvo
1•YeGoblynQueenne•29s ago•0 comments

ClearQ Ticket System. Customer support, reimagined

https://www.getclearq.com/
1•hackedapostle•59s ago•0 comments

French Mobile Network Datasets

https://tech.marksblogg.com/france-open-mobile-network-data.html
1•marklit•1m ago•0 comments

How Pants

https://www.amacad.org/daedalus/how-pants
1•jruohonen•1m ago•0 comments

The Fire Framework

https://dvcoolarun.com/2026/04/18/Fire-Framework.html
2•dvcoolarun•2m ago•0 comments

Show HN: GAI, A flexible and idiomatic GO Agent framework

https://github.com/HecoAI/gai
1•samuel_kx0•3m ago•0 comments

India Won't Require Apple to Preinstall Government ID App on iPhones

https://www.macrumors.com/2026/04/17/india-preinstall-government-id-app/
1•Brajeshwar•3m ago•0 comments

SQLite prefixes its temp files with `etilqs_`

https://avi.im/blag/2026/etilqs/
1•avinassh•3m ago•0 comments

Tired of waiting for your EV to charge up? Chinese company has a novel solution

https://www.npr.org/2026/04/18/nx-s1-5788990/ev-battery-charge-long-china-nio
1•tatersolid•4m ago•0 comments

Show HN: Rapid-MLX – Run local LLMs on Mac, 2-3x faster than alternatives

https://github.com/raullenchai/Rapid-MLX
1•raullen•10m ago•0 comments

I calculated AI image generation cost for 18 models

https://komelin.com/blog/ai-image-generation-cost-analysis
1•kka•12m ago•1 comments

Ask HN: How do you use Local LLMs? (April 2026)

1•Hixon10•16m ago•0 comments

Ask HN: Why did your government attack Iran?

1•roschdal•17m ago•4 comments

Jonathan the Tortoise Crypto Death Hoax and True Age Question

https://vincemancini.substack.com/p/jonathan-the-tortoise-crypto-death-real-age
1•coloneltcb•19m ago•0 comments

What does annoy you the most with AI today?

1•ofabioroma•20m ago•1 comments

I Automated Oracle 19.28 Database and Grid Patching with Ansible

https://dincosman.com/2026/04/18/ansible-oracle-patching/
1•osmandinc•22m ago•1 comments

Reverse Engineering ME2's USB with a Heat Gun and a Knife

https://github.com/coremaze/ME2-Writeup
1•Bawoosette•22m ago•0 comments

How We Build Effective Agents: Barry Zhang, Anthropic [video]

https://www.youtube.com/watch?v=D7_ipDqhtwk
5•Anon84•23m ago•0 comments

Real-time poker engine (replay, telemetry, AI players)

https://oxyklon.net/portal
2•naydref•25m ago•1 comments

2001: A TV Series

https://owl.billpg.com/2001-a-tv-series/
1•billpg•25m ago•0 comments

Gpgwrap

https://0ut3r.space/2026/04/06/gpgwrap/
1•h0ek•27m ago•0 comments

Recent advances push Big Tech closer to the Q-Day danger zone

https://arstechnica.com/security/2026/04/while-some-big-tech-players-accelerate-pqc-readiness-oth...
1•bookmtn•29m ago•0 comments

Ask HN: Is an open source initiative for students worth it?

1•wasimsk•30m ago•0 comments

Show HN: 60-Second Hack – a daily hacker typing puzzle

https://hack-game-pi.vercel.app/
1•Kodaren1338•33m ago•1 comments

Trump deal with IRS could see him given $14B in taxpayer money

https://www.9news.com.au/world/donald-trump-irs-lawsuit-suing-10-billion-dollars-tax-office-usa-p...
10•johnbarron•38m ago•0 comments

TPM 2.0 is actually cool

https://apas.tel/blog/tpm-is-cool
1•art049•39m ago•1 comments

Native IPv6 Kubernetes for true edge routing

https://henrikgerdes.me/blog/2026-04-k8s-native-ipv6-edge/
1•jandeboevrie•41m ago•0 comments

The UK needs to discuss the risk of a new Pacific War over Taiwan

https://ukdefencejournal.org.uk/the-uk-needs-to-discuss-the-risk-of-a-new-pacific-war-over-taiwan/
1•Bender•43m ago•0 comments

I visited drone factories in Ukraine

https://ukdefencejournal.org.uk/i-visited-drone-factories-in-ukraine/
3•Bender•43m ago•2 comments

Raspberry Pi OS ends open-door policy for sudo

https://www.theregister.com/2026/04/15/raspberry_pi_os_sudo/
2•Bender•44m ago•0 comments