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.

Hand-Picked Free Typefaces

https://facetypes.xyz/
2•coinfused•3m ago•0 comments

I run multiple Claude Code accounts on one Mac

https://twitter.com/viborc/status/2121941174374039788
1•taubek•4m ago•0 comments

What the Water Keeps – Reverse-engineered Three.js source of the Fable 5.2 game

https://github.com/dill-lk/What-the-Water-Keeps
1•ftcjnk•6m ago•0 comments

Amiga Studio: IDE for making games and apps for Amiga and C64 in assembler or C

https://piershaw.itch.io/amiga-studio-ide
3•doener•6m ago•1 comments

Rust and Git worktrees: share one Cargo build

https://docspring.com/blog/posts/rust-git-worktrees-shared-cargo-build/
1•torutofu•8m ago•0 comments

RISC-V Matrix Extensions

https://fprox.substack.com/p/risc-v-matrix-extensions
1•camel-cdr•9m ago•0 comments

In a Sloppified A.I. World, Only Humans Can Build 'Ecosystems'

https://www.nytimes.com/2026/11/15/business/tech-jobs-ai-ecosystem.html
1•saikatsg•10m ago•1 comments

Show HN: db-branch – database branches in seconds for AI agents and CI, via ZFS

https://github.com/db-branch/db-branch
1•nu_nu11•13m ago•0 comments

Beware the New AI-Fueled Ransomware Boom

https://www.wired.com/story/beware-the-new-ai-fueled-ransomware-boom/
2•saikatsg•14m ago•0 comments

Go 1.27's Generic Methods: Compile-Checked ORM Columns Without Generated Code

https://skudra.net/blog/field-pointers-as-column-names
1•birdculture•14m ago•0 comments

Code Red: Y2K Is Here

https://deadneurons.substack.com/p/code-red-y2k-is-here
1•nr378•14m ago•0 comments

New Species of Giant Fungus Discovered in China Grows Up to 20 Inches Tall

https://www.wired.com/story/new-species-of-giant-fungus-discovered-in-china-grows-up-to-20-inches...
1•saikatsg•15m ago•0 comments

Let your AI agent call you, over MCP

https://agline.in/mcp
1•franze•16m ago•0 comments

Things That Turbo Pascal Is Smaller Than (2011)

https://prog21.dadgum.com/116.html
2•ksec•20m ago•0 comments

HCSV (Hybrid CSV) – CSV with inline column types and bit-exact floats

https://cristian8vasile.substack.com/p/what-shipping-containers-can-teach
1•cristianvasile•20m ago•0 comments

Cursor: Pointer on Buttons

https://blog.kizu.dev/cursors/
1•shipitx•23m ago•1 comments

Hairpin – Reach macOS menu bar icons hidden by the notch

https://github.com/HiAriesZhou/Hairpin
2•AriesIMio•24m ago•0 comments

The Not-So-Crazy Legal Strategy to Free Nicolás Maduro

https://www.politico.com/news/magazine/2026/11/15/nicolas-maduro-trial-legal-strategy-01149709
1•ibn_khaldun•24m ago•0 comments

Booker Prize 2026: The End of Everything – First SF Winner, Oldest Laureate

https://www.betterreads.dev/blog/booker-prize-2026-end-of-everything
2•zlu•25m ago•0 comments

US investigators probe cause of tanker-cruise ship collision

https://www.abc.net.au/news/2026-11-15/tanker-cruise-ship-collison-investigation-new-orleans/1073...
1•firasd•30m ago•0 comments

Show HN: Every Satellite in Orbit Since 1957

https://berryde.com/projects/satellites/
1•daedbe•32m ago•0 comments

James Webb Telescope finds ice older than the star it orbits

https://arstechnica.com/science/2026/11/james-webb-telescope-finds-ice-older-than-the-star-it-orb...
2•Brajeshwar•33m ago•0 comments

Scientists Made Monkeys Watch Minions to Study Their Brains

https://www.404media.co/scientists-made-monkeys-watch-minions-to-study-their-brains/
2•Brajeshwar•33m ago•0 comments

The original Xbox was released 25 years ago today

https://www.tomshardware.com/video-games/xbox/the-original-xbox-was-released-25-years-ago-today-m...
3•Brajeshwar•33m ago•0 comments

Europa, the EU's open AI model, is aiming for nearly 1T parameters

https://maccus.fr/en/eu-open-ai-model-europa/
2•mstkllah•34m ago•0 comments

Singapore Government Clarification Statement on Wall Street Journal's Article

https://www.mha.gov.sg/media-room/newsroom/clarification-statement-on-wall-street-journals-article/
2•ksec•35m ago•2 comments

NeatStats – Simple, lightweight, privacy-first web stats

https://neatstats.net/
1•Tomte•37m ago•0 comments

Cramer: Insult to NV staff thinking NV is nothing without the leather-jacket guy

https://twitter.com/OutFrontCNN/status/2121064585033843071
1•ksec•38m ago•0 comments

Abyssal Garden: A dreamy underwater world you can swim through (96 KB HTML)

https://github.com/Noniv/abyssal-garden
2•soheilpro•39m ago•0 comments

Show HN: K8 Cruft – a Windows dev-junk cleaner with a 30-day undo

https://lilbuba.ai/k8-cruft.html
1•infinity_jo•42m ago•0 comments