frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Evolution Strategies at the Hyperscale

https://eshyperscale.github.io/
1•E-Reverance•1m ago•0 comments

Why Vinted Is Getting into the Payments Business

https://www.vogue.com/article/why-vinted-is-getting-into-the-payments-business
1•petethomas•6m ago•0 comments

30k watt light bulb experiments

https://www.youtube.com/watch?v=zr-eZ_pLTNI
1•aeonfox•8m ago•0 comments

Act-1: A Robot Foundation Model Trained on Zero Robot Data

https://www.sunday.ai/journal/no-robot-data
1•walterbell•16m ago•0 comments

Hyperscalers Are Hard

https://shvbsle.in/hyperscalers-are-hard/
1•dropbox_miner•22m ago•0 comments

Researchers at Tokamak Energy

https://twitter.com/ExploreCosmos_/status/1991377648384155757
2•keepamovin•23m ago•0 comments

Quantum computing: too much to handle

https://scottaaronson.blog/?p=9325
2•qnleigh•23m ago•1 comments

Xhisper – Dictation for Linux

https://github.com/imaginalnika/xhisper
2•cc_ashby•28m ago•0 comments

Zork is now open source

https://blog.zarfhome.com/2025/11/zork-is-open-source
3•susam•30m ago•1 comments

Wall Street is banking on too many Federal Reserve rate cuts, Vanguard warns

https://www.ft.com/content/22b5e0e8-1db9-44c3-bade-11269f2fa670
2•zerosizedweasle•32m ago•0 comments

Cool similarities between chaldean and arabic

https://nasmaofny.com/similarities-between-aramaic-and-arabic/
1•marysminefnuf•34m ago•0 comments

Share with everyone the trialable Nano Banana Pro website – VGenie

https://vgenie.ai/image-models/nano-banana-pro
1•funny_ai•36m ago•1 comments

Bridging GNNs and LLMs: A Survey and Unified Perspective

https://infoscience.epfl.ch/server/api/core/bitstreams/7e6f86c2-04cf-4866-8d2e-5ad08272ca0b/content
2•sadid•43m ago•0 comments

Zoox kicks off robotaxi rides in San Francisco

https://www.cnbc.com/2025/11/18/zoox-begins-offering-robotaxi-rides-in-san-francisco-t.html
2•gmays•46m ago•0 comments

Swedish musician teaches a highly intelligent octopus to play piano

https://www.classicfm.com/music-news/videos/swedish-musician-teaches-octopus-piano/
2•NoRagrets•49m ago•3 comments

Show HN: Pynote – Embed Python code and editor, notebooks in any HTML page

https://getpynote.net/
2•laurentabbal•54m ago•0 comments

New video shows solar thermal water tank manufacturing in Hawaii

https://www.youtube.com/watch?v=6MI0ZMWlhmw
1•LMSolar•1h ago•1 comments

AI in Practice Survey

https://theoryvc.com/blog-posts/ai-in-practice-survey
3•gmays•1h ago•0 comments

Show HN: Nano Banana Pro – Real Physics, Real Lighting, 4K Quality in Seconds

https://nanobanana-pro.co/
2•nicohayes•1h ago•3 comments

A "cooked" Computer Science grad's perspective

https://pomogaev.ca/cooked/
2•arrakark•1h ago•2 comments

Meta Sam 3D – Image to Mesh Model

https://ai.meta.com/sam3d/
1•tsnl•1h ago•0 comments

Former Google chief accused of spying on employees through account 'backdoor'

https://www.latimes.com/business/story/2025-11-20/former-google-chief-accused-of-spying-on-employ...
5•coloneltcb•1h ago•1 comments

Show HN: Dream Decoder AI – Jungian dream analysis with 3D visualization

1•brandonmillsai•1h ago•0 comments

How Children die from Indian cough syrup

https://www.reuters.com/business/healthcare-pharmaceuticals/safety-lapses-weak-oversight-how-chil...
4•petethomas•1h ago•0 comments

US cancels October's employment report after shutdown prevented data collection

https://www.reuters.com/world/us-canceling-octobers-employment-report-after-shutdown-prevented-da...
5•mgh2•1h ago•1 comments

Show HN: Tablecraft – instant in-browser table converter/exporter

https://tablecraft-web.vercel.app/
2•tultra•1h ago•2 comments

PolyAgora: A natural-language multi-agent OS built through conversation

https://github.com/Takeshi-Sakamoto5/PolyAgora
1•takeshi_sakamo•1h ago•1 comments

The Saudification of America is under way

https://www.theguardian.com/commentisfree/2025/nov/18/saudi-arabia-america-jamal-khashoggi
5•JumpCrisscross•1h ago•3 comments

Gesture Control for Smartwatches: Would Anyone Use This?

3•WayneFung1992•1h ago•0 comments

Netflix, Comcast and Paramount Submit Warner Bros. Discovery Bids

https://www.nytimes.com/2025/11/20/business/media/warner-discovery-bids-paramount-netflix-comcast...
3•ChrisArchitect•1h ago•1 comments