frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Switching my website from Hugo to Quarto

https://nrennie.rbind.io/blog/hugo-quarto-website/
1•m-hodges•8s ago•0 comments

Bash script to vendor in NPM packages to your repo

https://gist.github.com/danthegoodman1/021678f0aac498d7d644a70609109655
1•dangoodmanUT•2m ago•1 comments

Open Neuromorphic

https://open-neuromorphic.org
1•bcye•2m ago•0 comments

UK campaigner targeted by Trump accuses tech giants of 'sociopathic greed'

https://www.theguardian.com/us-news/2025/dec/26/uk-campaigner-targeted-by-trump-accuses-tech-gian...
2•pera•4m ago•0 comments

From Intent to Proof: Dafny Verification for Web Apps

https://midspiral.com/blog/from-intent-to-proof-dafny-verification-for-web-apps/
1•namin•5m ago•0 comments

Bypass Windows user interface privilege isolation via the CTF input method proto

https://projectzero.google/2019/08/down-rabbit-hole.html
1•fanf2•9m ago•0 comments

Find Your Celebrity Twin with AI

https://celeblookalike.org/
1•ivanvolt•12m ago•0 comments

I shrunk down into an M5 chip [video]

https://www.youtube.com/watch?v=Jh9pFp1oM7E
1•Timothee•12m ago•0 comments

Show HN: Hybrid-Transpiler – A tool to convert C++ to Rust and Go

https://github.com/cmc-labo/hybrid-transpiler
1•hpscript•13m ago•1 comments

Archivara Math Research Agent became 1st AI to solve an Erdős problem on its own

https://twitter.com/spicey_lemonade/status/2004558572496035892
1•amichail•13m ago•0 comments

SourceGit: Open-Source Git UI for Windows/macOS/Linux

https://github.com/sourcegit-scm/sourcegit
1•sea-gold•13m ago•0 comments

The AI bubble is all over now, baby blue

https://garymarcus.substack.com/p/the-ai-bubble-is-all-over-now-baby
3•ArmageddonIt•15m ago•0 comments

Play the Monty Hall Game

https://monty.donk.systems
1•todsacerdoti•16m ago•0 comments

Guide to Machine Learning

https://www.ibm.com/think/machine-learning
2•b-man•16m ago•0 comments

VisualDiffer: Compare Folders and Files on macOS

https://visualdiffer.com
1•sea-gold•16m ago•1 comments

Low-frequency noise can help electric vehicles stand out in busy environments

https://techxplore.com/news/2025-12-employing-frequency-noise-electric-vehicles.html
1•PaulHoule•17m ago•0 comments

DevSecOps Tools Periodic Table

https://digital.ai/learn/devsecops-periodic-table/
1•gudzpoz•21m ago•0 comments

Rock Paper Scissors Strategies

https://wrpsa.com/rock-paper-scissors-strategies/
1•cainxinth•21m ago•0 comments

AI Generated Tests Might Be Lying to You

https://www.youtube.com/watch?v=-iGptIr7FZA
2•nslog•22m ago•1 comments

My coworker's 36 key Corne open-source keyboard setup

https://nuon.co/blog/nuon-keyboard-culture/
1•realsharkymark•22m ago•1 comments

Automatic label checking: The missing step in making reliable medical AI

https://www.omu.ac.jp/en/info/research-news/entry-100013.html
1•geox•23m ago•0 comments

AI Usage Policy – Tao of Mac

https://taoofmac.com/space/site/ai_policy
1•rcarmo•24m ago•0 comments

More Than 80% of New California Properties Are in High Fire-Risk Areas

https://www.theguardian.com/news/datablog/2025/feb/15/california-wildfire-building-risk
2•karakoram•24m ago•0 comments

Predictions for 2026

https://taoofmac.com/space/blog/2025/12/26/1500
1•rcarmo•24m ago•0 comments

My year in tech switches- 2025 edition

1•KitN•25m ago•1 comments

The Ethical Computing Initiative, Xmas Edition

https://aol.codeberg.page/eci/
1•babylon5•26m ago•0 comments

The gut bacteria that put the brakes on weight gain in mice

https://phys.org/news/2025-12-gut-bacteria-weight-gain-mice.html
2•WaitWaitWha•27m ago•0 comments

Show HN: Witr – Explain why a process is running on your Linux system

https://github.com/pranshuparmar/witr
1•pranshuparmar•30m ago•1 comments

Optics Primer, Part 1: Traditional Pluggable Optics

https://www.chipstrat.com/p/optics-primer-part-1-traditional
1•rbanffy•32m ago•0 comments

Show HN: Zero-config staging environments for GitHub repos

https://autodock.io
1•mikesol•34m ago•0 comments