frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Meta Begins AI-Driven Layoffs, Report Says. Can They Boost the Struggling Stock?

https://www.barrons.com/articles/meta-stock-ai-layoffs-f9dba997
1•1vuio0pswjnm7•3m ago•0 comments

InferenceBench: A Benchmark for Open-Ended Inference Optimization by AI Agents

https://inferencebench.ai/
1•matt_d•4m ago•0 comments

Valkey 9.1 trims memory 10% and pulls search into the core

https://thenewstack.io/valkey-91-cuts-memory/
1•ohjeez•5m ago•0 comments

RFC 9989: What's New in the Latest DMARC Specification

https://dmarcchecker.app/articles/rfc-9989-dmarc-changes
1•awulf•7m ago•0 comments

Tracking Capabilities for Safer Agents

https://arxiv.org/abs/2603.00991
1•matt_d•12m ago•0 comments

The Elements of Power (AI Supply Chain)

https://z-library.im/book/xkRN77V9kg/the-elements-of-power-a-story-of-war-technology-and-the-dirt...
1•mmirshekar•13m ago•0 comments

JAM: DSP audio engine programmable via AI chat

https://jeffsaudiomachines.com/my-story
1•jcward•15m ago•1 comments

Ask HN: Mac users – has Mac-to-Android texting broke for you, since macOS 26.5?

https://discussions.apple.com/verify-human/verify.html?next=/thread/256300699
1•akulbe•15m ago•1 comments

Marketing Help

https://itcrowd.io
1•chrisrichardson•16m ago•0 comments

Anti-Meme Explosion

https://www.jernesto.com/articles/anti_meme_explosion
3•slopranker•18m ago•1 comments

Search engines and me. Is the classic internet dying? [off-topic]

1•nullpwr•20m ago•3 comments

SpaceX files for IPO that could make Elon Musk a trillionaire

https://www.bbc.com/news/articles/cg4pe2953q1o
1•colinprince•21m ago•0 comments

Kickstarter apologises for its criticised adult content rules

https://www.eurogamer.net/kickstarter-adult-content-rules-censorship-apology
1•healsdata•21m ago•0 comments

Primer Pay – micropayments for WordPress on Base requiring Chrome extension

https://wordpress.org/plugins/primer-pay/
1•adrianwaj•24m ago•1 comments

Deep – CLI/REPL for generating and iterating on codebases using DeepSeek

https://github.com/cynchro/deepseekCLI
2•cynchro980•26m ago•0 comments

Show HN: udoc. Dependency-free document extraction in Rust

https://newelh.github.io/udoc/
2•newelh•28m ago•1 comments

Understanding Bitcoin Inscriptions

https://www.learnbitcoin.com/glossary/inscriptions
3•granya•30m ago•1 comments

New Bitcoin Fee Pressure Signal

https://chainquery.com/reports/fee-pressure
2•granya•32m ago•0 comments

Global Biolab Map

https://global-biolab-atlas.netlify.app/
1•sebg•32m ago•0 comments

Staged Publishing for NPM Packages

https://docs.npmjs.com/staged-publishing/
1•jonchurch_•33m ago•1 comments

Moe inference optimizations: 15% lower expert load by request reordering

https://blog.doubleword.ai/moe-expert-coactivations
1•mezark•36m ago•0 comments

Show HN: A fast, free AI rewriter

https://revise.io/ai-rewriter
2•artursapek•39m ago•0 comments

Model Thinking – Percolation Models (2020) [video]

https://www.youtube.com/watch?v=Bs0QuGYrPAI
1•throwaw12•40m ago•0 comments

Shareable text box

https://txt.box
2•shikhar•40m ago•0 comments

Boundaries – A Talk by Gary Bernhardt

https://www.destroyallsoftware.com/talks/boundaries
2•throwaw12•40m ago•0 comments

DOS Zone

https://dos.zone/
24•rglover•48m ago•6 comments

Everything Is Computer

https://turbulence.substack.com/p/everything-is-computer
2•ivm•49m ago•0 comments

Step by step guide: How to install Microsoft Office in any Linux distribution

https://gist.github.com/eylenburg/38e5da371b7fedc0662198efc66be57b
1•nogajun•50m ago•0 comments

China Wants A.I. To Flourish, but Not at the Expense of Jobs

https://www.nytimes.com/2026/05/19/business/china-ai-unemployment.html
5•tom2026hn•51m ago•3 comments

Node.js TypeScript: The future of –experimental-transform-types

https://github.com/nodejs/typescript/issues/51
3•lioeters•55m ago•0 comments