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.

AI Augmentation vs. AI Amputation

https://indiantinker.bearblog.dev/ai-augmentation-ai-amputation/
1•indiantinker•20s ago•0 comments

It's time to accept the US Supreme Court is illegitimate and must be replaced

https://www.theguardian.com/commentisfree/2025/dec/19/us-supreme-court-legitimacy
2•KnuthIsGod•1m ago•0 comments

Why I'm not letting the juniors use GenAI for coding

https://lukeplant.me.uk/blog/posts/why-im-not-letting-the-juniors-use-genai-for-coding/
1•hecanjog•3m ago•0 comments

Can ChatGPT help with a midlife crisis?

https://www.ft.com/content/8b6e0a41-f3d1-474d-9d69-d5e0b897907b
1•fallinditch•4m ago•1 comments

A Better Way to Shuffle Your Apple Music Artists

https://www.smartshuffler.com
1•jackhanel•5m ago•1 comments

Single Board Module for Local LLM

https://shop.m5stack.com/products/m5stack-llm-large-language-model-module-kit-ax630c
1•giuseppedita•8m ago•0 comments

Show HN: Why Your Visitors Leave Without Buying

https://getrevdock.com/blog/why-your-visitors-leave-without-buying
1•imadjourney•10m ago•0 comments

Brothers are taking down Claude Code with OSS CLI

https://github.com/blackboxaicode/cli
1•mcflem007•11m ago•1 comments

Show HN: Role Call, discover more great TV from writers you enjoy

https://notactuallytreyanastasio.github.io/role_call/
1•rhgraysonii•12m ago•0 comments

My Cursed Setup for Public Bookmarks

https://sdf.org/~pkal/blog/tech/links.html
1•pkal•13m ago•0 comments

Evaluating Chain-of-Thought Monitorability

https://openai.com/index/evaluating-chain-of-thought-monitorability/
1•kjhughes•15m ago•0 comments

LLM Benchmark: Frontier models now statistically indistinguishable

2•js4ever•17m ago•0 comments

A Remarkable Coincidence in Wave Executive Trading

https://rxdatalab.com/research/wave-life-sciences-insiders/
1•nnmg•19m ago•0 comments

Go ahead, self-host Postgres

https://pierce.dev/notes/go-ahead-self-host-postgres#user-content-fn-1
24•pavel_lishin•23m ago•11 comments

How we made our SaaS homepage cookie-free

https://leavemealone.com/blog/no-more-cookies/
1•fanf2•24m ago•0 comments

Building with Claude Code

https://www.tik.dev/blog/building-with-claude-code
1•thakobyan•29m ago•0 comments

Laid Off After 25 Years in Tech

https://www.youtube.com/watch?v=VeMA9WGKxOg
3•farhanhubble•31m ago•0 comments

The era of GenAI.mil is here. Users have mixed reactions and many questions

https://defensescoop.com/2025/12/18/genai-mil-users-have-mixed-reactions-and-many-questions/
1•KnuthIsGod•32m ago•0 comments

Hardware-Attested Nix Builds

https://garnix.io/blog/attested-nix-builds/
1•birdculture•32m ago•0 comments

Pedagogy Recommendations

https://parentheticallyspeaking.org/articles/pedagogy-recommendations/
1•kaycebasques•32m ago•0 comments

Show HN: Savior – Automatic form draft recovery for real-world failures

https://github.com/Pepp38/Savior
1•Pepp38•32m ago•0 comments

DotMeow – A fun domain with a serious mission

https://www.dotmeow.org
7•OuterVale•32m ago•2 comments

PromptGuard – A way to guard your system prompts

https://karanja.xyz/blog/prompt-guard/
2•3093•35m ago•0 comments

Fundamentals of Browser Exploitation

https://browser.training.ret2.systems/welcome
2•zffr•37m ago•0 comments

The first climate refugees will arrive in Australia in 2026

https://www.economist.com/the-world-ahead/2025/11/12/the-worlds-first-climate-refugees-will-arriv...
1•andsoitis•41m ago•0 comments

Appark – Free app analytics tool

https://appark.ai
1•xuechen006•41m ago•0 comments

AIVO Standard Independence and Limitations Doctrine (v1.0)

https://zenodo.org/records/18001171
1•businessmate•41m ago•1 comments

Ntfy: Send push notifications to your phone or desktop using PUT/POST

https://github.com/binwiederhier/ntfy
2•thunderbong•42m ago•0 comments

How to Become Unhealthy in One Week

https://breatheless.substack.com/p/52-ways-to-become-unhealthy-in-one
2•not-so-darkstar•44m ago•0 comments

Trumps Biggest Failures 2025

https://asiaviewnews.com/gigabots/Threads?p=100049
4•mark336•47m ago•0 comments