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.

Some Projects Need to Fail

https://yusufaytas.com/some-projects-need-to-fail
1•theanonymousone•58s ago•0 comments

Show HN: Yet another agent manager (but we needed it)

https://varinomics.com/logonomic/
1•imakris•2m ago•0 comments

Vyrma – Herhlaup (100% AI music/visuals/audio showing what's possible) [video]

https://www.youtube.com/watch?v=drjltnetKtQ
1•somenameforme•2m ago•0 comments

KDE: The Documentary on Free Software [video]

https://tube.kockatoo.org/w/fFinNHfanLT7aBEb2B8y4c
1•mrkn1•3m ago•0 comments

Securing Rust's supply chain with an extended standard library

https://kerkour.com/rust-supply-chain-security-stdx
1•znano•3m ago•0 comments

Show HN: SunsetLint – find retiring AI model IDs in your GitHub repos

https://www.sunsetlint.com
1•karthikgenz•5m ago•0 comments

Does the Crichton Estate Have a Case?

https://yapern.com/blog/does-the-crichton-estate-have-a-case/
1•15o9o•7m ago•0 comments

Cursor - An update on our recent security incident

https://cursor.com/blog/security-incident-update
1•dsr12•7m ago•0 comments

The Claude Delusion

https://www.netmeister.org/blog/claude-delusion.html
3•janvdberg•10m ago•0 comments

Let the Real Chaos Begin

https://straightlinelogic.com/2026/11/13/let-the-real-chaos-begin-by-robert-gore/
2•ytNumbers•11m ago•0 comments

A visual explanation of reinforcement learning

https://aatishb.com/myriad/how-brains-learn/
2•aatish•15m ago•0 comments

How to Handle Errors in Go

https://blog.jetbrains.com/go/2026/09/02/how-to-handle-errors-in-go/
3•mroche•15m ago•0 comments

Crx-gate – Audit Chrome extensions and predict CWS rejections in 2ms

https://github.com/jaw0r3k/crx-gate
1•jaw0r3k•16m ago•0 comments

Probabilistic Data Structures in Go

https://antonz.org/probabilistic-data-structures/
3•mfrw•17m ago•0 comments

What new PyPI packages import: LLM, httpx, pydantic v2

https://py-code.org/blog/new-pypi-packages-imports/
1•aidotguru•18m ago•0 comments

Engineering Fought Productivity Metrics. AI Ended the Fight

https://nerdstein.net/blog/measuring-developer-productivity/
1•RickJWagner•19m ago•0 comments

One adds and the other subtracts – founders on Ableton

https://roberthenke.com/interviews/ableton.html
1•felixbraun•19m ago•0 comments

Terminal text-editor for I know roughly what I'm looking for, just take me there

https://crates.io/crates/peek-editor
1•lalithaar•21m ago•0 comments

Dwight D. Eisenhower's farewell address

https://en.wikipedia.org/wiki/Dwight_D._Eisenhower%27s_farewell_address
2•embedding-shape•24m ago•1 comments

Advancing science and engineering with Antigravity's /teamwork

https://research.google/blog/advancing-science-engineering-with-antigravitys-teamwork-feature/
2•acossta•25m ago•0 comments

Xdg-ninja: Shell script to check $HOME for unwanted files and directories

https://github.com/b3nj5m1n/xdg-ninja
1•ankitg12•26m ago•1 comments

Guru: An Self Evolving AI framework for Android

https://github.com/GuruProduction/GURUbyUnusLumen
1•introducingGURU•28m ago•0 comments

Show HN: Zigzag, a native Mac browser for people leaving Arc

https://github.com/ZigaoWang/zigzag
1•ZigaoWang•29m ago•0 comments

Building Strong Safety Evaluations

https://www.anthropic.com/research/building-strong-safety-evaluations
1•tosh•31m ago•0 comments

Riding the Rails with America's Last Train-Hoppers

https://www.currentaffairs.org/news/trainhoppers
1•bookofjoe•31m ago•0 comments

Les Espaces D'Abraxas

https://en.wikipedia.org/wiki/Les_Espaces_d%27Abraxas
1•tosh•31m ago•0 comments

FPGA Chronicles

https://hackaday.com/series_of_posts/fpga-chronicles/
1•AlexeyBrin•33m ago•0 comments

Show HN: Sauron – Error tracking and product analytics in one self-hosted stack

https://sauron.splimter.com/
1•splimter•33m ago•0 comments

Show HN: What bootstrapped startups get from credit programs

https://github.com/aidenixai/bootstrapped-credits
1•fantomkoenig•33m ago•0 comments

Those Secret Fonts from the ISA-16 PS/2 Models (Again)

https://int10h.org/blog/2025/07/secret-fonts-isa16-ps2-again/
2•tosh•37m ago•0 comments