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.

Show HN: OpenRegistry – Open global company registry search

https://github.com/sophymarine/openregistry
1•richardwong1•53s ago•0 comments

A cache-friendly IPv6 LPM with AVX-512 (linearized B+-tree, real BGP benchmarks)

https://github.com/esutcu/planb-lpm
1•debugga•53s ago•0 comments

Introductory Biology: Evolutionary and Ecological Perspectives

https://pressbooks.umn.edu/introbio/
1•rolph•5m ago•0 comments

Is AI pressure making developer burnout worse? (anonymous survey inside)

1•rechargedaily•12m ago•0 comments

WebUSB Extension for Firefox

https://github.com/ArcaneNibble/awawausb
1•luu•16m ago•0 comments

Autonomous Testing and AI‑Driven Tooling Are Redefining Developer Productivity

https://semanticed.online/editorial-why-autonomous-testing-and-ai-driven-tooling-are-redefining
2•alihassaanmug•19m ago•0 comments

Qwen releases Qwen3-Embedding-0.6B

https://huggingface.co/Qwen/Qwen3-Embedding-0.6B
2•arabicalories•19m ago•1 comments

Box-shadow is no alternative to outline

https://www.matuzo.at/blog/2026/box-shadow-no-alternative-to-outline
1•salkahfi•20m ago•0 comments

Show HN: Ribbon, a Native iOS Linkding Client

https://apps.apple.com/us/app/ribbon-a-linkding-client/id6762416055
1•cdrnsf•21m ago•0 comments

Wonderful Life: The Burgess Shale and the Nature of History[pdf]

https://ia601309.us.archive.org/20/items/historyDEEPWEB/Wonderful%20Life_%20The%20Burgess%20Shale...
1•rolph•24m ago•0 comments

Detecting Gunshots with a Watch Accelerometer

https://humanparadox.org/garmin-fenix-shot-timer-app/
2•colingauvin•30m ago•0 comments

Unfortunate day for companies named Context

https://www.context.dev/blog/we-are-context-dev-not-context-ai
2•ICodeSometimes•35m ago•1 comments

Mac Mini and Mac Studio Supply Shortages

https://www.wsj.com/tech/personal-tech/apple-mac-mini-supply-3e7a7509
2•Brajeshwar•35m ago•1 comments

Henry David Thoreau 2026 film

https://www.pbs.org/show/henry-david-thoreau/
1•rasengan0•37m ago•0 comments

Scalable Fluxonium Quantum Processors via Tunable-Coupler Architecture

https://arxiv.org/abs/2604.13363
2•jonbaer•38m ago•0 comments

Anthropic installs spyware when you install Claude Desktop

https://www.thatprivacyguy.com/blog/anthropic-spyware/
19•twapi•43m ago•3 comments

The Khan Ted Institute [video]

https://www.youtube.com/watch?v=kEhRi1tFlhs
1•apparent•44m ago•1 comments

Contra Benn Jordan, data center (and all) sub-audible infrasound issues are fake

https://blog.andymasley.com/p/contra-benn-jordan-data-center-and
2•logicprog•48m ago•1 comments

Where Did FIFA Seat Lottery Winners?

https://seat-transparency.com/
4•hnburnsy•51m ago•1 comments

Deception is strongly amplified in the job market (a statistical model)

https://bosoncutter.substack.com/p/deception-is-amplified-by-default
4•the_tyger•56m ago•1 comments

Why every developer needs their own agent-skills

https://olshansky.substack.com/p/why-every-developer-needs-their-own
1•Olshansky•57m ago•0 comments

Vercel Incident Response Playbook

https://github.com/OpenSourceMalware/vercel-april2026-incident-response
5•6mile•1h ago•4 comments

Third New Glenn launch suffers upper stage malfunction

https://spacenews.com/third-new-glenn-launch-suffers-upper-stage-malfunction/
2•ExpertAdvisor01•1h ago•2 comments

Google should allow third-party search engines access to data, EU says

https://www.reuters.com/world/google-should-allow-third-party-search-engines-access-data-eu-says-...
4•1vuio0pswjnm7•1h ago•1 comments

Show HN: TikTok-style mobile scroller for Civitai, self-hosted recs on a $5 VPS

https://testflight.apple.com/join/JkKUCXY6
1•alongbottom•1h ago•0 comments

Earthset

https://twitter.com/astro_reid/status/2046009031613907029
1•jbegley•1h ago•0 comments

Salesforce Announces AI Initiative and Calls It 'Headless 360'

https://gizmodo.com/salesforce-announces-huge-ai-initiative-and-calls-it-headless-360-2000748243
4•Brajeshwar•1h ago•0 comments

Show HN: A free, offline-capable math quiz app

https://quiz.neuralrad.com
1•coolwulf•1h ago•1 comments

AAVE price sinks 18% as KelpDAO exploit spreads

https://ambcrypto.com/aave-price-sinks-18-as-kelpdao-exploit-spreads-what-happened/
1•wslh•1h ago•0 comments

China's 7-tonne cargo drone completes maiden flight

https://news.cgtn.com/news/2026-03-31/China-s-new-7-tonne-unmanned-cargo-drone-set-for-maiden-fli...
5•pseudolus•1h ago•1 comments