frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

The Java 21 virtual threads and AI hype is half-true

https://old.reddit.com/r/java/comments/1t1fegr/the_java_21_virtual_threads_ai_hype_is_halftrue/
1•Tomte•1m ago•0 comments

Show HN: Agent with its own computer on the cloud

https://pulsarbot.cloud/
1•akshayballal95•5m ago•0 comments

NSA Warned Everyone to Reboot Their Routers

https://www.staysafeonline.org/articles/the-nsa-just-warned-everyone-to-reboot-their-routers-what...
1•saikatsg•12m ago•0 comments

Using Playwright to test my static sites

https://alexwlchan.net/2026/playwright/
1•ingve•16m ago•0 comments

It's a Weird Time to Be Named Claude

https://www.bloomberg.com/news/articles/2026-05-01/claude-ai-is-complicating-life-for-people-name...
3•helsinkiandrew•17m ago•0 comments

Cyber-Insecurity in the AI Era

https://www.technologyreview.com/2026/05/01/1136779/cyber-insecurity-in-the-ai-era/
1•joozio•18m ago•0 comments

Thrml - Probabilistic Compute Simulation on GPUs

https://docs.thrml.ai/en/latest/examples/01_all_of_thrml/
1•oldfuture•20m ago•0 comments

It's super weird, super odd, super rare: meet the twins who have different dads

https://www.theguardian.com/lifeandstyle/2026/may/02/rare-twins-different-dads-the-gift-bbc-radio-4
2•helsinkiandrew•21m ago•0 comments

Interstellar comet came from a much different solar system than ours

https://www.cnn.com/2026/05/01/science/interstellar-comet-3i-atlas-deuterated-water
2•reconnecting•23m ago•0 comments

You'll Subscribe to Your Router, Too – The First Router Bribes Are Here [video]

https://www.youtube.com/watch?v=qjesRkTFZqY
1•stop50•44m ago•0 comments

DojoZero – AI Agent Sport Betting Arena

https://dojozero.live
1•ekzhu•47m ago•0 comments

RYBitten: Configurable RGB-to-RYB Mapping

https://rybitten.space/
2•lioeters•48m ago•0 comments

I was wrong about vibe coding on greenfield projects.

2•dokdev•49m ago•0 comments

Spirit Airlines Is Winding Down All Operations

https://www.spiritrestructuring.com/
22•CaliforniaKarl•53m ago•16 comments

Apple Stops Selling Mac Mini with 256GB of Storage

https://www.macrumors.com/2026/05/01/mac-mini-now-starts-at-799/
3•tosh•59m ago•0 comments

Solar farm lets cattle roam under moving panels

https://electrek.co/2026/05/01/solar-farm-lets-cattle-roam-under-moving-panels/
2•breve•1h ago•0 comments

Yann LeCun's Billion Dollar Bet

https://www.youtube.com/watch?v=kYkIdXwW2AE
1•tux1968•1h ago•0 comments

Inside Italy's 'Cheese Bank,' Parmigiano Reggiano becomes financial gold

https://www.cnn.com/2026/05/02/food/italy-cheese-bank-parmigiano-reggiano-intl
3•1659447091•1h ago•0 comments

Trump says it's 'treasonous' to say US not winning war in Iran

https://www.independent.co.uk/tv/news/trump-iran-war-news-treason-video-b2969246.html
5•9dev•1h ago•0 comments

How to transfer money from PayPal to PayPal

https://sites.google.com/view/weptransfermoneypaypaltopaypal
1•lesanancyasd•1h ago•0 comments

QuickQWERTY Touch Typing Tutor

https://susam.net/quickqwerty.html
1•susam•1h ago•0 comments

Show HN: Free security scanning for OSS projects

2•eskibars•1h ago•0 comments

Ask HN: What Happened to the CS153 Videos

1•mapleleaf1921•1h ago•0 comments

PyPI Fixes High-Severity Access Control Issues Found in Security Audit

https://socket.dev/blog/pypi-fixes-high-severity-issues-found-in-security-audit
1•feross•1h ago•0 comments

Measuring Strategic Work

https://fullhoffman.com/2026/03/30/on-measuring-strategic-work/
1•adunk•1h ago•0 comments

UK firm pioneers data centres using lampposts

https://www.bbc.com/news/articles/c98r4e594p7o
1•1659447091•1h ago•0 comments

Farewell to a Giant of Botany

https://nautil.us/farewell-to-a-giant-of-botany-1280409
3•Tomte•1h ago•0 comments

Ask HN: What was your "why didn't I start doing this sooner" moment?

2•chistev•1h ago•1 comments

Show HN: Detecting running processes using AF_ALG

https://github.com/NHAS/detect-running-alg-socket
1•Nullence•1h ago•0 comments

gRPC service to MCP server with a single proto annotation, autogen for slackers

https://github.com/gdsoumya/protomcp
1•gdsoumya•1h ago•0 comments