frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•7mo 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•7mo 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•7mo 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.

Microsoft rolls out hardware-accelerated BitLocker in Windows 11

https://www.bleepingcomputer.com/news/security/microsoft-rolls-out-hardware-accelerated-bitlocker...
1•thunderbong•1m ago•0 comments

LongCat Avatar: Turn Any Photo into a Talking Video

https://www.longcatavatar.net/
1•chengzeyi•1m ago•1 comments

Slate AX: Wi-Fi 6 Gigabit travel router

https://www.gl-inet.com/products/gl-axt1800/
1•cl3misch•1m ago•0 comments

Unintentional Type Theory

https://ncatlab.org/nlab/show/unintentional+type+theory
1•measurablefunc•2m ago•0 comments

Google 2025 recap: Research breakthroughs of the year

https://blog.google/technology/ai/2025-research-breakthroughs/
1•Anon84•7m ago•0 comments

UK to ban deepfake AI 'nudification' apps

https://www.bbc.co.uk/news/articles/cq8dp2y0z7wo
2•GaryBluto•9m ago•1 comments

Ten mistakes marred firewall upgrade at telco, contributing to 2 deaths

https://www.theregister.com/2025/12/19/optus_emergency_outages_cause_report/
1•GaryBluto•11m ago•0 comments

Airplane lands itself after in-flight emergency, a first for aviation automation

https://www.cnn.com/2025/12/23/us/airplane-lands-itself-first-aviation-automation
1•breve•12m ago•0 comments

Starlink satellite fails, polluting orbit with debris and falling toward Earth

https://www.theregister.com/2025/12/23/starlink_satellite_fails_debris/
2•beardyw•15m ago•0 comments

Is This Normal? My new projector's picture is dull even though I paid for 4K

1•SorabAlavi•17m ago•0 comments

Ask HN: Anybody here ever write apps for Windows Phone?

1•ge96•17m ago•0 comments

What happened next:how a rape and murder case was solved 58 years later

https://www.theguardian.com/lifeandstyle/2025/dec/24/what-happened-next-how-a-shocking-and-case-w...
2•zeristor•19m ago•2 comments

I built an AI app for deep research, reverse image search, and price comparison

https://apps.apple.com/us/app/claritycheck-deep-search-ai/id6747683917
1•mamunaso•20m ago•2 comments

Trump Admin Reinvents US Digital Services Program After Elon Musk Fired Experts

https://www.techdirt.com/2025/12/23/trump-admin-reinvents-us-digital-services-program-after-elon-...
2•beardyw•20m ago•0 comments

Appark

https://appark.ai/en
1•xuechen006•22m ago•0 comments

Next JavaScript app is hacked, you just don't know it yet

https://audits.blockhacks.io/audit/your-next-js-app-is-already-hacked
1•block_hacks•22m ago•1 comments

The dpkg shell implementation (by Ian A. Murdock)

https://www.dpkg.org/history/ancient/dpkg-0.93beta.sh
1•fisheuler•22m ago•0 comments

Nobody knows how large software products work

https://www.seangoedecke.com/nobody-knows-how-software-products-work/
1•danielfalbo•24m ago•0 comments

Yes, Termux works just the same on Android-based "dumbphones"

https://old.reddit.com/r/termux/comments/1ptzzyz/in_case_anyones_curious_yes_termux_works_just_the/
1•sipofwater•26m ago•0 comments

Homophily

https://en.wikipedia.org/wiki/Homophily
1•nomilk•30m ago•0 comments

The lightest notes app implementation in 111 loc

https://github.com/antonmedv/textarea
2•birdculture•37m ago•0 comments

AutoCodeBench: Tencent Hunyuan revolutionizes AI programming evaluation

https://medium.com/@leivadiazjulio/autocodebench-how-tencent-hunyuan-revolutionizes-ai-programmin...
2•stareatgoats•39m ago•0 comments

Ultra-Wide Band: A Transformational Technology for the Internet of Things

https://www.eetimes.com/ultra-wide-band-a-transformational-technology-for-the-internet-of-things/
2•fzliu•40m ago•0 comments

Show HN: I built a system that locks you out until you rest

https://www.kensho.zone/
2•kenshozone•40m ago•0 comments

Consciousness May Require a New Kind of Computation FeaturedNeuroscience·

https://neurosciencenews.com/consciousness-computing-ai-30068/
1•_____k•45m ago•0 comments

C++: "We have try...finally at home"

https://devblogs.microsoft.com/oldnewthing/20251222-00/?p=111890
2•HeliumHydride•48m ago•0 comments

Research shows sharing of cavity-causing bacteria may not be only from mothers

https://www.uab.edu/news/health-medicine/research-shows-sharing-of-cavity-causing-bacteria-may-no...
1•thunderbong•49m ago•0 comments

Interactively visualize GitHub Actions Matrix configurations

https://katexochen.github.io/github-matrix-parser/
2•todsacerdoti•51m ago•0 comments

Ask HN: Does the WhatsApp Apple Watch app work for anyone at all?

1•wateralien•56m ago•0 comments

If it's not a hit, switch

https://sive.rs/hitswitch
1•alabhyajindal•56m ago•0 comments