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.

Why Do Galaxies Refuse to Obey Gravity? Universe Is Accelerating and Science

https://medium.com/@pajuhaan/why-do-galaxies-refuse-to-obey-gravity-fb7cfa5ac8b1
1•pajuhaan•3m ago•1 comments

Ask HN: Constantly updated publicly accessible HN dataset?

1•freakynit•7m ago•0 comments

How to Ralph Wiggum

https://github.com/ghuntley/how-to-ralph-wiggum
1•ghuntley•7m ago•0 comments

Quality Week – December 2025 – Zed's Blog

https://zed.dev/blog/quality-week-december-2025
1•provdr•8m ago•0 comments

Template Parameter Deduction: Eliminating Hidden Copies in Generic Code

https://0xghost.dev/blog/template-parameter-deduction/
1•tempodox•12m ago•0 comments

Hongdown: An opinionated Markdown formatter in Rust

https://github.com/dahlia/hongdown
1•todsacerdoti•12m ago•0 comments

Org Mode Syntax Is One of the Most Reasonable Markup Languages to Use for Text

https://karl-voit.at/2017/09/23/orgmode-as-markup-only/
2•adityaathalye•14m ago•0 comments

Week 5 in Data Science: Image recognition neural network with 90% accuracy

https://igorstechnoclub.com/deep-learning-transfer-learning/
1•Igor_Wiwi•18m ago•0 comments

Show HN: A daily cryptic clue puzzle with a twist

https://www.riddlegrid.com/
1•jpallen•19m ago•0 comments

Opus 4.5 is now free in Amp, up to $10 in credits per day, ad-supported

https://twitter.com/beyang/status/2009511604480606219
1•tosh•21m ago•0 comments

Gleam's Design and Compiler

https://www.youtube.com/watch?v=WC_j91qogyY
1•todsacerdoti•21m ago•0 comments

Docker Socket Myths

https://amf3.github.io/articles/virtualization/docker_socket/
1•thunderbong•21m ago•0 comments

Doctorow: Trump may be the beginning of end of 'enshittification'

https://www.theguardian.com/commentisfree/2026/jan/10/trump-beginning-of-end-enshittification-mak...
4•defrost•26m ago•0 comments

You guys are doing this to us

2•zerosizedweasle•28m ago•0 comments

Bolt Graphics

https://bolt.graphics/
1•TechTechTech•30m ago•0 comments

Homeworld – The Digital Antiquarian

https://www.filfre.net/2026/01/homeworld/
1•rcarmo•31m ago•0 comments

iFixit names worst products of CES 2026 by handing out "Worst in Show" awards

https://www.notebookcheck.net/iFixit-names-worst-products-of-CES-2026-by-handing-out-Worst-in-Sho...
2•yread•33m ago•0 comments

Self-Evolving Cellular Automata

https://kaistale.com/blog/2602_gol
2•kabla•36m ago•0 comments

Google: Don't make "bite-sized" content for LLMs if you care about search rank

https://arstechnica.com/google/2026/01/google-dont-make-bite-sized-content-for-llms-if-you-care-a...
2•Tomte•37m ago•0 comments

Signs of Accepted Prayers

https://mycharisma.com/culture/clear-signs-god-is-saying-yes-to-your-prayers/
1•marysminefnuf•40m ago•0 comments

Laws of Software

https://www.laws-of-software.com/
1•dtj1123•49m ago•0 comments

Debian goes retro with a spatial desktop that time forgot

https://www.theregister.com/2026/01/09/desktop_classic_system/
2•rcarmo•52m ago•0 comments

Annote – writing Java using only annotations

https://github.com/kusoroadeolu/annote
2•matthews3•54m ago•1 comments

Lord of War, Meet Lord of Tokens

https://singhkays.com/blog/lord-war-test-image-models/
1•Twixes•59m ago•0 comments

How to Get Early Users

https://blog.thefoundermanual.com/p/how-to-get-early-users
2•adeeb•59m ago•0 comments

Resistance Honeypots

https://heatherburns.tech/2026/01/10/resistance-honeypots/
2•treadump•59m ago•1 comments

Vibe-Claude: Self-evolving multi-agent system for Claude Code

https://github.com/kks0488/vibe-claude
1•kyoungsookim•1h ago•1 comments

Outkast – Monitor Global Outages

https://github.com/groundcover-com/outkast
1•puppion•1h ago•0 comments

Chatbots put through psychotherapy report trauma and abuse

https://www.nature.com/articles/d41586-025-04112-2
2•javajive•1h ago•2 comments

Show HN: Scriberoo – fast, TOP-quality transcriptions for any audio / video

https://scriberoo.com
1•elcapizz•1h ago•0 comments