frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Correlated randomness in Slay the Spire 2

https://tck.mn/blog/correlated-randomness-sts2/
1•rdmuser•4m ago•0 comments

Brick: SOTA LLM Routing

https://arxiv.org/abs/2606.13241
1•FrancescoMassa•6m ago•0 comments

A Chinese rocket breaks apart dangerously close to the Starlink constellation

https://arstechnica.com/space/2026/06/a-chinese-rocket-breaks-apart-dangerously-close-to-the-star...
1•rbanffy•6m ago•0 comments

Florida lawsuit accuses TikTok of violating state's child social media ban

https://www.theguardian.com/us-news/2026/jun/15/florida-sues-tiktok-teen-social-media-access-law
1•beardyw•6m ago•0 comments

Papers from the Lost Culture of Array Languages (2011)

https://prog21.dadgum.com/114.html
1•tosh•7m ago•0 comments

France's sovereignty push can't escape the Microsoft gravity well

https://www.theregister.com/on-prem/2026/06/16/frances-digital-sovereignty-push-is-struggling-to-...
2•Timofeibu•8m ago•1 comments

Notes from Tired Egyptian Whose Job Is Explaining That Humans Built the Pyramids

https://www.mcsweeneys.net/articles/notes-from-a-tired-egyptian-guy-whose-job-is-explaining-that-...
2•Geekette•14m ago•0 comments

Show HN: Hyperbox- $40/month Mac mini rentals

https://hyperbox.sh
1•Areibman•17m ago•0 comments

Show HN: ProData AI – 14 MCP tools for automated data science

https://mcpize.com/mcp/prodata-ai
2•varunwalekar•18m ago•0 comments

ML research datasets from ArXiv and Semantic Scholar (JSONL, quality-scored)

https://huggingface.co/fineset-io
1•dangerlego5•20m ago•1 comments

Trinket.io shutting down, so we saved it and hosted it a trinket.strivemath.org

https://trinket.strivemath.org/
3•apulkit6•20m ago•1 comments

My unvarnished guide to solution engineering

https://maxhalford.github.io/blog/solution-engineering-advice/
1•AdilZtn•21m ago•0 comments

Writing a browser-use agent from scratch – Part 1/3 – The Capture

https://pdufour.substack.com/p/writing-a-browser-use-agent-from
1•thunderbong•21m ago•0 comments

The fight over Claude Mythos 5

https://www.theverge.com/ai-artificial-intelligence/950412/anthropic-trump-adminstration-claude-m...
1•Shank•21m ago•0 comments

What the CEO Wants You to Know

https://commoncog.com/what-the-ceo-wants-you-to-know/
1•simonebrunozzi•22m ago•0 comments

How We Built a High-Performance Factor Evaluation Framework

https://medium.com/@DolphinDB_Inc/high-performance-factor-evaluation-with-dolphindb-23bb6954503f
2•Polly_Liu•25m ago•0 comments

Feds freaked over Fable 5 after simple 'fix this code' prompt, not jailbreak

https://www.theregister.com/security/2026/06/15/feds-freaked-over-fable-5-after-simple-fix-this-c...
3•_tk_•25m ago•0 comments

Mike Acton: Convex Primitive Collision Detection – Reference and LLM-Optimized

https://github.com/macton/differentiable-collisions-optc
2•thdr•25m ago•0 comments

Dutch children are unusually happy and healthy. Because of this walking ritual?

https://www.theguardian.com/lifeandstyle/2026/jun/16/dutch-children-unusually-happy-healthy-avond...
2•monkey_monkey•27m ago•0 comments

Tensordyne Napier AI Processor Announced with Logarithmic Math

https://www.servethehome.com/tensordyne-napier-ai-processor-announced-with-logarithmic-math/
1•lumpa•32m ago•0 comments

South Korea Politicians Wants AI Profit Sharing

https://www.gadgetreview.com/south-korea-floats-citizen-dividend-ai-profit-sharing-as-tech-stocks...
2•thomasfl•34m ago•1 comments

GPU Is the New Oil

https://xificurc.github.io/blog/gpu-is-the-new-oil/
1•comma_at•35m ago•0 comments

We Built a Real-Time Money Flow Monitoring System

https://medium.com/@DolphinDB_Inc/tracking-smart-money-in-real-time-building-a-minute-level-capit...
2•CrazyTomato•35m ago•0 comments

Commodore Releases Flip Phone

https://commodore.net/why-a-flip-phone/
6•bartekrutkowski•35m ago•0 comments

The Fable 5 Export Controls Harm US Cyber Defense

https://simonwillison.net/2026/Jun/16/fable-5-export-controls/
1•lumpa•36m ago•0 comments

Nubase – an open-source back end/deploy layer for AI-written apps

https://github.com/OtterMind/Nubase
2•jipengfei1016•39m ago•0 comments

Commodore Callback

https://commodore.net/callback/
8•peterkelly•42m ago•0 comments

Nockchain: A protocol for verifiable Compute Networks

https://nockchain.org/
2•MrBuddyCasino•43m ago•0 comments

Show HN: Opencom – an open-source Intercom alternative given the Salesforce deal

https://opencom.dev
4•jackjayd•45m ago•0 comments

Show HN: Cultivation World Simulator

https://github.com/4thfever/cultivation-world-simulator
2•bridge2333•45m ago•0 comments