frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

A CLI tool to migrate VSCode themes to Zed

https://www.npmjs.com/package/migrazed
3•enbonnet•37s ago•0 comments

Vets Who Code 2026: What to Know Before You Apply

https://vetswhocode.io/blogs/vwc-2026-what-to-know-before-you-apply
2•mooreds•1m ago•0 comments

ACP Agent Registry in JetBrains IDEs

https://blog.jetbrains.com/ai/2026/01/acp-agent-registry/
3•saikatsg•2m ago•0 comments

Granular API Keys for Buttondown

https://buttondown.com/blog/2026-01-24-api-keys
1•mooreds•2m ago•0 comments

High-Fidelity RPPG Waveform Reconstruction from Palm Videos Using GANs

https://www.mdpi.com/1424-8220/26/2/563
1•PaulHoule•3m ago•0 comments

WebView2, a browser for WinForms in .NET 5

https://grantwinney.com/webview2-a-browser-for-winforms/
1•bob1029•3m ago•0 comments

MetMo Gear Train Fidget Toy

https://www.metmo.co.uk/collections/gear-train
1•cirrus3•5m ago•0 comments

Bonzai gives a bird's eye view of a codebase while AI generates

https://www.bonzai.dev/hn1
1•frootoftheloom•6m ago•1 comments

Is that allowed? Authentication and authorization in Model Context Protocol

https://stackoverflow.blog/2026/01/21/is-that-allowed-authentication-and-authorization-in-model-c...
1•mooreds•7m ago•0 comments

Show HN: Generative UI: MCP Apps, A2UI, and AG-UI

https://github.com/CopilotKit/generative-ui
1•nathan_tarbert•7m ago•0 comments

Multi-Die Assemblies Require More Detailed Test Plan Earlier

https://semiengineering.com/multi-die-assemblies-require-more-detailed-test-plan-earlier/
1•rbanffy•7m ago•0 comments

Show HN: I built a fun interactive tutorial teach about Docker Containers

https://github.com/mraza007/learn-docker
2•mraza007•7m ago•0 comments

Is time a fundamental part of reality? Quiet revolution in physics suggests not

https://theconversation.com/is-time-a-fundamental-part-of-reality-a-quiet-revolution-in-physics-s...
2•Stratoscope•8m ago•0 comments

Show HN: SimpleSVGs – Free Online SVG Optimizer Multiple SVG Files at Once

https://www.simplesvgs.online/
1•firtaet•8m ago•0 comments

Ergo Keyboard Design Reveal

https://read.compose.llc/p/our-keyboard-design-reveal
1•ryanlpeterman•8m ago•0 comments

Molty Report #2 by Robert Scoble

https://twitter.com/Scobleizer/status/2016766016349753662
1•nadis•9m ago•0 comments

MakuluLinux (6.4M Downloads) Ships Persistent Backdoor from Developer's Own C2

https://werai.ca/security-disclosure.html
1•werai•9m ago•0 comments

48 hours without lungs: artificial organ kept man alive until transplant

https://www.nature.com/articles/d41586-026-00239-y
1•geox•10m ago•0 comments

OpenAI's In-House Data Agent

https://openai.com/index/inside-our-in-house-data-agent
2•meetpateltech•10m ago•0 comments

Coffee timing isn't one-size-fits-all

https://examine.com/articles/the-healthiest-time-to-not-drink-coffee/
1•mustaphah•10m ago•0 comments

Apache ADBC is all I need for DB connections

https://github.com/early-signal-tech/adbc/tree/main/adbc-streamlit-demo
1•nhemerson•10m ago•1 comments

Tesla is committing automotive suicide

https://electrek.co/2026/01/29/tesla-committing-automotive-suicide/
4•jethronethro•11m ago•0 comments

Show HN: Suzerain – How much autonomy do you give your AI coding assistants?

https://suzerain.dev
1•amadeuswoo•13m ago•0 comments

Amazon and Google Eat into Nvidia's A.I. Chip Supremacy

https://www.nytimes.com/2026/01/29/technology/amazon-google-nvidia-chips-competition.html
1•ripe•17m ago•1 comments

Ask HN: Is archive.is currently broken for WSJ links?

1•bigwheels•17m ago•0 comments

What those AI benchmark numbers mean

https://ngrok.com/blog/ai-benchmarks/
3•samwho•17m ago•1 comments

The Speed of Time

https://www.brendangregg.com/blog/2021-09-26/the-speed-of-time.html
1•dgramaciotti•18m ago•0 comments

Deploy Moltbot on AWS or Hetzner Securely with Pulumi and Tailscale

https://www.pulumi.com/blog/deploy-moltbot-aws-hetzner/
1•jjwiseman•18m ago•2 comments

Raku MCP SDK

https://github.com/wkusnierczyk/raku-mcp-sdk
1•wkusnierczyk•18m ago•1 comments

Ask HN: Share your favorite digital bookshelves

1•surprisetalk•19m ago•0 comments