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 Book Is a Book, Not Disembodied Information

https://nntaleb.medium.com/the-book-is-a-book-not-disembodied-information-a3fa583b9e8a
1•herbertl•3m ago•0 comments

DeepSeek Harness Desktop Version

https://github.com/hairyf/deepseek-harness-desktop
1•mehraban•4m ago•0 comments

Show HN: Clair APIs – an active jobs dataset from public ATS boards

https://clair.im/resources/datasets/active-jobs
1•kaanersoy•6m ago•0 comments

The paradox of the perfect code editor (2021)

https://austinhenley.com/blog/perfecteditorparadox.html
1•azhenley•7m ago•0 comments

US to tell partners they must pick sides in AI race with China

https://www.reuters.com/world/china/us-tell-partners-they-must-pick-sides-ai-race-with-china-2026...
4•jonbaer•8m ago•0 comments

Godfather of AI: "If you sleep well tonight..." [video]

https://www.youtube.com/watch?v=iVXerna_D4A
2•Izmaki•8m ago•0 comments

Is AI Coding Effecting Our Mental Health? [video]

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

Socync – Automatic social media posting and scheduling

https://wordpress.org/plugins/socync/
1•sepokroce•12m ago•0 comments

Qwen 3.8 27B at 2x speed on a 5090

https://github.com/jtc268/balto-speedrunner
2•husky8•12m ago•0 comments

We Need to Talk About Leopold [video]

https://www.youtube.com/watch?v=rE75WvOtcu8
1•mgh2•13m ago•0 comments

I built Fusekit, a place to deploy internal web apps

https://fusekit.cloud/
1•harshulgoel•13m ago•0 comments

A 25-year-old video patent just expired, ending a legal headache for Linux

https://www.xda-developers.com/25-year-old-brazilian-video-patent-expired-legal-headache-linux/
5•theanonymousone•15m ago•0 comments

How to get a matmul kernel into the top percentile (worklog)

https://philipbotros.github.io/2026/08/11/matmul-worklog.html
1•gietema•16m ago•0 comments

Show HN: Bluetooth keyboard and Windows 95 cursor on reMarkable

https://twitter.com/martino_piaggi/status/2088659565168509424
1•martinopiaggi•16m ago•0 comments

The origin of vertebrate teeth and evolution of sensory exoskeletons

https://www.nature.com/articles/s41586-025-08944-w
1•BiraIgnacio•16m ago•1 comments

Find duplicate code across nine languages

https://deslop.live/
2•cebert•16m ago•0 comments

Show HN: Velorn – an open-source desktop video editor with MCP agent control

https://github.com/VelornLabs/velorn
1•j_aime•17m ago•0 comments

Show HN: MyFirst.News – AI-generated podcast for curious kids

https://myfirst.news/
1•hakunin•19m ago•1 comments

Ask HN: You have model orders of magnitudes faster than transformer, so?

1•aunekiaru•20m ago•0 comments

Humanists Have Done Nothing Wrong

https://defector.com/humanists-have-done-nothing-wrong
1•rlio•20m ago•0 comments

JOE – Joe's Own Editor

https://joe-editor.sourceforge.io/
1•smartmic•20m ago•0 comments

Show HN: Ansi.md – Everything I know about building modern CLI apps

https://ansi.md
1•gurgeous•22m ago•0 comments

What makes a good puzzle image?

https://composedpuzzles.com/articles/what-makes-a-good-puzzle-image
1•debo_•26m ago•0 comments

One agent, every surface: how we built the Kiro agent harness

https://kiro.dev/blog/one-agent/
2•gmays•27m ago•0 comments

Apple takes down billboard after accusations of 'electronic babysitting'

https://9to5mac.com/2026/08/14/apple-takes-down-billboard-after-accusations-of-normalizing-electr...
1•latexr•28m ago•0 comments

The price of a Costco hot dog has gone up

https://sina.bio/posts/costco-hotdog.html
2•sinab•31m ago•0 comments

CheckMate: An RPG inspired habit tracker with quests, co-op raids, Journal

https://checkmate-daily.com/
1•cionutd•31m ago•1 comments

Empty shelves or lost keys? Recall is the bottleneck for parametric factuality

https://research.google/blog/empty-shelves-or-lost-keys-recall-is-the-bottleneck-for-parametric-f...
1•Anon84•32m ago•0 comments

The Architectural Hostility of Doorknobs (2025)

https://sightlessscribbles.com/posts/doorknob-hostility/
1•jllyhill•33m ago•0 comments

Does vitriol make you feel special?

https://news.harvard.edu/gazette/story/2026/08/does-vitriol-make-you-feel-special/
2•thisislife2•33m ago•0 comments