frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Apple's lobbying effort saves it from new App Store rules

https://appleinsider.com/articles/26/04/27/apples-massive-lobbying-effort-saves-it-from-new-app-s...
1•latexr•18s ago•0 comments

Intel has best month ever, after years of losing to TSMC and Nvidia

https://www.cnbc.com/2026/04/30/intel-has-best-month-ever-after-years-of-losing-to-tsmc-and-nvidi...
1•elsewhen•5m ago•0 comments

NASA chief says he's in the camp of 'make Pluto a planet again'

https://www.space.com/astronomy/pluto/nasa-chief-jared-isaacman-says-hes-fighting-for-pluto-i-am-...
2•OutOfHere•14m ago•1 comments

Why Being Curious and Asking Questions Are Essential in Life [Book]

https://www.forbes.com/sites/marybethgasman/2026/04/30/why-being-curious-and-asking-questions-is-...
1•stmw•16m ago•0 comments

Passwordless Root Access in qubes

https://doc.qubes-os.org/en/latest/user/security-in-qubes/vm-sudo.html
2•negura•19m ago•0 comments

DeepSeek V4 Flash and V4 Pro in Microsoft Foundry

https://techcommunity.microsoft.com/blog/azure-ai-foundry-blog/introducing-deepseek-v4-flash-and-...
2•zhoutong•21m ago•1 comments

America's New Surveillance Dragnet

https://www.wsj.com/politics/policy/immigration-ice-arrests-surveillance-6f1cef64
7•julienchastang•38m ago•1 comments

Interactive physics moving block structures in Minecraft

https://github.com/ryanhcode/sable
3•LelouBil•42m ago•1 comments

Create Aeronautics

https://modrinth.com/mod/create-aeronautics
3•LelouBil•43m ago•0 comments

Find and fix any Windows error

https://errorcodereference.com/
1•megamike•45m ago•1 comments

Metro Hits 1.0.0 – Compile-Time Dependency Injection Framework for Kotlin

https://github.com/ZacSweers/metro/releases/tag/1.0.0
2•TheWiggles•48m ago•0 comments

Where the Money Is Coming From

https://www.warman.life/blog/2026-04-30-where-the-money-is-coming-from/
3•shaunistyping•48m ago•0 comments

OpenAI to use third-party cookies to advertise products

https://openai.com/policies/us-privacy-policy/
3•shpat•49m ago•0 comments

I made a weird AI kids app (Little Chicken)

https://apps.apple.com/us/app/kids-games-fun-little-chicken/id6759822036
1•mrWONDERFULguy•50m ago•0 comments

Cursor's 'Rogue' AI agent goes haywire, deletes company's database [video]

https://www.youtube.com/watch?v=XBVoLSXaAHA
2•mgh2•51m ago•2 comments

Ask HN: Any good ways to extend Codex sessions?

1•tabmate•1h ago•2 comments

AI Value Capture – The Shift to Model Labs

https://newsletter.semianalysis.com/p/ai-value-capture-the-shift-to-model
1•nsoonhui•1h ago•0 comments

Show HN: Vibe, a single-header C networking library for Linux

https://github.com/xtellect/vibe
3•enduku•1h ago•0 comments

Ask HN: Does Claude use 'prior' in a Bayesian sense more than English?

1•slake•1h ago•0 comments

Ruby Gems and Go Modules Impersonate Dev Tools to Steal Secrets and Poison CI

https://socket.dev/blog/malicious-ruby-gems-and-go-modules-steal-secrets-poison-ci
3•ilreb•1h ago•0 comments

A Three Horizons Framework for Government Reform

https://www.eatingpolicy.com/p/a-three-horizons-framework-for-government
2•brandonb•1h ago•0 comments

Health care costs reach a breaking point

https://newsroom.heart.org/news/health-care-costs-reach-a-breaking-point
6•geox•1h ago•0 comments

10-day training cycle that Sabastian Sawe used to run a sub-2 marathon

https://www.runnersworld.com/uk/training/marathon/a71027488/sebastian-sawe-training-cycle/
2•canucker2016•1h ago•3 comments

The Fick equation and your heart

https://www.empirical.health/blog/fick-equation-vo2max-heart/
1•brandonb•1h ago•0 comments

Show HN: Winpodx – run Windows apps on Linux as native windows

https://github.com/kernalix7/winpodx
18•kernalix7•1h ago•2 comments

Claude Code is going to fail you eventually, and you need to be ready

https://claudefolio.com/blog/claude-code-is-going-to-fail-you-eventually-and-you-need-to-be-ready
2•VaderMaster•1h ago•0 comments

US-Indian Spacecraft Captures Mexico City Subsidence

https://science.nasa.gov/photojournal/us-indian-spacecraft-captures-mexico-city-subsidence/
2•hsuresh•1h ago•0 comments

Codex subscription in an Electron app and Chromium Browser

https://github.com/chillysbabybackribs/Goldenboy-YouTube-Reddit-Extractor
1•goldenboychrome•1h ago•1 comments

Termshot: Create screenshots based on terminal command output

https://github.com/homeport/termshot
2•sea-gold•1h ago•0 comments

KDE at 30

https://kde.org/anniversaries/30/
2•kristianp•1h ago•0 comments