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.

Ubuntu website and repositories outage due to DDoS

https://twitter.com/VECERTRadar/status/2049934376272810445
1•jtlebigot•1m ago•0 comments

Don't Delegate Understanding

https://stephango.com/understand
1•sebg•2m ago•0 comments

Apple's iPhone revenue jumps to $57B despite chip shortagesiPhone

https://www.theverge.com/tech/921527/apple-iphone-revenue-q2-2026-earnings
1•mgh2•2m ago•0 comments

Apple beats on Q2 earnings, thanks to China and iPhone sales

https://finance.yahoo.com/video/apple-beats-on-q2-earnings-thanks-to-china--iphone-sales-20405227...
2•mgh2•3m ago•1 comments

OxyContin maker Purdue Pharma dissolves after judge approves criminal sentence

https://apnews.com/article/oxycontin-purdue-pharma-criminal-sentence-settlement-b8aa94eaab3d9d8ef...
2•embedding-shape•7m ago•0 comments

Discovering Hard Disk Physical Geometry Through Microbenchmarking (2019)

https://blog.stuffedcow.net/2019/09/hard-disk-geometry-microbenchmarking/
1•TapamN•7m ago•0 comments

AI outperforms doctors in Harvard trial of emergency triage diagnoses

https://www.theguardian.com/technology/2026/apr/30/ai-outperforms-doctors-in-harvard-trial-of-eme...
3•pseudolus•9m ago•0 comments

Multi-agent systems as distributed software

https://fulcrum.inc/2026/04/30/multi-agent-as-distributed.html
2•etherio•10m ago•0 comments

Embodied AI: China's ambitious path to transform its robotics industry

https://merics.org/en/report/embodied-ai-chinas-ambitious-path-transform-its-robotics-industry
1•atlasunshrugged•13m ago•0 comments

Show HN: Pu.sh – a full coding-agent harness in 400 lines of shell

https://pu.dev/
6•nahimn•14m ago•0 comments

Mapping the U.S. Federal Government Ecosystem for Cybersecurity

https://strategyofsecurity.com/p/mapping-the-u-s-federal-government-ecosystem-for-cybersecurity-p...
2•mooreds•14m ago•0 comments

Big Tech will spend nearly $700B on AI this year

https://fortune.com/2026/04/30/big-tech-hyperscalers-will-spend-700-billion-on-ai-infrastructure-...
5•mgh2•16m ago•2 comments

Biological Weapons Convention

https://en.wikipedia.org/wiki/Biological_Weapons_Convention
2•dave1010uk•17m ago•0 comments

I'm Powered by Linux

https://forkingmad.blog/powered-by-linux/
1•SoniaPanda•18m ago•1 comments

Nothing Is Impossible in Software Engineering

https://twitter.com/ichebykin/status/2049948519096811525
1•mifydev•18m ago•0 comments

Running creative online bots with ten thousand followers

https://stefanbohacek.com/blog/on-running-creative-online-bots-with-ten-thousand-followers/
2•sieste•19m ago•0 comments

Pie Thieves

https://www.endnotes.net/pie-thieves/
1•zdw•19m ago•0 comments

SimpleX Channels, SimpleX Network Consortium and Community Crowdfunding

https://simplex.chat/blog/20260430-simplex-channels-v6-5-consortium-crowdfunding-freedom-of-speec...
1•pmw•20m ago•0 comments

Patch Your Kernel NOW: 732byte Python rootkit, cracks all distros since 2017

https://github.com/rootsecdev/cve_2026_31431
3•cednore•20m ago•1 comments

Hard budget enforcement for AI agents – blocks before the API call

https://awx-shredder.fly.dev
1•awxglobal•22m ago•0 comments

GCC 16.1 Released

https://lwn.net/Articles/1070649/
2•kazu11max17•22m ago•0 comments

Blue Origin certainly has ambitious launch targets for New Glenn

https://arstechnica.com/space/2026/04/blue-origin-certainly-has-ambitious-launch-targets-for-new-...
3•LorenDB•23m ago•0 comments

The most severe Linux threat to surface in years catches the world flat-footed

https://arstechnica.com/security/2026/04/as-the-most-severe-linux-threat-in-years-surfaces-the-wo...
3•AndrewDucker•24m ago•0 comments

Passlib 1.7.4 incompatible with bcrypt >= 4.3 ausing runtime warnings

https://bugs.launchpad.net/kolla-ansible/+bug/2150764
2•shayangeedook•25m ago•0 comments

Hackers are actively exploiting a bug in cPanel and WHM

https://techcrunch.com/2026/04/30/hackers-are-actively-exploiting-a-bug-in-cpanel-used-by-million...
4•dotmanish•25m ago•0 comments

Cerberus Anti-theft is stalkerware: a reverse engineering

https://hexproof.dev/datagrams/cerberus-stalkerware-re/
1•ImJasonH•27m ago•0 comments

Prolific Chinese State-Sponsored Contract Hacker Extradited from Italy

https://www.justice.gov/opa/pr/prolific-chinese-state-sponsored-contract-hacker-extradited-italy
5•737min•27m ago•1 comments

The Iran internet blackout has entered its second month

https://mastodon.social/@netblocks/116481109110598719
5•us321•32m ago•0 comments

iOS 27 to bring AI inside the Camera app, iPhone shutdown problem

https://9to5mac.com/2026/04/30/happy-hour-588/
1•omer_k•34m ago•0 comments

My Daughter Died at 32. My Devices Won't Let Me Rest

https://www.wsj.com/lifestyle/relationships/my-daughter-died-at-32-my-devices-wont-let-me-rest-50...
6•impish9208•35m ago•2 comments