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.

Unsung US civil rights pioneer Claudette Colvin dies, aged 86

https://www.aljazeera.com/news/2026/1/14/unsung-us-civil-rights-pioneer-claudette-colvin-dies-age...
1•Qem•12s ago•0 comments

OSS, Multiplayer, Server-Rendered, Game Engine

https://github.com/cleoselene-engine/cleoselene
1•brunovcosta•36s ago•0 comments

1000 Blank White Cards

https://en.wikipedia.org/wiki/1000_Blank_White_Cards
1•eieio•52s ago•0 comments

Wine 11.0

https://gitlab.winehq.org/wine/wine/-/releases/wine-11.0
1•zdw•1m ago•0 comments

Show HN: Overengineered recommender (activity/game/series/etc.) (57 knobs)

https://activity-architect-1.web.app/
1•arthurstarlake•1m ago•0 comments

Fresh starts: how to thrive when you leave academia

https://www.nature.com/articles/d41586-026-00085-y
1•bookofjoe•3m ago•0 comments

Vintagemapstore: Outline of the world by mapping all lighthouses

https://twitter.com/vintagemapstore/status/2011247533172150512
1•johntfella•4m ago•0 comments

GitHub should charge everyone $1 more per month

https://blog.greg.technology/2025/11/27/github-should-charge-1-dollar-more-per-month.html
1•gregsadetsky•4m ago•1 comments

Death by AI Gibberish

https://elocination.substack.com/p/death-by-ai-gibberish
1•mellosouls•6m ago•0 comments

GLM-Image

https://huggingface.co/zai-org/GLM-Image
2•scosman•6m ago•0 comments

Neo humanoid maker 1X releases world model to help bots learn what they see

https://techcrunch.com/2026/01/13/neo-humanoid-maker-1x-releases-world-model-to-help-bots-learn-w...
1•akg130522•7m ago•0 comments

Solving Factorio with Terraform [video]

https://www.youtube.com/watch?v=uU06vKlCNXk
1•itherseed•9m ago•0 comments

GPT-5.2 series consistently score higher than Opus 4.5 on Art AGI

https://arcprize.org/leaderboard
1•behnamoh•10m ago•0 comments

A gRPC Transport for the Model Context Protocol

https://cloud.google.com/blog/products/networking/grpc-as-a-native-transport-for-mcp
1•mrry•13m ago•0 comments

Mitigating DoS Vulnerability from Unrecoverable Stack Space Exhaustion

https://nodejs.org/en/blog/vulnerability/january-2026-dos-mitigation-async-hooks
1•cebert•14m ago•0 comments

Show HN: Multiplayer Game of Life

https://www.codingame.com/multiplayer/bot-programming/game-of-life-or-death
1•shevis•14m ago•0 comments

The Gleam Programming Language

https://gleam.run/
1•Alupis•20m ago•0 comments

Show HN: Early Compression in High-Dimensional Reality Stops Action

https://jupiterglimpse.substack.com/p/how-premature-compression-in-a-high
1•jupiterglimpse•20m ago•0 comments

Show HN: This guy is smarter that two thirds of (You)

https://lngnmn2.github.io/
1•lngnmn2•28m ago•0 comments

Claudette Colvin, US civil rights pioneer, dies at 86

https://www.bbc.co.uk/news/articles/c1dknn00v3eo
2•mellosouls•29m ago•1 comments

Stop using natural language interfaces

https://tidepool.leaflet.pub/3mcbegnuf2k2i
1•steveklabnik•40m ago•0 comments

Show HN: Cachekit – High performance caching policies library in Rust

https://github.com/OxidizeLabs/cachekit
2•failsafe•40m ago•0 comments

Rust (language) at Volvo Cars [video]

https://www.youtube.com/watch?v=vBofCW8j70A
1•cwt137•47m ago•0 comments

ASCII Clouds

https://caidan.dev/portfolio/ascii_clouds/
17•majkinetor•48m ago•4 comments

Show HN: I made a to-do list web app that nags you via text message

https://tizzy.com/
1•pud•50m ago•0 comments

Families reeling, businesses suffering after ICE raided Ventura cannabis farms

https://www.latimes.com/california/story/2025-12-24/families-reeling-six-months-after-ice-raid-ca...
2•PaulHoule•56m ago•0 comments

US DOJ releases partially redacted documents on Operation Absolute Resolve

https://www.justice.gov/olc/media/1423306/dl
11•Agreed3750•58m ago•0 comments

Map of 'Over the Edge: Death in Grand Canyon by Kenneth Field'

https://www.arcgis.com/apps/webappviewer/index.html?id=9359a0790ffe4bc09edd6b9c17a43b90
1•thunderbong•59m ago•0 comments

A $2,500 full body scan said he was healthy. Then he had a catastrophic stroke

https://www.washingtonpost.com/health/2026/01/13/prenuvo-lawsuit-full-body-scan/
4•ThePhantom•1h ago•5 comments

Show HN: A slsqp solver WASM demo

https://slsqp-wasm.shuo23333.app/
2•shuoli84•1h ago•0 comments