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.

Cewsco is a premium all-in-one AI

https://cewsco.com/
2•kalkalnewyork•3m ago•1 comments

Show HN: Agentic Orchestrator, a TUI for long-running coding agents

https://github.com/doordash-oss/agentic-orchestrator
1•ivrr•8m ago•0 comments

Comcast plans to spin off NBCUniversal in strategy reversal

https://www.msn.com/en-us/money/companies/comcast-plans-to-spin-off-nbcuniversal-in-strategy-reve...
1•chirau•11m ago•0 comments

The Official Rules for Calling Shotgun

https://www.shotgunrules.com/
1•Cider9986•14m ago•0 comments

Daytona sandbox is closed-source

https://github.com/daytonaio/daytona/
1•honghe•14m ago•1 comments

Kalshi temporarily blocked from sports betting in Michigan [pdf]

https://www.michigan.gov/ag/-/media/Project/Websites/AG/releases/2026/June/KalshiEX-Order-Grantin...
2•michaefe•16m ago•0 comments

Show HN: Let an AI manage your homelab over MCP without giving it SSH

https://homebutler.dev/
1•swq115•16m ago•0 comments

Diving Suit for Cyborg Cockroaches

https://smbtech.au/news/at-last-top-asian-universities-make-diving-suit-for-cyborg-cockroaches/
1•rawgabbit•17m ago•0 comments

I built a free I Ching oracle web app

https://iching-silk.vercel.app/
1•henrychannel•19m ago•1 comments

JetBlue flight hit drone while approaching JFK airport, FAA says

https://www.reuters.com/world/us/jetblue-flight-hit-drone-while-approaching-jfk-airport-faa-says-...
1•jofzar•22m ago•1 comments

Show HN: Lighting fast writing app on Mac with powerful table functions

https://org-notes-mac.netlify.app
1•unparadise•25m ago•1 comments

The OWASP Agentic Security Initiative Top: A Practical Developer Guide

https://agentsafelabs.com/blog/the-owasp-agentic-security-initiative-top-10-a-practical-developer...
2•waqarjaved•25m ago•0 comments

Exploring PDP-1 Lisp (1960)

https://obsolescence.dev/pdp1-lisp-introduction.html
7•ozymandiax•26m ago•5 comments

Letos: Create, edit, browse SQLite databases. Formerly known as SQLiteStudio

https://letos.org/
3•thunderbong•26m ago•0 comments

GameBoy Emulator on E-Ink [video]

https://www.youtube.com/watch?v=oPbOK90aJEo
2•toomim•27m ago•0 comments

Tau – Learn how coding agents are built

https://twotimespi.dev
2•lemming•32m ago•0 comments

Show HN: Embeddable Cumulative Crates Download History

https://cratesdownloadhistory.undivisible.dev/
2•undivisible•32m ago•0 comments

Show HN: PDF Insight – local-first AI that sorts your PDFs on-device

https://pdf-insight.com/
2•jacoblav•34m ago•0 comments

Getting the B.l.o.a.t. (Best liver of all time)

https://sull1van.com/best-liver-of-all-time
2•s3p•38m ago•0 comments

Foray – AI that generates a complete business blueprint and stress-tests ur idea

https://www.getforay.ai/
2•foray-ai•39m ago•0 comments

Memory Safe Context Switching (longjmp, setjmp) in Fil-C

https://fil-c.org/context_switches
10•modeless•44m ago•0 comments

'News' Site Keeps Hallucinating EFF Staffers

https://www.eff.org/deeplinks/2026/06/news-site-keeps-hallucinating-eff-staffers
3•speckx•44m ago•1 comments

Large language models and the textual nude

https://plus.flux.community/p/large-language-models-and-the-textual
3•milosstich•47m ago•0 comments

TPG boss to enter guilty plea and seek leniency

https://www.afr.com/companies/financial-services/tpg-boss-to-enter-guilty-plea-and-seek-leniency-...
2•Alien1Being•47m ago•0 comments

Chinese Hedge Funds Warn the AI 'Super Bubble' Is Ready to Burst

https://www.bloomberg.com/news/articles/2026-06-26/chinese-hedge-funds-warn-the-ai-super-bubble-i...
4•cdrnsf•48m ago•2 comments

Tidal Says It Won't Pay Royalties for AI-Generated Music

https://www.404media.co/tidal-says-it-wont-pay-royalties-for-ai-generated-music/
3•cdrnsf•49m ago•4 comments

Show HN: AMA2, messenger built for AI agent

https://ama2.me/
2•ejhooooon•50m ago•0 comments

Devin Fusion: Frontier Performance at 35% Lower Cost

https://cognition.com/blog/devin-fusion#examples-of-sidekick-in-action
2•qainsights•52m ago•0 comments

LongCat-2.0, a large-scale MoE model with 1.6T total and 48B Active

https://longcat.chat/blog/longcat-2.0/
4•benjiro29•52m ago•0 comments

What any website can see about you

https://mysysinfo.com
4•rajsuper123•1h ago•3 comments