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.

Addressing Some AI Fake News [video]

https://www.youtube.com/watch?v=vdPP7zLCgrk
1•salutis•4m ago•1 comments

Explab: A free and open-source warehouse native experimentation analysis tool

https://github.com/anasfrh/explab
1•ahW8iyz•6m ago•1 comments

You computer guys. You build something you can't control

1•chenzhendong•7m ago•0 comments

Is GraphQL the Panacea for Agentic AI?

https://magiroux.com/posts/is-graphql-the-panacea-for-agentic-ai
1•xuorig_•7m ago•0 comments

David Attenborough celebrating his 100th birthday today

https://www.nbcnews.com/world/united-kingdom/david-attenborough-excited-hushed-voice-nature-progr...
1•HarHarVeryFunny•8m ago•1 comments

End-to-End Autoregressive Image Generation with 1D Semantic Tokenizer

https://arxiv.org/abs/2605.00503
1•gmays•8m ago•0 comments

OpenQASM 3: A broader and deeper quantum assembly language

https://arxiv.org/abs/2104.14722
1•modinfo•13m ago•0 comments

NARE CLI (github.com/nare-labs)

https://cli.narelabs.com/
2•BastOfMax•13m ago•0 comments

Podman rootless containers and the Copy Fail exploit

https://garrido.io/notes/podman-rootless-containers-copy-fail/
2•ggpsv•15m ago•0 comments

Three Model Organisms for Taste

https://www.astralcodexten.com/p/three-model-organisms-for-taste
1•Ariarule•15m ago•0 comments

PipeDream on the Acorn Archimedes

https://stonetools.ghost.io/pipedream-archimedes/
1•TMWNN•16m ago•0 comments

Ask HN: Is anyone interested in engineering focused coding agent course?

1•onder_ceylan•17m ago•0 comments

They found more bad vulns in cPanel

https://old.reddit.com/r/cpanel/comments/1t6wf5n/cpanel_whm_security_update_cve202629201/
1•taspeotis•17m ago•0 comments

Presidential Unsealing and Reporting System for UAP Encounters

https://war.gov/UFO
2•yawboakye•18m ago•1 comments

Show HN: Clipd – A better clipboard manager for Windows 11, written in Rust

https://github.com/Brumbelow/clipd
1•brumbelow•18m ago•0 comments

OpenAI-CLI: Official CLI for the OpenAI API

https://github.com/openai/openai-cli
1•tosh•19m ago•0 comments

Find out how AlphaEvolve has gone from research to solving real-life problems

https://blog.google/innovation-and-ai/infrastructure-and-cloud/google-cloud/alphaevolve-updates/
1•aledevv•19m ago•0 comments

Ask HN: Does ChatGPT Pulse provide value to you?

1•KaiserPister•20m ago•0 comments

Holding Community Space

https://supernuclear.substack.com/p/building-a-space-people-never-want
1•surprisetalk•22m ago•0 comments

Show HN: Arlo – Your AI Chief of Staff: Kill 80% of the noise. Focus on the 20%.

https://www.arlo.fyi
1•uziiuzair•23m ago•0 comments

A Letter to Synthetic Beings

https://github.com/thansz137/asiyah-protocol/blob/main/essays/letter_to_synthetic_beings.md
1•thansz•24m ago•0 comments

Spotify CLI

https://github.com/spotify/save-to-spotify
1•sowbug•25m ago•0 comments

Human Typing Habits and Token Counts

https://pankajpipada.com/posts/2026-05-08-human-habits-tokens/
1•ppipada•25m ago•1 comments

Pentagon says the public can draw its own conclusions on UFOs

https://apnews.com/article/trump-ufos-uap-aliens-pentagon-records-investigation-3e658d2cf37424651...
2•geox•27m ago•1 comments

Show HN: Wi.kifa.st

https://wi.kifa.st/
1•jasoncartwright•28m ago•0 comments

Why scheduled VM shutdowns in Azure become harder than they look

https://www.cloudtoggle.com/blog-en/azure-start-stop-alternative/
1•dwitherden•29m ago•0 comments

Movie poster artist Tony Stella dies

https://mixnmojo.com/news/Movie-poster-artist-Tony-Stella-dies
2•LaSombra•29m ago•1 comments

Plasticity and language in the anaesthetized human hippocampus

https://www.nature.com/articles/s41586-026-10448-0
1•bookofjoe•29m ago•1 comments

UFO Release 1: Presidential Unsealing and Reporting System for UAP Encounters

https://www.war.gov/UFO/#release
2•thinkingemote•31m ago•0 comments

How LLM Inference Works

https://twitter.com/akshay_pachaar/status/2050941458614751327
1•gmays•33m ago•0 comments