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.

I built Cashnix – an expense tracker to make managing personal finances easier

https://play.google.com/store/apps/details?id=com.warehouseapp.Spendora&hl=en_IN
1•sbbhaiswar•1m ago•0 comments

Show HN: MFlare – Cloudflare usage and deploy status in the macOS menu bar

https://mflare.yuy1n.io/
1•yuyinws•1m ago•0 comments

Key Facts About U.S. Data Centers

https://www.pewresearch.org/short-reads/2026/11/11/key-facts-about-us-data-centers/
1•giuliomagnifico•4m ago•0 comments

Show HN: Keep all your Claude desktop Code sessions when you swap accounts

https://github.com/ramonmazinga/claude-code-multi-account-bridge
1•ramonmazinga•5m ago•0 comments

Show HN: Taster – Plain-English rules your agent can't forget or ignore

https://github.com/rinkesh2010rpp/taster-ai
1•Rinkesh2010rpp•5m ago•0 comments

Show HN: MetaArena – Compare & Rank 5,353 Meta Global AI Hackathon Projects

https://meta-hackathon.com/
1•ohong•9m ago•0 comments

Tell HN: Gemini Hydrogen Reachable via Vertex

1•jcmp•11m ago•0 comments

Online multiplayer and more on an original NES [video]

https://www.youtube.com/watch?v=eGUmaMX9nOs
1•LaserDiscMan•12m ago•0 comments

Show HN: Tmv – File sorting utility written in Bash

https://github.com/SamerKolasevic29/tmv
2•znj29•19m ago•1 comments

FreeSpeech Messenger – Decentralized messaging with your own relay servers

https://github.com/rebisway/freespeech
2•rebisway•22m ago•0 comments

Possible Futures

https://borretti.me/article/possible-futures
2•varjag•24m ago•1 comments

'GTA 6' Pre-Loads Are Slowing Down the Internet, Warns ISP

https://www.forbes.com/sites/paultassi/2026/11/11/gta-6-pre-loads-are-slowing-down-the-internet-w...
3•vismit2000•24m ago•1 comments

Banks turn to credit insurance as they hit limits on lending to AI boom

https://www.ft.com/content/599932b7-a548-4d45-8ba4-70bdb8d14565
3•petethomas•24m ago•0 comments

Uncle Timmy's Public Blog PSF 2024 Bans

https://tim-one.github.io/psf/
3•Bluestein•28m ago•0 comments

1,509 to 6ms: Why Was Sending Feedback So Slow from Sydney?

https://usero.io/blog/why-feedback-was-slow-from-sydney
1•willsmith72•28m ago•1 comments

Show HN: Sparks Dating reply assistant focused on natural conversarion

https://www.yourdatingassistant.com
2•saurav7__•29m ago•1 comments

Computational Thinking Through Modular Sound Synthesis

https://olney.ai/ct-modular-book/index.html
1•CyMonk•31m ago•0 comments

Every email is three emails in a trench coat

https://buttondown.com/blog/plain-text-email
1•maguay•37m ago•0 comments

Open Agentic Web Challenge – Help Build the Open Agentic Web

https://worldwideagenticweb.org/
1•sjdh76•38m ago•0 comments

Unable to start new workspace agent runs

https://status.openai.com
1•planb•44m ago•1 comments

Flash Aloha

https://johnyao93.github.io/flash-aloha/
2•GaggiX•49m ago•0 comments

WFX Phantom: The GitHub Actions Secret Stealer That Deletes Its Tracks

https://www.stepsecurity.io/blog/wfx-phantom-the-github-actions-secret-stealer
1•varunsharma07•50m ago•0 comments

Show HN: TripCoral – Free itinerary and live updates from public flight data

https://www.tripcoral.com/
1•yuav•51m ago•0 comments

Revisiting inferences of trustworthiness from moral judgments

https://www.nature.com/articles/s41598-023-27943-3
1•bryanrasmussen•54m ago•1 comments

Convert PDF to Markdown and JSON

https://github.com/datalab-to/marker
1•saikatsg•55m ago•0 comments

A Family Tree of Software Job Titles

https://zwischenzugs.com/2026/11/03/a-family-tree-of-software-job-titles/
1•slyall•59m ago•1 comments

Tesserae: E-ink dashboard companion. Compose dashboards and push to e-ink panels

https://github.com/dmellok/tesserae
1•thunderbong•1h ago•0 comments

Exploration–Exploitation Dilemma

https://en.wikipedia.org/wiki/Exploration%E2%80%93exploitation_dilemma
1•aragonite•1h ago•2 comments

Ask HN: Still Getting [Dead] Posts

2•_ananos_•1h ago•0 comments

Show HN: Builderoom – find indie-made software by describing what you need

https://www.builderoom.com/en
1•cena576•1h ago•0 comments