frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

What Language Is This? Ask Your Tokenizer

https://arxiv.org/abs/2602.17655
1•tigoo•2m ago•0 comments

The History of a Security Hole

https://www.os2museum.com/wp/the-history-of-a-security-hole/
1•st_goliath•4m ago•0 comments

New electrolyzer turns plastic-waste syngas into ethylene with less energy

https://techxplore.com/news/2026-02-electrolyzer-plastic-syngas-ethylene-energy.html
1•PaulHoule•7m ago•0 comments

Final Bench: The Real Bottleneck to AGI Is Self-Correction

https://huggingface.co/blog/FINAL-Bench/metacognitive
1•seawolf2357•7m ago•0 comments

Free Sort Feed Alternative for Instagram Content Creators

https://chromewebstore.google.com/detail/outliers/heogkfpbeagpoodininnfhdgjmpdalgj
1•Rostik312•8m ago•1 comments

GTIG AI Threat Tracker: Distillation, Experimentation, Adversarial Use

https://cloud.google.com/blog/topics/threat-intelligence/distillation-experimentation-integration...
1•thread_id•9m ago•0 comments

Show HN: A minimal framework-agnostic agent-to-agent execution layer

https://github.com/joaquinariasco-lab/flowing
1•silentvector•9m ago•0 comments

Why a Raspberry Pi Can't Be a Stoplight [video]

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

Websites on Nekoweb

https://nekoweb.org/explore
1•TigerUniversity•12m ago•0 comments

Agentation Cut Claude Code UI Iterations in Half

https://reactdevelopment.substack.com/p/how-agentation-cut-claude-code-ui
1•javatuts•12m ago•0 comments

Apple researchers develop on-device AI agent that interacts with apps for you

https://9to5mac.com/2026/02/20/apple-researchers-develop-on-device-ai-agent-that-interacts-with-a...
1•brandonb•13m ago•0 comments

Show HN: Ambient – Local cognitive daemon with episodic+procedural memory

https://github.com/sgunadhya/ambient
1•shivaodin•13m ago•0 comments

I'm #1 on Google thanks to AI bullshit [video]

https://www.youtube.com/watch?v=6uKZ84zwJI0
2•basilikum•17m ago•0 comments

Gurning contests

https://en.wikipedia.org/wiki/Gurn
1•petethomas•20m ago•0 comments

eBPF on Hard Mode

https://feyor.sh/blog/ebpf-on-hard-mode/
1•rossbing•20m ago•0 comments

Show HN: Quill – A system-wide tech dictionary for the AI coding era

https://github.com/uptakeagency/quill
1•c3nx•22m ago•0 comments

Securing AI Agents: Architecture for Systems You Can't Control

https://medium.com/@epappas/securing-ai-agents-an-architecture-for-systems-you-cant-fully-control...
1•hevalon•22m ago•0 comments

Lobste.rs Migrates from MariaDB to SQLite

https://lobste.rs/s/oz7ebk
1•zdw•23m ago•1 comments

Trump Threatens Netflix with 'Consequences' over Rice Board Seat

https://www.bloomberg.com/news/articles/2026-02-21/trump-threatens-netflix-with-consequences-over...
3•petethomas•23m ago•0 comments

Lobste.rs Migrates from MariaDB to SQLite

https://lobste.rs/s/oz7ebk/lobste_rs_migrates_from_mariadb_sqlite
3•wa008•25m ago•0 comments

How and Why Local LLMs Perform on Framework 13 AMD Strix Point

https://msf.github.io/blogpost/local-llm-performance-framework13.html
1•todsacerdoti•25m ago•0 comments

Cisco set to release hypervisor as VMware alternative

https://www.theregister.com/2026/02/16/cisco_nfvis_for_uc_hypervisor/
1•walterbell•25m ago•0 comments

Show HN: Agentic Gatekeeper – AI pre-commit hook to auto-patch logic errors

https://github.com/revanthpobala/agentic-gatekeeper
1•revanth1108•26m ago•0 comments

Open AI Letter to Congress February 12 2026

https://assets.bwbx.io/documents/users/iqjWHBFdfxIU/rRmql_jJcxb4/v0
2•thread_id•28m ago•1 comments

The Future of Math Research in the Age of AI

https://siliconreckoner.substack.com/p/the-future-of-math-research-in-the
4•jbkcc•28m ago•0 comments

CanvaSub – generate styled subtitles and render ready-to-post short videos

https://canvasub.com
1•KhaledAlam•28m ago•1 comments

What happens when your creative tools start thinking with you

https://twitter.com/kickingkeys/status/2025324829717467612
2•kickingkeys•28m ago•0 comments

Steinberger, Karpathy et al. on the negativity of Hacker News

https://twitter.com/steipete/status/2025334681047015791
3•mellosouls•30m ago•1 comments

Show HN: I built a better way to track regulatory action

https://frtracker.app
2•tldrthelaw•31m ago•1 comments

Europe's Cyber Bullets Can't Replace Political Will

https://www.lawfaremedia.org/article/europe%27s-cyber-bullets-can%27t-replace-political-will
1•thread_id•32m ago•2 comments