frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Making of RP2040 Doom

https://kilograham.github.io/rp2040-doom/
1•tosh•1m ago•0 comments

OpenAI Claims DeepSeek Distilled US Models to Gain an Edge

https://www.bloomberg.com/news/articles/2026-02-12/openai-accuses-deepseek-of-distilling-us-model...
1•nsoonhui•3m ago•0 comments

Russian woman carried Ukraine team placard at Winter Olympics opening ceremony

https://www.theguardian.com/sport/2026/feb/17/russian-woman-carried-ukraine-team-placard-at-winte...
2•prmph•5m ago•0 comments

Ask HN: How are indie AI app builders monetizing in 2026 without killing UX?

1•axrisi•5m ago•1 comments

China's dancing robots: how worried should we be?

https://www.theguardian.com/world/2026/feb/18/china-dancing-humanoid-robots-festival-show
1•prmph•6m ago•0 comments

Ineffable Intelligence (Founder David Silver, ex Deepmind) Raises $1B Seed

https://www.ft.com/content/dffe72d0-4064-4412-8ebc-50198a30d40e
1•aanet•7m ago•0 comments

Show HN: Limpio – New Crypto Market Data Engine in Go (Redis and Timescale)

https://docs.limpioterminal.pro
1•arturstankevicz•8m ago•0 comments

arXivisual – Transform research papers into 3blue1brown style Manim animations

https://github.com/rajshah6/arXivisual
1•helloplanets•11m ago•0 comments

Show HN: SentryRF – A private, local-first Android app to detect hidden trackers

https://sentryrf.com/
1•vidoluc•12m ago•0 comments

Shigeru Ban on building cathedrals and quake shelters with paper

https://www.theguardian.com/artanddesign/2026/feb/17/shigeru-ban-architect-cathedrals-quake-shelt...
1•seanhunter•13m ago•0 comments

Apple developer account takes up to weeks to enroll

https://developer.apple.com/forums/thread/815713
2•throwaway423342•13m ago•1 comments

Air pollution may directly contribute to Alzheimers

https://theconversation.com/air-pollution-may-directly-contribute-to-alzheimers-disease-new-study...
1•lonelyasacloud•14m ago•1 comments

Index, Count, Offset, Size

https://tigerbeetle.com/blog/2026-02-16-index-count-offset-size/
1•ingve•15m ago•0 comments

After a fierce competition OpenAI hired Peter Steineberger

https://www.wsj.com/tech/ai/this-viral-ai-project-went-from-side-hustle-to-coveted-prize-in-three...
1•pretext•17m ago•0 comments

Chunyun – the largest annual human migration [video]

https://aeon.co/videos/chinese-new-year-is-a-stunning-spectacle-of-human-migration-in-3-billion-j...
1•hunglee2•23m ago•0 comments

Climber on trial for leaving girlfriend to die on Austria's highest mountain

https://www.bbc.com/news/articles/c5yv9plyjgpo
1•Stevvo•23m ago•0 comments

Show HN: SideDisplay – Turn Tesla screen into a wireless second monitor for Mac

https://sidedisplay.co/
1•maccraft•25m ago•0 comments

MineBench: LLM benchmark using voxel art

https://old.reddit.com/r/ClaudeAI/comments/1r7lphz/difference_between_sonnet_45_and_sonnet_46_on_a/
1•odie5533•29m ago•2 comments

Supply Chain Necromancy: Reborn Namespaces in JitPack Coordinates

https://labs.itresit.es/2026/02/18/supply-chain-necromancy-reborn-namespaces-in-jitpack-coordinates/
1•Yippee-Ki-Yay•30m ago•0 comments

Making music with MIDI just got a real boost in Windows 11

https://blogs.windows.com/windowsexperience/2026/02/17/making-music-with-midi-just-got-a-real-boo...
1•ingve•31m ago•0 comments

A single typo led to RCE in Firefox

https://kqx.io/post/firefox0day/
1•birdculture•32m ago•0 comments

Cryptographic Issues in Matrix's Rust Library Vodozemac

https://soatok.blog/2026/02/17/cryptographic-issues-in-matrixs-rust-library-vodozemac/
2•u1hcw9nx•34m ago•0 comments

Carl Sassenrath

https://en.wikipedia.org/wiki/Carl_Sassenrath
2•doener•40m ago•0 comments

Hazardous substances found in all headphones tested by ToxFREE project

https://www.theguardian.com/technology/2026/feb/18/hazardous-substances-headphones
3•doener•41m ago•1 comments

Your Agent Framework Is Just a Bad Clone of Elixir

https://georgeguimaraes.com/your-agent-orchestrator-is-just-a-bad-clone-of-elixir/
1•ahmaman•45m ago•0 comments

Parents are opting kids out of school laptops, returning them to pen and paper

https://www.nbcnews.com/tech/tech-news/parents-opt-kids-school-laptops-ask-pen-paper-rcna257158
1•apparent•46m ago•0 comments

RocksDB development finds a CPU bug

https://rocksdb.org/blog/2026/02/17/cpu-bug.html
1•botusaurus•46m ago•0 comments

Connectix RAM Doubler (2022)

https://computeradsfromthepast.substack.com/p/connectix-ram-doubler
2•andruby•50m ago•0 comments

Portugal: The First Global Empire

https://www.historytoday.com/archive/first-global-empire
1•Thevet•50m ago•0 comments

Microsoft .NET on Linux Patches Use IO_uring for Performance Benefits

https://www.phoronix.com/news/Microsoft-dotNET-IO-uring
1•dcgudeman•50m ago•0 comments