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.

Absolute Voter Analysis from North Carolina Guaranteed

https://nc-voter-dashboard.puter.site/
1•amallur•1m ago•0 comments

Show HN: Thinpack.js – Pack tsc into a 9 MiB binary with QuickJS (vs 110M Bun)

https://thinpack.dev/how-it-works
2•ropoz•12m ago•0 comments

P-256 certificate authorities will be blocked by Google Chrome on March 15, 2027

https://twitter.com/iangcarroll/status/2128716458159190202
3•espeed•16m ago•0 comments

SolDocs: Free, encrypted document and pdf editor

https://soldocs.org/
1•dylndyln•16m ago•0 comments

YouTube to pay $415M to air the Oscars in a multiyear deal

https://www.cnbc.com/2026/12/03/youtube-oscars-rights-deal.html
5•kartik147•18m ago•0 comments

Spatie ported their Laravel packages to Rust and Go

https://spatie.be/blog/weve-brought-our-packages-to-rust
2•vpaulus•19m ago•0 comments

Wispr Flow sends desktop app text to the cloud by default (how to turn it off)

https://docs.wisprflow.ai/articles/4678293671-feature-context-awareness
1•reddmachine•19m ago•0 comments

Tell HN: The next frontier in AI is making it sound like a normal person

2•andai•21m ago•0 comments

Rust Anything: Migrate your app to Rust and check it still behaves the same

https://github.com/erkamyaman/rust-anything
2•auraham•22m ago•0 comments

Cryptographic Algorithm Plans and Recommendations

https://letsencrypt.org/2026/12/03/crypto-algorithms.html
1•gpi•22m ago•0 comments

Show HN: ISH – a Rust agent runtime with SDKs for six languages

https://github.com/ish-harness/ish-harness
1•harishmashetty•22m ago•0 comments

Godot Foundation Receives donation of console porting technology from W4 Games

https://godotengine.org/article/godot-foundation-receives-donation-of-console-porting-technology-...
3•danbolt•22m ago•0 comments

Rex+Dot: A Meta-Meta-Harness

https://fugue-labs.ai/blog/the-terminal-pane-was-the-handoff/
1•helsinki•29m ago•0 comments

DeepSeek may keep its most powerful AI models closed-source for safety

https://www.scmp.com/tech/tech-trends/article/3376814/deepseek-may-keep-its-most-powerful-ai-mode...
4•halamadrid•29m ago•1 comments

Nugget: Mainline Linux on a stock Mac Plus with shell, networking and a desktop

https://github.com/ActionRetro/nugget-linux
2•ethanpil•30m ago•0 comments

Earlier this year my 15 year old Google account was also disabled

https://twitter.com/zmh/status/2127237396366328061
3•luu•31m ago•0 comments

ExTerm – Local-first 240Hz terminal for stocks, crypto, DEX and Kalshi

https://github.com/uexxx/ExTerm
1•u_ex•33m ago•0 comments

Show HN: Extract 1000+ leads from an area in single go can pause Play anytime

https://github.com/userisaziz/google-maps-leads-scraper
1•Userisaziz•40m ago•0 comments

A simple Python implementation of the ReAct pattern for LLMs

https://til.simonwillison.net/llms/python-react-pattern
1•auraham•46m ago•0 comments

The script has flipped: AI writes the logic, a compiler derives the plumbing

https://cube-root.com/articles/the-script-has-flipped.html
1•sanjay-sharma•51m ago•0 comments

Bitcoin making post-quantum top priority

https://twitter.com/TheBlueMatt/status/2128598941738709310
1•cft•51m ago•0 comments

Japan court recognises overseas same-sex marriage in first-of-its-kind ruling

https://www.reuters.com/world/japan-court-recognises-overseas-same-sex-marriage-first-of-its-kind...
1•1659447091•52m ago•0 comments

Dan Murphy's customer information accessed in breach of membership platform

https://www.abc.net.au/news/2026-12-03/dan-murphys-my-dans-customer-information-data-breach/10745...
2•Khaine•1h ago•0 comments

DoorDash says customer data exposed in AI-assisted breach

https://www.cnn.com/2026/12/04/business/doordash-customer-data-breach-ai-hnk
1•1659447091•1h ago•0 comments

Woolworths cyber breach exposes data of 14M customers

https://www.news.com.au/technology/online/hacking/woolworths-cyber-breach-exposes-data-of-14-mill...
4•paid_dot_expert•1h ago•3 comments

Haru-122B-A10B-Preview, a 122B-parameter full-duplex speech-to-speech model

https://www.higurashi.ai/haru122b/
1•jzebedee•1h ago•0 comments

Reverse Engineering a Board Game

https://lobste.buzzing.cc/en/
1•Alex5758•1h ago•0 comments

Halo's New Developer Announced by Activision

https://www.ign.com/articles/halo-new-developer-announced-by-activision-methodical-games
1•extasetoi12•1h ago•0 comments

Keeping HTTPS Connections Safe in Chrome

https://blog.google/security/keeping-https-connections-safe-in-chrome/
42•arwt•1h ago•9 comments

How to Stop Generic Type Explosion in Rust

https://edvong.com/stop-generic-type-explosion/
1•ohmycobol•1h ago•0 comments