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.

Launching a Free file converter APP

https://play.google.com/store/apps/details?id=com.konvert.fileconverter&hl=en_US
1•uzairhussainsm•1m ago•1 comments

The secret maths problems hidden throughout Japan

https://www.newscientist.com/article/2599802-the-secret-maths-problems-hidden-throughout-japan/
1•Breadmaker•1m ago•0 comments

Microsoft suggests Registry hack to work around hotpatch woes

https://www.theregister.com/os-platforms/2026/11/20/microsoft-suggests-registry-hack-to-work-arou...
2•Bender•2m ago•0 comments

China attackers deployed AI agents that autonomously found and exploited a 0-day

https://www.theregister.com/security/2026/11/20/china-attackers-deployed-ai-agents-that-autonomou...
1•Bender•2m ago•0 comments

Show HN: Fundamental, page elements that bend a shared field and read it back

https://fundamentalengine.com/
1•zachshallbetter•3m ago•0 comments

Trump FCC declares California isn't allowed to enforce phone service rules

https://arstechnica.com/tech-policy/2026/11/fcc-orders-california-to-stop-enforcing-phone-service...
1•Bender•4m ago•0 comments

Amazon closes in on $7.5B deal to buy AI startup Decart

https://www.ft.com/content/dc7749dc-0f40-44a6-80bc-7f747d383616
1•notfried•9m ago•0 comments

Arctic: A Practical Lock-Free Adaptive Radix Tree

https://www.usenix.org/conference/osdi26/presentation/ni
1•grep_it•9m ago•0 comments

Ask HN: Advice on switching careers within software dev?

2•akudha•11m ago•0 comments

Revolt-rs: the Revolt event loop as a native PHP extension, up to 16x faster

https://blog.daniil.it/2026/11/21/revolt-rs/
1•danogentili•11m ago•0 comments

P&G to Cut Up to 30% of Product-Supply Workers Amid Automation Push

https://www.wsj.com/business/p-g-to-cut-up-to-30-of-product-supply-workers-amid-automation-push-8...
1•frank_clover•11m ago•0 comments

Corporate Gaslighting, the "All-in-One" Phenomenon, and the Agentic Reassembly

https://medium.com/@smoking2man/corporate-gaslighting-the-all-in-one-phenomenon-and-the-agentic-r...
2•Aldus551•12m ago•0 comments

A 4x faster async drop-in replacement for the MongoDB PHP extension

https://blog.daniil.it/2026/11/21/mongo-php-async-driver/
1•danogentili•12m ago•0 comments

Study points toward a potential blood test for CTE

https://www.wbur.org/news/2026/11/19/boston-university-study-biomarker-cte
1•gmays•13m ago•0 comments

Fine-tuning vision models: how expertise forms and how forgetting happens

https://andrewxhill.com/from-clip-to-bioclip-2/
2•andrewxhill•16m ago•0 comments

The New Yorker, John Hersey, and the Horrors of Hiroshima

https://engelsbergideas.com/portraits/john-hersey-and-the-horrors-of-hiroshima/
2•fanf2•17m ago•0 comments

Can you patch Ruby faster now?

https://paagman.dev/can-you-patch-ruby-faster-now/
1•Kerrick•18m ago•0 comments

"Race realism" isn't good science. Modern genetics does not support it

https://theconversation.com/race-realism-why-scientifically-flawed-approach-fails-to-present-a-re...
2•u1hcw9nx•18m ago•0 comments

C++26 is done (again) – Trip November 2026 ISO C++ standards meeting

https://herbsutter.com/2026/11/21/c26-is-done-again-trip-report-november-2026-iso-c-standards-mee...
3•flyingpast•22m ago•1 comments

Show HN: Pencil – LLM writes whiteboard lessons as JSON, a browser draws them

https://github.com/VBS2004/pencil
2•VBS2004•26m ago•0 comments

Countries open path towards 'killer robots' pact

https://www.afp.com/en/countries-open-path-towards-killer-robots-pact
4•geox•26m ago•0 comments

Show HN: Scalar codebook quantization, an interactive guide

https://myli.page/posts/202611211800-how-a-weight-fits-in-4-bits/
1•tslmy•26m ago•0 comments

The People's Code

https://thepeoplescode.us/
1•acmeyer9•27m ago•2 comments

Create Your Own VPN Server in 2026: Complete Setup Guide

https://blog.openvpn.net/create-your-own-vpn-server-in-2026-complete-setup-guide
1•DeepLogin•27m ago•0 comments

The Aims of Education – Alfred North Whitehead [pdf]

https://la.utexas.edu/users/hcleaver/330T/350kPEEwhiteheadaimstable.pdf
1•tylerdane•28m ago•1 comments

Upload2Shell · CVE-2026-125396

https://pwn.ai/upload2shell
4•f311a•30m ago•1 comments

Google makes it easier to use Gemini without training AI on your data

https://arstechnica.com/ai/2026/11/google-will-let-you-opt-out-of-ai-training-in-gemini-without-l...
1•doodlesdev•30m ago•0 comments

Show HN: Test product ideas on synthetic customers (open-source SSR skill)

https://github.com/AryamanSi17/synthetic-consumer-research
1•aryamansi1712•32m ago•0 comments

Abusing Git Trailers for Faster CI/CD

https://jonathannen.com/abusing-git-trailers/
2•jwilliams•32m ago•0 comments

Free Culture (Lawrence Lessig, Oscon) (2002)

https://www.youtube.com/watch?v=JDYiMcXRfGs
1•Tomte•34m ago•0 comments