frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

No-Stack Web Development

https://dbushell.com/2026/04/10/no-stack-web-development/
2•jannesan•9m ago•0 comments

What Did You Name Your Bot?

https://kilo.ai/kiloclaw/names
1•twapi•10m ago•0 comments

Show HN: Reword, a Daily Anagram Game

https://amolkapoor.com/games/reword/
1•theahura•12m ago•0 comments

What Is ProtoFlow Workflow Automation Platform? [pdf]

https://storage.prod.researchhub.com/uploads/papers/users/187827/0720f181-328b-4333-ba79-f0593869...
1•KobyLawry•12m ago•0 comments

Do We Still Need Software Architects?

https://d3v.one/do-we-still-need-software-architects/
2•gpi•14m ago•0 comments

Show HN: GNU Grep as a PHP Extension

https://github.com/hparadiz/ext-gnu-grep
1•hparadiz•14m ago•0 comments

Transparency and knowledge exchange in AI-assisted data analysis code generation

https://www.nature.com/articles/s43588-025-00781-1
1•Anon84•16m ago•0 comments

2025 ACM Prize in Computing Goes to Apache Spark Creator Matei Zaharia

https://awards.acm.org/award-recipients/zaharia_8851855
1•tharakam•22m ago•0 comments

How zero-knowledge proofs make quantum circuits "private"

https://medium.com/@jkim_tran/verifying-private-quantum-circuits-9ec99d5f3d5a
1•jennifer-trin•29m ago•0 comments

Wan 2.7 – AI Video Generator for Text-to-Video and Image-to-Video

https://wan2-7.net
1•danielmateo773•31m ago•0 comments

The poetical works of Henry Wadsworth Longfellow (1889)

https://gutenberg.org/cache/epub/78406/pg78406-images.html
2•petethomas•31m ago•0 comments

789 KB Linux Without MMU on RISC-V (2023)

https://popovicu.com/posts/789-kb-linux-without-mmu-riscv/
2•pabs3•38m ago•0 comments

Metamath C

https://github.com/digama0/mm0/blob/master/mm0-rs/mmc.md
1•smj-edison•40m ago•0 comments

Chimpanzees' bloody 'civil war' may offer insight into human conflict

https://www.thetimes.com/uk/science/article/chimpanzee-group-splits-bloody-civil-war-59dk6swzr
2•petethomas•40m ago•0 comments

Crude: The Journey Of Oil (2007) [video]

https://www.youtube.com/watch?v=I7bmUYxwcaQ
1•measurablefunc•42m ago•0 comments

Borrow-Checking Surprises

https://www.scattered-thoughts.net/writing/borrow-checking-surprises/
1•jamii•47m ago•0 comments

Mayflower hero was sold into slavery, hidden files reveal

https://www.thetimes.com/world/europe/article/mayflower-hero-squanto-malaga-spain-sjx8sbvpv
1•petethomas•47m ago•0 comments

A Trip Through the Graphics Pipeline

https://alaingalvan.gitbook.io/a-trip-through-the-graphics-pipeline
1•sph•51m ago•0 comments

Bug in original PageRank paper (1998) [pdf]

https://www.cis.upenn.edu/~mkearns/teaching/NetworkedLife/pagerank.pdf
1•Abhavk•55m ago•1 comments

'Project Hail Mary' Directors Announce In-Theater Director Commentary Track

https://thatparkplace.com/project-hail-mary-commentary-track/
1•canucker2016•56m ago•1 comments

Moving from academic to industry career plans: A personal story

https://sites.lsa.umich.edu/math-graduates/2020/09/15/moving-from-academic-to-industry-career-pla...
1•ganitam•1h ago•0 comments

Why I quit "The Strive"

https://www.joanwestenberg.com/why-i-quit-the-strive/
1•zdw•1h ago•0 comments

Vulnpocalypse: AI, Open Source, and the Race to Remediate

https://www.resilientcyber.io/p/vulnpocalypse-ai-open-source-and
1•chillax•1h ago•0 comments

YouTube locked my accounts and I can't cancel my subscription

https://pocketables.com/2026/04/ai-music-corporate-control-and-the-creator-who-cant-even-leave.html
3•digitalhigh•1h ago•0 comments

PeerDrop – P2P file sharing across devices

https://github.com/jj10133/PeerDrop
2•jj_3•1h ago•1 comments

Eyes on the Solar System – Artemis

https://eyes.nasa.gov/apps/solar-system/
1•memalign•1h ago•0 comments

Negative effects of artificial sweeteners may pass on to next generation

https://www.eurekalert.org/news-releases/1121524
2•XzetaU8•1h ago•0 comments

Show HN: Shell-MCP A persistent terminal for AI- CD, env vars,and nvm carry over

2•prasanthsd•1h ago•0 comments

Explaining the Most Important Artemis II Photos [video]

https://www.youtube.com/watch?v=oaXRREHVkHo
1•Orochikaku•1h ago•0 comments

RoboPhD: Evolving complex agents under tight budgets

https://arxiv.org/abs/2604.04347
2•azhenley•1h ago•0 comments