frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Your AI Frustration Is My Opportunity

https://metedata.substack.com/p/012-your-ai-frustration-is-my-opportunity
2•young_mete•4m ago•0 comments

Data Analysis with Vector Functional Programming (2016) [video]

https://www.youtube.com/watch?v=VOeiSMFp2uA
1•tosh•4m ago•0 comments

I did everything but my app doesn't attract anyone

https://apps.apple.com/tr/app/finverdict-crypto-stocks/id6757922254
2•ooozooo•7m ago•1 comments

Confessions of a Gasoline Huffer

https://www.thestranger.com/features/confessions-of-a-gasoline-huffer-484025/
2•CharlesW•9m ago•0 comments

Show HN: Amazon-ready books from simple HTML: EPublish

https://frequal.com/epublish/
1•TeaVMFan•9m ago•1 comments

SixForty – photo sharing for the Apple II

https://www.colino.net/wordpress/sixforty-photo-sharing-for-the-apple-ii/
1•wanderingjew•12m ago•0 comments

Show HN: Make your own Tiled Words puzzles

https://tiledwords.com/builder
1•paulhebert•20m ago•0 comments

EU weighs restricting use of US cloud platforms to process sensitive gov data

https://www.osnews.com/story/144943/eu-weighs-restricting-use-of-us-cloud-platforms-to-process-se...
8•abdelhousni•23m ago•1 comments

Bieke Depoorter – A Magnum photographer exploring portraiture

https://www.youtube.com/watch?v=NGZDrpCTKRM
1•fallinditch•24m ago•0 comments

Raw Truthful Nothing Hidden Musician Bares All with New Album

https://www.youtube.com/playlist?list=PLiykFHje0lV5iOXB1WegGtZ3yOS11HtP1
1•keithgdarley•25m ago•1 comments

Show HN: Free guide to CPython internals for experienced Python developers

https://pynsights.vercel.app
1•MarinhoD•36m ago•0 comments

Nostalgic Electronics Kits Central

https://www.nostalgickitscentral.com/
3•cf100clunk•38m ago•1 comments

Human-Like Document AI

https://pageindex.ai/
2•DeathArrow•40m ago•1 comments

What AI Did to My College Class

https://www.nytimes.com/2026/05/17/opinion/chatgpt-ai-college-school-graduation.html
7•billyp-rva•41m ago•0 comments

The quiet grief of adult friendship

https://timesofindia.indiatimes.com/blogs/civil-irony/the-quiet-grief-of-adult-friendship/
6•luispa•41m ago•1 comments

Meta deletes popular 1M follower account after Kuwaiti request

https://twitter.com/ryangrim/status/2055992439031185782
21•bhouston•42m ago•3 comments

The occasional ECONNRESET

https://movq.de/blog/postings/2026-05-05/1/POSTING-en.html
10•zdw•43m ago•1 comments

Americans Are Smashing Flock Cameras

https://stateofsurveillance.org/news/flock-cameras-destroyed-nationwide-ice-backlash-2026/
133•rolph•44m ago•92 comments

The Protein Shortage Is Coming

https://www.theatlantic.com/culture/2026/05/protein-powder-shortage/687193/
4•paulpauper•45m ago•0 comments

A chat-less interface to collaborate with LLM agents directly from your files

https://github.com/piqoni/piqo-extension
1•lexoj•45m ago•0 comments

Is the Modern NBA Breaking Its Stars?

https://www.theringer.com/2026/05/14/nba/nba-injuries-leg-calf-hamstring-achilles-data
1•paulpauper•46m ago•0 comments

Reviewing so called Pull Requests at $dayjob

https://rkta.de/dayjob-pr-review.html
2•g0xA52A2A•48m ago•0 comments

Oto Smart Sprinkler Review (2026): Solar-Powered and Simple to Use

https://www.wired.com/review/oto-smart-sprinkler/
1•joozio•48m ago•0 comments

TinyStories-260K running locally on a stock Game Boy Color

https://github.com/maddiedreese/gbc-transformer
1•medbar•49m ago•0 comments

Google Maps Satellite Imagery of Palisades Fire Area Reverts

https://twitter.com/royolone/status/2055465365312815543
4•kingleopold•51m ago•1 comments

Engineering a Safer World: Risk Modelling – and Safety Engineering? – For AI Lo

https://www.lesswrong.com/posts/mL5asdegoa56CkqgJ/engineering-a-safer-world-risk-modelling-and-sa...
2•joozio•52m ago•0 comments

A Parliament of Owls and a Murder of Crows: How Groups of Birds Got Their Names

https://www.themarginalian.org/2024/01/04/brian-wildsmith-birds-company-terms/
4•bookofjoe•52m ago•0 comments

Getting Matured Through Learning

2•Shailendra_S•52m ago•0 comments

How Reese Witherspoon built $900M company from a problem Hollywood wouldn't fix

https://fortune.com/2026/05/17/reese-witherspoon-hello-sunshine-900-million-sale-founder-mindset/
4•happy-go-lucky•55m ago•0 comments

Rethinking the Luddites in the Age of AI

https://www.newyorker.com/books/page-turner/rethinking-the-luddites-in-the-age-of-ai
4•simonebrunozzi•59m ago•0 comments