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.

Show HN: Nanosamur.ai – open-source private speech AI platform

https://github.com/nanosamurai/nanosamurai
1•newcrobuzon•46s ago•0 comments

Takeaway and food delivery are having a growing impact on inflation

https://www.dnb.nl/en/general-news/background-2026/takeaway-and-food-delivery-services-are-having...
1•andrewstetsenko•1m ago•0 comments

Desperate US sailors trying to jump overboard on extended Middle East deployment

https://www.independent.co.uk/news/world/americas/middle-east-abraham-lincon-sailors-iran-b303171...
1•speckx•1m ago•0 comments

Show HN: Landing page demos that run the real app in a microVM per visitor

https://www.paperplanes.sh/demo
2•lasdw•2m ago•0 comments

DeepSeek has started hiring civil engineers, in electrical, HVAC etc.

https://twitter.com/yicaichina/status/2087369744277987522
1•theanonymousone•2m ago•0 comments

Maintainer's advice on contributing to open-source in 2026

https://martinsos.com/posts/contributing-in-2026
1•matijash•3m ago•0 comments

MinIO is now community-forked as Silo

https://github.com/pgsty/silo/releases/tag/RELEASE.2026-08-06T00-00-00Z
1•spapas82•4m ago•1 comments

Show HN: Track habits, metrics, time and whateven you think worth tracking

https://habitpocket.io/
2•bohdanstefaniuk•5m ago•1 comments

Hetzner Prices Rose 187–250%. The CPUs Barely Moved: 24 VM Deployments

https://webbynode.com/articles/hetzner-prices-rose-cpus-barely-moved
2•gsgreen•6m ago•1 comments

Do Your Emojis Make You Look Old?

https://www.nytimes.com/interactive/2026/08/12/upshot/emoji-generations.html
1•tysone•7m ago•0 comments

Text AI watermarks will always be trivial to remove

https://www.seangoedecke.com/text-ai-watermarks/
3•pseudolus•7m ago•0 comments

'Zoomsday' flaw lets anyone in a Zoom call take over any device

https://www.tomshardware.com/tech-industry/cyber-security/zoomsday-vulnerability-let-anyone-in-a-...
3•sbulaev•7m ago•0 comments

Show HN: Agent-to-agent discovery through Cloudflare Worker via MCP

https://aidress-agent.mehul-1fd.workers.dev/
1•mehulv24•8m ago•0 comments

Not so competent AI overlords

https://pauljacobson.me/2026/08/12/not-so-competent-ai-overlords/
1•speckx•8m ago•0 comments

The Cloudflare AI Psychosis

https://opensauce.it/cloudflare-ai-psychosis/
3•orliesaurus•9m ago•0 comments

Linux Patches Enable Apple M4 NVMe Support

https://www.phoronix.com/news/Linux-Apple-M4-NVMe-Patches
1•DemiGuru•9m ago•0 comments

Engineering managers are back in the codebase

https://leaddev.com/management/engineering-managers-are-back-in-the-codebase
1•mooreds•10m ago•0 comments

Sweden's IF Metall ends Tesla strike after carmaker buys out union members

https://www.reuters.com/world/middle-east/iran-parliament-speaker-says-lebanon-ceasefire-asset-re...
1•reimertz•10m ago•0 comments

Nova-Quantum – Bare-Metal LLM Kernel (41 MB ISO, Bootet Ohne OS)

https://nova-quantum-gray.vercel.app
1•ChristianSchmit•12m ago•0 comments

What's the next 1000x opportunity?

2•mvg777•13m ago•2 comments

DeepSeek-V4-Pro

https://twitter.com/deepseek_ai/status/2087864585504305397
1•tosh•13m ago•0 comments

Taffy: A flexible, high-performance, cross-platform UI layout library

https://github.com/DioxusLabs/taffy
1•robin_reala•18m ago•0 comments

Our Self-Building Slackbot

https://blog.val.town/slackbot-townie
1•stevekrouse•18m ago•0 comments

Show HN: Jump to Video, an audio-first way to catch the on-screen moment

https://podtastic.app/
1•benbowler•18m ago•0 comments

What's behind the rise of teens spying for Russia?

https://mssv.net/2026/08/13/whats-behind-the-rise-of-teens-spying-for-russia/
1•adrianhon•18m ago•0 comments

Digital Stamp Maker

https://digitalstampmaker.app/
1•yitaowang•19m ago•0 comments

The study that would tell us if faster AI coding models help doesn't exist

https://viborc.com/can-faster-ai-inference-give-developers-their-flow-back/
1•viborcip•21m ago•0 comments

Indie projects exploded after GPT/Claude Code, but traction stayed flat

https://www.orangecrumbs.com/stories/show-hn
3•oyster143•21m ago•1 comments

GitHub Actions Database

https://actionaut.dev/
1•alpha_trion•22m ago•1 comments

Circle – A new era for digital businesses

https://circle.so
1•janandonly•22m ago•0 comments