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.

I built an AI image generator that turns simple prompts into quality visuals

https://nanobananagen.org/
1•huixiaodewenzi•1m ago•1 comments

FluxVector – Free vector search API with built-in multilingual embeddings

https://fluxvector.dev
1•andresdp•1m ago•0 comments

Axios Compromised on NPM – Malicious Versions Drop Remote Access Trojan

https://www.stepsecurity.io/blog/axios-compromised-on-npm-malicious-versions-drop-remote-access-t...
2•mtud•1m ago•1 comments

Seems like a bad idea: "One login to connect Glassdoor and Indeed"

https://www.glassdoor.ca/about/onelogin/
1•SteveVeilStream•1m ago•1 comments

Show HN: Asto – AST-based code editing for AI agents

https://github.com/ntaraujo/asto
1•ntaraujo1•8m ago•0 comments

Show HN: HN Sieve – AI scores every HN project so you don't miss the good ones

https://github.com/primoia/hn-sieve
1•cezarvil•11m ago•0 comments

Earth's Fortunate Escape Velocity

https://www.universal-sci.com/headlines/2018/4/22/the-challenges-of-an-alien-spaceflight-program-...
1•sinoue•15m ago•0 comments

You still have to refactor, even with AI

https://www.adamhjk.com/blog/you-still-have-to-refactor-even-with-ai/
3•vinhnx•15m ago•0 comments

Super Investor

https://apps.apple.com/us/app/super-investor/id1441737952
1•jm33077•16m ago•0 comments

TokenSurf – Drop-in proxy that cuts LLM costs 40-94%

https://tokensurf.io
2•CemBas•16m ago•0 comments

Llama.cpp at 100k Stars

https://twitter.com/ggerganov/status/2038632534414680223
1•simonpure•17m ago•0 comments

NASA Computing in the '80's – JPL Building 230 [video]

https://www.youtube.com/watch?v=T_bqc76_3xU
1•jnord•21m ago•0 comments

American Exchange Group to buy sneaker maker Allbirds for $39M

https://www.reuters.com/business/american-exchange-group-buy-sneaker-maker-allbirds-39-million-20...
2•noleary•23m ago•0 comments

100x Less Power: The Breakthrough That Could Solve AI's Energy Crisis

https://scitechdaily.com/100x-less-power-the-breakthrough-that-could-solve-ais-massive-energy-cri...
1•g-b-r•23m ago•2 comments

Inkline: All-in-one workspace for authors and creative writers

https://github.com/enxilium/inkline
1•sukdip•24m ago•1 comments

Askable – give any UI element LLM awareness with one attribute

https://askable-ui.github.io/askable/
2•vamgan•26m ago•0 comments

Trump Tells Aides He's Willing to End War Without Reopening Hormuz

https://www.wsj.com/world/middle-east/trump-iran-war-strait-of-hormuz-ee950ad4
5•Jimmc414•27m ago•2 comments

Federal judges report broad adoption of AI tools

https://news.northwestern.edu/stories/2026/03/northwestern-study-finds-a-significant-number-of-fe...
2•pseudolus•28m ago•0 comments

We hate AI-assisted articles

https://idiallo.com/blog/why-we-hate-llm-articles
2•foxfired•29m ago•1 comments

Mr. Chatterbox is a Victorian-era ethically trained model

https://simonwillison.net/2026/Mar/30/mr-chatterbox/
2•y1n0•29m ago•0 comments

Effective Strategies for Asynchronous Software Engineering Agents

https://arxiv.org/abs/2603.21489
2•simonpure•31m ago•1 comments

Artemis II is not safe to fly

https://idlewords.com/2026/03/artemis_ii_is_not_safe_to_fly.htm
3•idlewords•32m ago•0 comments

How the Solar Wind Works

https://phys.org/news/2026-03-solar.html
2•y1n0•34m ago•0 comments

Put the Certificate Down

https://awakenedvoices.substack.com/p/put-the-certificate-down
1•sacredcam•38m ago•0 comments

See the Computers That Powered the Voyager Space Program

https://hackaday.com/2026/03/30/see-the-computers-that-powered-the-voyager-space-program/
1•y1n0•38m ago•0 comments

Pete Hegseth's broker looked to buy defence fund before Iran attack

https://www.ft.com/content/744ea8dc-6d93-4fe9-a5e3-36de4f5d06db
3•petethomas•39m ago•0 comments

Arbitrary Code Execution Discovered in Super Mario Bros 1 (1985)

https://tasvideos.org/10297S
2•sciolistse•40m ago•0 comments

Show HN: Agent Red Team – Adversarial testing for AI agents before production

https://agentredteam.ai
3•LukataSolutions•41m ago•1 comments

Release Engineering Lessons from Google and Facebook

https://morrigan-tech.com/blog/release-engineering-lessons/
1•gpi•45m ago•1 comments

Anthropic's Claude popularity with paying consumers is skyrocketing

https://techcrunch.com/2026/03/28/anthropics-claude-popularity-with-paying-consumers-is-skyrocket...
3•gmays•46m ago•1 comments