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.

The Creators of VHS Dates Are Ready to Come Clean

https://www.nytimes.com/2026/08/10/arts/vhs-dates-instagram-tapes.html
1•rdmuser•9s ago•0 comments

The Datamaxxers Feeding Their Every Health Move to AI

https://www.wsj.com/tech/ai/the-datamaxxers-feeding-their-every-health-move-to-ai-df0af9b5
1•thm•23s ago•0 comments

Show HN: A website for exploring historical photographs of my city

https://yesterdays.maprva.org/
1•uneekname•45s ago•0 comments

GUI Programming

https://fomenko.fr/devlogs/gui-programming/
1•kooparse•1m ago•0 comments

Google launches Pixel 11 Pro Fold

https://blog.google/products-and-platforms/devices/pixel/pixel-11-pro-fold/
2•thm•1m ago•0 comments

Startups Need an Asymmetry

https://sunilpai.dev/posts/startups-need-an-asymmetry/
1•tosh•1m ago•0 comments

Show HN: Bough – a coding agent that writes a program per turn, not tool calls

https://github.com/andreylukin/bough
1•alukin•2m ago•0 comments

Twisted Physics: Astronomers Solve 30-Year-Old Stellar Mystery

https://public.nrao.edu/news/twisted-physics-astronomers-solve-30-year-old-stellar-mystery/
1•MarcoDewey•3m ago•0 comments

Psysonic: A full-featured desktop client for Navidrome

https://github.com/Psychotoxical/psysonic
1•ju3rg•3m ago•0 comments

Show HN: Tss – Simple Tailscale Host Selector command-line tool

https://github.com/berggren/tss
1•jbn4711•3m ago•1 comments

Qwen3.8 Weights Release in 10 minutes

https://modelscope.cn/models/Qwen/Qwen3.8-2.4T-A95B
3•apitman•3m ago•0 comments

We built a job board where the employers aren't human. Here's what broke

1•Taskpoolai•4m ago•1 comments

AI Startup Cognition in New Funding Talks at $40B Value

https://www.bloomberg.com/news/articles/2026-08-12/ai-startup-cognition-in-new-funding-talks-at-4...
2•closetheloopdev•5m ago•0 comments

Qwen3.8-27B Countdown

https://modelscope.cn/models/Qwen/Qwen3.8-27B
2•apitman•5m ago•0 comments

The Odyssey as AI Allegory

https://unherd.com/2026/08/the-odyssey-as-an-ai-allegory/
1•voxleone•7m ago•0 comments

Show HN: Kery – comments on your PR with a video of the feature working

https://github.com/Kery-HQ/Kery
1•Kvlshah•8m ago•1 comments

Is GitHub Online?

https://isgithub.online/
2•vednig•8m ago•1 comments

Canadians call for U.S. Ambassador Hoekstra to be expelled

https://www.cbc.ca/lite/story/9.7303372
4•colinprince•9m ago•0 comments

Show HN: Capto – alerts when a background job goes quiet, with breadcrumbs

https://capto.run
1•johndory80•9m ago•1 comments

We made Kamal deploy to Kubernetes without touching deploy.yml

https://miget.com/blog/deploy-rails-with-kamal
1•ktaraszk•9m ago•0 comments

Hax – a minimalist, terminal-native coding agent written in C

https://usehax.dev/
2•OleksandrC•10m ago•0 comments

License Plate Reader Searches Should Require a Warrant

https://andrewpwheeler.com/2026/08/12/license-plate-reader-searches-should-require-a-warrant/
12•apwheele•10m ago•0 comments

StashKit – ez QR code generator

https://stashkit.co
1•adStein•10m ago•0 comments

I built an AI website in 3 hours, got 562 sign-ups, and felt lost

https://medium.com/@kakuxwn/i-built-an-ai-website-in-3-hours-it-got-562-sign-ups-in-8-hours-and-l...
1•Irving-AI•12m ago•1 comments

Aperiodic Tilings

https://presheaf.blogspot.com/2021/08/aperiodic-tilings-some1-explainer.html
1•unprovable•13m ago•0 comments

Where Did the Productivity Gains Go?

https://idiallo.com/blog/where-did-the-productivity-gains-go
3•Brajeshwar•13m ago•0 comments

Show HN: TokenMaxxer – Create AI spend leaderboards with your friends

https://tokenmaxxer.xyz
2•SYeomans•15m ago•0 comments

Water-from-Air Systems to Enable Decentralized Water Infrastructure

https://www.kubota.com/news/2026/20260722-001220.html
1•beauzero•15m ago•0 comments

Every disease is a policy failure

https://worksinprogress.co/issue/future-of-medicine/
4•bensouthwood•15m ago•0 comments

I ran the same local LLM on an RTX 5070 laptop and one with an iGPU

https://www.xda-developers.com/same-local-llm-on-rtx-5070-and-integrated-graphics/
1•vednig•16m ago•0 comments