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.

Max Dama on Automated Trading (2011) [pdf]

http://isomorphisms.sdf.org/maxdama.pdf
1•TheAlchemist•46s ago•0 comments

Van Halen Test

https://en.wikipedia.org/wiki/Van_Halen_test
1•evo_9•3m ago•0 comments

Getting 50 GB/S Back from the Apple Neural Engine

https://eiln.github.io/posts/ane-dma.html
1•eiln•5m ago•1 comments

Tor VPN Beta: What we've learned building our own VPN for Android from scratch

https://blog.torproject.org/tor-vpn-beta/
1•woofcat•5m ago•0 comments

Where Stability Becomes the Constraint

https://medium.com/@gurvinder372/article-14-where-stability-becomes-the-constraint-c262b07f617c
1•gps372•8m ago•0 comments

Coinbase Design Systems Are Powering the AI Prototyping Era

https://www.coinbase.com/blog/how-coinbase-design-systems-are-powering-the-ai-prototyping-era
2•dukebartnik•8m ago•0 comments

Weighing matrix W(35,25) found

https://github.com/Jepfore-Games-Ltd/W-35-25
1•jepforegames•11m ago•0 comments

More Impossible-to-Ignore Vaccine Effects

https://www.science.org/content/blog-post/more-impossible-ignore-vaccine-effects
2•EA-3167•14m ago•0 comments

USN-8717-1: Apache Tika vulnerability – Ubuntu security notices

https://ubuntu.com/security/notices/USN-8717-1
2•kyisaiah47•17m ago•0 comments

Muse, the band, lost its social media handles to Muse, Meta's new AI agent

https://www.engadget.com/2254419/muse-the-band-lost-its-social-media-handles-to-muse-meta-s-new-a...
7•_djo_•24m ago•1 comments

Show HN: Herdr Agents Fleet on your phone with TermRover

https://termrover.sh/blog/herdr-agent-fleet-mobile/
1•ptgamr•24m ago•0 comments

Show HN: Botbin – Pastebin for AI agent artifacts

https://botbin.io/
1•obilgic•24m ago•0 comments

Show HN: Text-to-SQL picks tables before RLS runs – demo in the browser

https://ashishsinha1602.github.io/schemagate/
1•asinha0216•28m ago•0 comments

Automattic's board forces CEO Matt Mullenweg into leave of absence

https://techcrunch.com/2026/09/09/automattics-board-forces-ceo-matt-mullenweg-into-leave-of-absence/
6•LeoPanthera•32m ago•0 comments

AI May Become the Third Superpower

https://www.wsj.com/opinion/ai-may-become-the-third-superpower-1f7700dc
1•gmays•32m ago•0 comments

ORC8R, one-click compute for developers, no ops required

https://orc8r.com/docs/quickstart
2•ORC8R•36m ago•2 comments

Ask HN: What's your AI coding set up?

4•asxndu•36m ago•1 comments

Hybrid approach to creating low poly game-ready 3D assets using AI models

https://playwarpact.com/blog/lowpoly-hybrid/
3•kodefreeze•37m ago•1 comments

Show HN: Ambient Context v1.0.0 (Missing context for AI assistants)

https://github.com/dragthelake/ambient-context/releases
2•Dramatize•41m ago•0 comments

I Let an AI Agent Hack All My Gadgets–and I'd Do It Again

https://www.wired.com/story/i-used-ai-to-hack-my-home-network/
3•thomadev0•41m ago•0 comments

Things that testers wished they'd learned earlier

https://www.functionize.com/blog/16-things-that-testers-wished-theyd-learned-earlier
2•ohjeez•44m ago•0 comments

Show HN: EmbedFlow –> Upgrade embedding models without re-embedding your corpus

https://github.com/arnsri33/embedflow
5•coolArnav•47m ago•1 comments

Astrophysical Journal adopts new guidelines on AI use

https://journals.aas.org/news/new-guidelines-on-ai-use-in-aas-journals/
1•tomr_stargazer•49m ago•1 comments

World's First: Minor Authors Public License for Expression (Maple-1.0)

https://openrockets.com/research/maple/v1/
2•interkids•54m ago•0 comments

Prosecutors allege man used AI to scrape court website

https://www.abc.net.au/news/2026-09-10/christopher-duff-to-stand-trial-over-nsw-ai-court-data-bre...
2•yeetosaurusrex•1h ago•1 comments

Artificial Intelligence Markup Language

https://en.wikipedia.org/wiki/Artificial_Intelligence_Markup_Language
3•marvinborner•1h ago•0 comments

A Design Space Exploration of Async/Await

https://cel.cs.brown.edu/blog/design-space-async-await/
2•matt_d•1h ago•0 comments

SubmitSafe

https://submitsafe.app/
2•igrik•1h ago•0 comments

Show HN: Model meets Reality: Model and test your understanding

https://modelmeetsreality.xyz/
3•sauronsrv•1h ago•0 comments

Automatic Key Exchange: faster, post-quantum secure origin handshakes

https://blog.cloudflare.com/automatic-key-exchange-for-origins/
1•ackackack•1h ago•0 comments