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.

B2B Copywriting That Drives Demand

https://draft.dev/learn/b2b-copywriting-drives-demand
1•mooreds•1m ago•0 comments

Mode collapse has a name, and he's selling cancer treatment advice on Amazon

https://danielmay.co.uk/posts/cheap-agents-alumni-shirts-and-elias-thorne/
1•danielrmay•1m ago•0 comments

An Accent in the National Anthem

https://coloradosun.com/2026/05/16/immigration-assimilation-melting-pot-baseball-america/
1•mooreds•3m ago•0 comments

JA4 fingerprinting: a better way to identify clients

https://bunny.net/blog/ja4-fingerprinting-a-better-way-to-identify-clients/
1•mooreds•3m ago•0 comments

Are We Asymmetric Yet?

https://areweasymmetricyet.dev/
1•mzyz•3m ago•0 comments

DeepSeek-V4-Flash means LLM steering is interesting again

https://www.seangoedecke.com/steering-vectors/
2•Brajeshwar•4m ago•0 comments

The Transformation of Documents: Repositories Are the New Unit of Knowledge Work

https://blog.sigplan.org/2026/05/15/the-transformation-of-documents-repositories-are-the-new-unit...
1•rbanffy•6m ago•0 comments

Venus Surface Formations Puzzle Planetary Scientists

https://www.universetoday.com/articles/bizarre-venus-surface-formations-puzzle-planetary-scientists
1•rbanffy•6m ago•0 comments

Recent Developments in LLM Architectures: KV Sharing, MHC, Compressed Attention

https://magazine.sebastianraschka.com/p/recent-developments-in-llm-architectures
1•vismit2000•10m ago•0 comments

Wow – these are the AI sources worth paying attention to

https://fav0.com/en/2026-05-16/
1•Justin3go•16m ago•0 comments

Research paper of Atlantic white shark migratory patterns

https://www.int-res.com/abstracts/meps/v580/p1-16
1•realsharkymark•18m ago•1 comments

Someone forked my repo, then submitted a PR written by Claude

https://github.com/dacracot/Klondike3-Simulator/pull/53
2•dacracot•21m ago•1 comments

Math, Bits, Magic, and a Bit of Abnormal C Programming

https://alexeev-dev.bearblog.dev/math-bits-magic-and-a-bit-of-abnormal-c-programming/
1•alexeev-prog•21m ago•1 comments

OpenAI launches ChatGPT for personal finance, will let you connect bank accounts

https://techcrunch.com/2026/05/15/openai-launches-chatgpt-for-personal-finance-will-let-you-conne...
1•mwexler•24m ago•1 comments

Jane Street Interview Simulator

https://janestreet.gg/
1•throwaway2037•24m ago•1 comments

Popular node-IPC NPM package compromised to steal credentials

https://www.bleepingcomputer.com/news/security/popular-node-ipc-npm-package-compromised-to-steal-...
1•Brajeshwar•28m ago•0 comments

Virtual memory explained as a dialogue between a process and the Linux kernel

https://blog.codingconfessions.com/p/virtual-memory
2•wizerno•29m ago•0 comments

Ask HN: Do you still spend time maintaining Claude.md / AGENTS.md files?

2•david_d8912•29m ago•0 comments

MiniPlasma

https://github.com/Nightmare-Eclipse/MiniPlasma
1•throwaway2027•32m ago•0 comments

Why 'Smart' Products Have Started to Look Like the Dumb Choice

https://www.nytimes.com/2026/05/14/magazine/dumb-phones-tvs-retronym-smart-tech.html
2•reaperducer•33m ago•0 comments

MiniPlasma, a Powerful LPE

https://deadeclipse666.blogspot.com/2026/05/
2•throwaway2027•33m ago•0 comments

Why DKIM Fails and How to Fix Every Type of Alignment Error

https://dmarcguard.io/blog/dkim-alignment-failure-fix/
2•meysamazad•34m ago•0 comments

The world model that every AI godfather is racing to figure out

https://fortune.com/2026/04/23/goldman-sachs-ai-world-model-missing-link/
1•fcpguru•35m ago•2 comments

US Government openly schemes to indict Raúl Castro as prelude to invading Cuba

https://www.usatoday.com/story/news/politics/2026/05/16/raul-castro-cuba-indictment-trump-invasio...
3•ck2•36m ago•1 comments

Show HN: Layrr – Point Click and Edit any site

https://www.npmjs.com/package/layrr
1•kiranjohns•36m ago•0 comments

What Is JEPA? Joint Embedding Predictive Architecture Framework Prediction

https://medium.com/@tahirbalarabe2/what-is-jepa-085ca776013a
1•fcpguru•36m ago•0 comments

Google sued for firing staff who tried to follow 'Don't be evil' motto (2021)

https://www.theregister.com/on-prem/2021/12/01/google-sued-for-firing-staff-who-claim-they-tried-...
2•gurjeet•39m ago•0 comments

Show HN: League of Early Adopters – Give product feedback, get product feedback

https://sitenote.ai/league-of-early-adopters
1•sarim•43m ago•1 comments

AI Agents Are Tools, Not Replacements

https://derogab.com/2026/05/15/AI-Tools-Not-Replacements/
1•derogab•49m ago•1 comments

My Mom taught me HTML (and the rest was history)

https://ascii-tweezers.bearblog.dev/my-mom-taught-me-html-and-the-rest-was-history/
1•speckx•49m ago•0 comments