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.

Building a local-first family tree app

https://purplehoisin.com/own-your-family-tree/
1•fersarr•2m ago•0 comments

Cezar: A parallel coding agents orchestrator

https://github.com/open-mercato/cezar
1•zaiste•5m ago•0 comments

AI Agents Governance: Who guards the guardrails?

https://www.nofire.ai/blog/who-audits-the-ai-agent
3•spirosoik•6m ago•0 comments

Senate Panel Has Obtained Copy of Fauci's Cellphone

https://www.nytimes.com/2026/08/05/us/politics/fauci-phone-senate.html
2•ianeff•7m ago•0 comments

A year of AI disclosure in critical packages

https://nesbitt.io/2026/08/06/a-year-of-ai-disclosure-in-critical-packages.html
1•omkar-foss•7m ago•0 comments

AI music generator Suno loses copyright infringement legal case

https://www.nme.com/news/music/ai-music-generator-suno-loses-copyright-infringement-legal-case-39...
1•passwordoops•12m ago•0 comments

Vista: A Visual Harness for Reasoning in an Interactive World

https://vista-research.github.io/
1•haxfenx•14m ago•0 comments

Cloudflare OS: great insight into how to embrace internal AI demand securely

https://cephalosec.com/blog/cloudflare-os-insights-on-embracing-internal-ai-growth-securely/
1•Versipelle•14m ago•0 comments

Time Magazine has a separate version of its website with ads only AI can see

https://www.theregister.com/ai-and-ml/2026/08/05/time-magazine-has-a-separate-version-of-its-webs...
1•rguiscard•15m ago•0 comments

Slow Light Communications

https://medium.com/luminasticity/slow-light-communications-81bdd7a87afe
1•bryanrasmussen•16m ago•0 comments

You Should Almost Never Use AI to Write Anything Substantive

https://www.erichgrunewald.com/posts/why-i-think-you-should-almost-never-use-ai-to-write-anything...
1•erwald•17m ago•0 comments

Stillsane: Detect silent quality drift in deployed LLM apps

https://pypi.org/project/stillsane/
1•MSanket9•19m ago•0 comments

Grounded document extraction with bounding-box citations

https://pspdfkit.github.io/nutrient-extraction-samples/
1•marakiii•20m ago•0 comments

Turning Air into Alcohol [video]

https://www.youtube.com/watch?v=ID-K64Jk6JM
1•DiscourseFan•21m ago•0 comments

HCC – local-first, cross-agent memory layer (98% cache hit, $40/mo)

https://github.com/icemaple77/hanyan-cognitive-core
1•icemaple77•21m ago•0 comments

Products make going back unimaginable

https://datawarrior.medium.com/the-best-products-make-going-back-unimaginable-30049de81568
1•nikolamilosevic•24m ago•0 comments

Chrysalis (hypothetical moon) and Saturn's rings

https://en.wikipedia.org/wiki/Chrysalis_(hypothetical_moon)
1•simonebrunozzi•24m ago•0 comments

Show HN: My free web dev courses, no signup, with exercises

https://en.andros.dev/courses/
1•andros•27m ago•0 comments

The Aikido Machine: on-prem AI pentesting that never leaves your network

https://www.aikido.dev/blog/the-aikido-machine-on-prem-ai-pentesting-that-never-leaves-your-network
1•ultra2d•28m ago•0 comments

Show HN: Rask – The .NET One Person Framework

https://pal-tamas.github.io/rask/
1•pal-tamas•29m ago•0 comments

Using Git diffs to circumvent Fable's safeguards

https://blog.j11y.io/2026-07-18_Jailbreaking-Fable-disinformation/
2•padolsey•30m ago•0 comments

ESPBanking – Offline, local banking server running on a single ESP32

https://github.com/dumasha05/ESPBBanking1/blob/main/README.md
1•Dumasha05•30m ago•0 comments

Anthropic is hiring an AI chip design team

https://techcrunch.com/2026/08/05/anthropic-is-hiring-an-ai-chip-design-team/
2•mpfect•30m ago•0 comments

Claude Fable 5 finds a tiny formula that topples an 87-year-old math conjecture

https://www.sciencedaily.com/releases/2026/08/260804034634.htm
3•theanonymousone•34m ago•0 comments

1 RCE, 2 AI models, 0 ways to tell who was right

https://zhenyi.gibber.blog/1-rce-2-ai-models-0-ways-to-tell-who-was-right
1•zhenyi•34m ago•0 comments

We built a local SQL engine for DynamoDB

https://dynotable.com/blog/local-sql-engine-for-dynamodb
2•dynotable•35m ago•1 comments

Train the AI to Want to Die

https://slimemoldtimemold.com/2026/08/05/a-stupid-idea-for-ai-alignment-we-came-up-with-by-lookin...
1•mrcgnc•37m ago•0 comments

Fable 5 on ARC-AGI (Verified)

https://twitter.com/arcprize/status/2085115252035785168
2•tosh•41m ago•0 comments

Building Scalable Control Planes

https://www.allthingsdistributed.com/2026/08/on-building-scalable-control-planes.html
1•janvdberg•43m ago•0 comments

Google Search AI leaks ACTUAL name of a game's upcoming character

https://www.reddit.com/r/IndieDev/comments/1vfwuf5/a_player_found_out_the_actual_name_of_my_games/
3•erremerre•44m ago•0 comments