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.

Ask HN: Do you waste AI assisted time looking for answers?

1•Haeuserschlucht•1m ago•0 comments

Ask HN: Is anyone working on Gov Digital IDs or have implementation docs / FOSS

1•lifeisstillgood•3m ago•0 comments

Show HN: Space 4 Links

https://space4links.com/
1•skyfantom•3m ago•0 comments

Wanted: A New Finance Writer

https://www.economist.com/finance-and-economics/2026/04/23/wanted-a-new-finance-writer
1•bookofjoe•4m ago•0 comments

SpaceX: Test Like You Fly [video]

https://www.spacex.com/content/starship/test-like-you-fly
1•w8vY7ER•6m ago•1 comments

Buddhist monk builds irreverent classifieds for lonely human mortals

https://chickenlist.com
1•ascottaggart•7m ago•0 comments

Show HN: Mux0 – Open-source macOS terminal with workspace tabs and agent hooks

https://mux0.com/
1•Justin3go•8m ago•0 comments

Andromeda – Making local AI accessible to non-technical users

https://store.steampowered.com/app/4056090/SmarterWaysProductions_Andromeda/
1•klueglscheisser•9m ago•1 comments

Niri 26.04 was just released (scrollable-tiling Wayland compositor)

https://github.com/niri-wm/niri/releases/tag/v26.04
1•nickjj•10m ago•0 comments

The physics slop that YouTube wants me to make [video]

https://www.youtube.com/watch?v=Cd5EHfRerGI
1•thorum•10m ago•0 comments

NATO eyes Saab GlobalEye to replace AWACS planes in historic shift from the U.S.

https://www.armyrecognition.com/news/aerospace-news/2026/nato-selects-swedish-saab-globaleye-to-r...
2•vrganj•11m ago•0 comments

The Beautiful Barbell Effect

https://camerasearch.substack.com/p/the-beautiful-barbell-effect
1•Aeroi•12m ago•0 comments

Show HN: I gave Claude and Cursor a seat on my Kanban board [video]

https://www.youtube.com/watch?v=CD2-NGtshrY
1•spotlayn•12m ago•0 comments

Graphite open source hybrid image editor

https://www.graphite.art/
2•tomcam•14m ago•0 comments

Writing a book is a labor of love

https://usefulfictions.substack.com/p/writing-a-book-is-a-labor-of-love
2•eatitraw•14m ago•0 comments

Why Silicon Valley Is Turning to the Catholic Church

https://www.theatlantic.com/ideas/2026/04/silicon-valley-catholicism-ai-leo/686948/
2•jonah•16m ago•0 comments

Show HN: Odozi – open-source iOS journaling app

https://odozi.app
2•jlarks32•16m ago•0 comments

What's Missing in the 'Agentic' Story

https://www.mnot.net/blog/2026/04/24/agents_as_collective_bargains
6•ingve•18m ago•0 comments

Baldmaxxing Confidence Mobile App – No BS

https://baldandwinning.com/en
2•thisissidhant•21m ago•1 comments

GLP-1 receptor agonist effects on Alzheimer's pathophysiology: Systematic review

https://www.sciencedirect.com/science/article/pii/S1044743126000217
2•bookofjoe•21m ago•0 comments

A validation-gated execution system (VYRDON)

https://github.com/teee79A/vyrdon
2•vyrdon•23m ago•0 comments

Your Job Isn't Programming

https://codeandcake.dev/posts/2025-12-12-your-job-isnt-programming
2•dgroshev•25m ago•0 comments

Trump alum helps Israel mount AI influence campaign

https://www.axios.com/2026/04/25/israel-ai-influence-parscale
2•sosomoxie•26m ago•0 comments

Mo RAM, Mo Problems

https://fabiensanglard.net/curse/
3•blfr•27m ago•0 comments

From Gaza With Love (1h49 documentary)

https://www.youtube.com/watch?v=xYj-XMIjHGo
2•bigbugbag•28m ago•1 comments

Fast Attention for Short Sequences

https://blog.qwertyforce.dev/posts/fast_attention_for_short_sequences
2•qwertyforce•33m ago•0 comments

The Glass_V1 standard for new computational storage

https://github.com/argoscollective/Glass-V1-Standard
2•Argoscollective•33m ago•0 comments

Over the Past Decade, Congestive Heart Failure Increased by over 10%

https://www.scai.org/media-center/news-and-articles/over-past-decade-congestive-heart-failure-and...
2•geox•33m ago•0 comments

Vibe Designing

https://jonathannen.com/vibe-designing/
2•speckx•36m ago•0 comments

Some data on the shape of the forgetting curve

https://www.natemeyvis.com/some-data-on-the-shape-of-the-forgetting-curve/
2•ingve•37m ago•0 comments