frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•8mo 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•8mo 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•8mo 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: App to spoof GPS location on iOS without jailbreaking

https://github.com/acheong08/ios-location-spoofer
1•acheong08•7m ago•1 comments

Fish Shell

https://fishshell.com/
2•RyanShook•11m ago•1 comments

Ask HN: How do you catch silent logic bugs that don't crash?

1•vortexshadow•11m ago•0 comments

GTA 6's Dynamic Economy Will Change Open‑World Games Forever

https://techfusiondaily.com/gta-6-dynamic-economy/
2•nelkazzu•17m ago•0 comments

Examplefile – Sample Document File Formats

https://www.examplefile.com/document
1•petethomas•18m ago•0 comments

Show HN: DefendFlow Domain Security Board – live scans of popular sites

1•riyao_lin•21m ago•0 comments

Show HN: Headroom (OSS): Cuts LLM costs by 85%

https://github.com/chopratejas/headroom
1•chopratejas•23m ago•1 comments

Breaking the Linearity Barrier: Recursive Swarms for Long-Horizon AI Engineering

https://www.blankline.org/research/horizon-mode
1•satvikpendem•25m ago•0 comments

Data Activation Thoughts

https://galsapir.github.io/sparse-thoughts/2026/01/17/data_activation/
1•galsapir•28m ago•0 comments

Show HN: My way – 18-agent autonomous workflow for ClaudeCode – issues to deploy

https://github.com/avifenesh/awesome-slash
2•anotherCodder•33m ago•0 comments

The life of a playboy publisher who shaped 20th-century literature

https://www.washingtonpost.com/books/2026/01/09/bennett-cerf-biography-nothing-random-feldman-boo...
1•benbreen•36m ago•0 comments

Revisiting Brat Summer: Artists, politicians, and the summer of 2024

https://thelastwave.substack.com/p/revisiting-brat-summer
2•johanam•36m ago•0 comments

Build Your Own AI Coding Agent (Full Guide) [video]

https://www.youtube.com/watch?v=3GjE_YAs03s
2•kburman•41m ago•0 comments

Umarell – men of retirement age who spend their time watching construction sites

https://en.wikipedia.org/wiki/Umarell
3•gurjeet•41m ago•0 comments

The SaaS Selloff: AI and Interest Rates

https://davefriedman.substack.com/p/the-saas-selloff-ai-and-interest
4•stosssik•44m ago•0 comments

BioNeMo Platform Accelerate AI-Driven Drug Discovery

https://nvidianews.nvidia.com/news/nvidia-bionemo-platform-adopted-by-life-sciences-leaders-to-ac...
1•gmays•45m ago•0 comments

How do I stop participating?

https://geohot.github.io//blog/jekyll/update/2026/01/18/how-do-i-stop.html
4•JuanJohnJames•47m ago•0 comments

AI Sandbox for Claude Code CLI with Node and Python SDKs

https://sandbox.stateset.app
2•domsteil•55m ago•0 comments

You Need More AWS Accounts Than You Think

https://cloudposse.com/blog/you-need-more-aws-accounts-than-you-think
4•mooreds•56m ago•4 comments

Show HN: Gollem – Go framework for agentic AI app with MCP and built-in tools

https://github.com/m-mizutani/gollem
1•masa00•1h ago•0 comments

A new way to call C from Java: how fast is it?

https://lemire.me/blog/2026/01/17/a-new-way-to-call-c-from-java-how-fast-is-it/
1•mfiguiere•1h ago•1 comments

How to Read a Room in 5 Minutes

https://oedmethod.substack.com/p/how-to-read-a-room-in-5-minutes
1•truenfel•1h ago•0 comments

AI and Corporate Capture of Knowledge

https://www.schneier.com/blog/archives/2026/01/ai-and-the-corporate-capture-of-knowledge.html
5•fosco•1h ago•0 comments

Algebra will return to S.F. middle schools after more than a decade

https://www.sfchronicle.com/bayarea/article/algebra-return-sf-middle-schools-21299694.php
1•mikhael•1h ago•0 comments

MIT's Computer Systems Security (2024)

https://css.csail.mit.edu/6.858/2024/
2•barishnamazov•1h ago•0 comments

More HTTP/3 focus, one back end less

https://daniel.haxx.se/blog/2026/01/17/more-http-3-focus-one-backend-less/
2•todsacerdoti•1h ago•0 comments

The Born-XML Shakespeare Edition: The View from the New Oxford Shakespeare

http://www.gabrielegan.com/publications/Egan2019e.htm
1•mmooss•1h ago•0 comments

IRISC: An ARMv7 assembly interpreter and computer architecture simulator

https://polysoftit.co.uk/irisc-web/
5•rtybanana•1h ago•1 comments

Show HN: Agam Space – Self-hosted, zero-knowledge, E2EE file storage

https://github.com/agam-space/agam-space
3•rameshl•1h ago•0 comments

Astronomers spot one of the largest spinning structures found in the Universe

https://www.ast.cam.ac.uk/news/astronomers-spot-one-largest-spinning-structures-ever-found-universe
2•hhs•1h ago•0 comments