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.

Great Writers "Tell" All the Time

https://freddiedeboer.substack.com/p/great-writers-tell-all-the-time
1•paulpauper•2m ago•0 comments

Show HN: Ant – A from-scratch JavaScript runtime in 9 MB

https://github.com/themackabu/ant
1•theMackabu•8m ago•0 comments

SoC 2 has no real edge

1•krishgolcha•12m ago•0 comments

The Great American GLP-1 Experiment

https://www.nytimes.com/interactive/2026/04/15/opinion/glp1-health-effects.html
1•paulpauper•14m ago•0 comments

Show HN: KillClawd – a sarcastic AI desktop crab by local Ollama

https://github.com/ninjahawk/KillClawd
1•ninjahawk1•18m ago•0 comments

We mapped the nationwide Instructure breach

https://data.dailycal.org/2026-05-07-shiny-hunters/
1•notmysql_•21m ago•0 comments

The End of Elsewhere

https://fugitivemargins.substack.com/p/the-end-of-elsewhere
1•celadevra_•22m ago•0 comments

The Secret Diary That Has Spilled into the Musk vs. OpenAI Feud

https://www.wsj.com/tech/musk-openai-trial-greg-brockman-diary-journal-6950270e
1•RyanShook•23m ago•0 comments

Nvidia introduces back end for CUDA kernels in Rust

https://github.com/NVlabs/cuda-oxide
1•ketchup32613•47m ago•0 comments

Ask Hacker News: AI music with feedback

1•alpple•49m ago•0 comments

vLLM Routing and KV

https://avkcode.github.io/blog/how-vllm-works.html
2•gmays•57m ago•0 comments

A Mental Model for Agentic Work

https://basti.io/blog/agentic_work_mental_model/
1•vinhnx•59m ago•0 comments

Non-invasive profiling of the tumour microenvironment with spatial ecotypes

https://www.nature.com/articles/s41586-026-10452-4
2•bookofjoe•1h ago•0 comments

VGC: A Zone-Based Garbage Collection Architecture for Python's Parallel Runtime

https://arxiv.org/abs/2512.23768
2•teleforce•1h ago•0 comments

Tell HN: "Epstein files" is getting buried by deliberate suppresion-propaganda

15•notepad0x90•1h ago•4 comments

Open-source AWS evidence collector for SoC 2 audits

https://loxeai.com
1•arjavmehta•1h ago•0 comments

How to Work and Compound with AI

https://eugeneyan.com/writing/working-with-ai/
1•vinhnx•1h ago•0 comments

Open weights are quietly closing up – and that's a problem

https://martinalderson.com/posts/open-weights-are-quietly-closing-up/
2•vinhnx•1h ago•0 comments

He says U.S. troops abused him in Iraq's Abu Ghraib and his life is still ruined

https://www.npr.org/2023/04/11/1167341565/us-iraq-war-abu-ghraib-survivor
2•Cider9986•1h ago•0 comments

AI's Circular Psychosis

https://www.wheresyoured.at/premium-ais-circular-psychosis/
7•greedo•1h ago•4 comments

AI Hard Drive Shortage Making It More Expensive, Harder to Archive the Internet

https://www.404media.co/the-ai-hard-drive-shortage-is-making-it-more-expensive-and-harder-to-arch...
2•pabs3•1h ago•1 comments

Chamath Palihapitiya: What I Learned from Being Around the Top.01%

https://www.youtube.com/watch?v=RWqB9eAKMx0
2•Brysonbw•1h ago•0 comments

I built GitHub Store to 12,500 stars in 6 months – I started at 16

https://github-store.org/blog/how-i-built-github-store/
6•rainxchzed•1h ago•23 comments

I built a baremetal RustOS (O(1)allocator NVMeDMA ZeroTrust sandbox for AIAgents

https://github.com/joreag/polymorph_os
1•joreag•1h ago•0 comments

Software Sustainability Maturity Model (2013)

http://oss-watch.ac.uk/resources/ssmm
1•pabs3•1h ago•0 comments

Gemini went down with 1099 error

https://support.google.com/gemini/thread/431758595/1099-error?hl=en
1•nsoonhui•1h ago•0 comments

The Explorer Board a $99 Artix UltraScale+ FPGA Board from Adiuvo

https://explorerboard.tech/
3•signalhound•1h ago•0 comments

Remembering Planet Source Code: Sharing Code Before GitHub Made It Easy

https://www.pietschsoft.com/post/2026/05/05/remembering-planet-source-code-sharing-code-before-gi...
1•pabs3•1h ago•0 comments

Figure new humanoid robot demoing Bedroom Tidy

https://www.youtube.com/watch?v=8xEuFQz4E4A
1•zkjiang•1h ago•0 comments

Show HN: Nexa-gauge – Cache/cost-aware graph-based eval for LLM and RAG

https://github.com/harnexa/nexa-gauge
3•Sardhendu•1h ago•0 comments