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.

A world model that turns a few photos of a room into a simulation called Mundo

https://huggingface.co/blog/Luo-Yihang/mundo-world-model
1•sgiannopoulos•2m ago•0 comments

Switching Password Managers in 2026

https://rmondello.com/2026/09/07/switching-password-managers-2026/
1•birdculture•3m ago•0 comments

A public tourist webcam captured the fire at Hitler's Eagle's Nest

https://claude.ai/artifact/KQRxeaEpmtmwT8HwzhHRUt
1•mtford•6m ago•0 comments

A Love Letter to the Super Dimension Fortress

https://brennan.day/a-love-letter-to-the-super-dimension-fortress/
1•jruohonen•8m ago•0 comments

CppC – memory-safe C++ compiler (catches UAF, double-free at build time)

https://github.com/0x7m7/cppC
1•0x7m7•20m ago•1 comments

iPhone Duo Durability: Destroyed [video]

https://www.youtube.com/watch?v=5laDGy4INB0
2•phoronixrly•26m ago•1 comments

ThinkRail from JetBrains

https://thinkrail.ai/
1•wilsonjholmes•30m ago•0 comments

ERC Starting Grant success rate likely to fall to 4.8%

https://sciencebusiness.net/news/r-d-funding/european-research-council/erc-starting-grant-success...
1•theanonymousone•31m ago•0 comments

Node2Shell evolves to HTTP2Shell, blast radius affects many HTTP libs and apps

https://www.aikido.dev/blog/python-packages-vulnerable-node2shell-brotli-cve-2026-111497
3•theanonymousone•32m ago•0 comments

Show HN: Local AI for dictation, meetings, background tasks and more

https://sideos.ai
1•askintml•34m ago•0 comments

Same open model, two harnesses: 24/25 vs. 19/25 on 25 real merged pull requests

https://octomind.run/blog/coding-agent-benchmark-real-prs
1•donk8r•34m ago•0 comments

Ruby Regexp compilation fixes related to GitLab

https://www.ruby-lang.org/en/news/2026/10/23/regexp-fixes-for-gitlab-cves/
2•ksec•36m ago•0 comments

USB AI

https://usb-ai-hazel.vercel.app/
1•fipra1969•39m ago•0 comments

Cube – Run FreeBSD ARM64 VMs Natively on Apple Silicon Macs

https://cube.neuralengine.net
1•darcyliu•39m ago•0 comments

Worca – Deterministic Multi-Agent Pipeline

https://worca.dev/
1•Tomte•41m ago•0 comments

A high-performance gRPC server library in C

https://github.com/ThirdLetterC/grpc
1•yehors•45m ago•0 comments

Self-Contained String and Unicode (v17) Library for ISO C11/C17/C23

https://github.com/ThirdLetterC/cstr
2•yehors•46m ago•0 comments

Kimi K3.1 Pro will be leading open-weights model once its weights are published

https://artificialanalysis.ai/articles/kimi-k3-1-pro-launch-benchmarks
1•theanonymousone•47m ago•0 comments

Show HN: Inspect AI recommendation evidence and turn findings into website tasks

https://aicitewho.com/
1•chuanbuilds•51m ago•0 comments

Wish: Make SSH apps, just like that

https://github.com/charmbracelet/wish
2•ssernikk•51m ago•0 comments

A Self-Defining Operator for Distributed Applications

https://arxiv.org/abs/2610.14547
1•matt_d•52m ago•0 comments

Show HN: Calsee – a calculator whose themes are generated from a single seed

https://calsee.tech/
1•mishrabhilash•53m ago•0 comments

ZK-irp-firewall – Rust/Python ZK security framework for AI agents

https://github.com/trenttrinder1111-byte/zk-irp-firewall
1•trenttrinder111•55m ago•0 comments

New York's fracking ban leaves community to crumble while Pa. neighbors boom

https://nypost.com/2026/10/22/us-news/new-yorks-fracking-ban-leaves-community-to-crumble-while-pe...
1•ArizonaGal•56m ago•0 comments

Show HN: How religions evolved? Charts, Maps, Citations

https://howitevolved.com/chart/
1•silly073•1h ago•0 comments

Nonfiction Is Struggling. Publishers Are Making It Worse

https://thewalrus.ca/nonfiction-is-struggling-publishers-are-making-it-worse/
2•Michelangelo11•1h ago•1 comments

In the biotech world, you can't see everything

https://www.technologyreview.com/2026/10/23/1147610/biotech-you-can-see-everything-theranos-holmes/
1•joozio•1h ago•0 comments

Show HN: Encarta 95 Web

https://benwinding.github.io/encarta_95_web/#/article/Art
3•gitgud•1h ago•0 comments

A supercomputer is about to launch to the moon aboard a rover named FLIP

https://www.space.com/space-exploration/a-supercomputer-is-about-to-launch-to-the-moon-for-the-1s...
4•pseudolus•1h ago•0 comments

Why moving sprites are slow

https://debuff.jp/blog/why-moving-sprites-are-slow
1•gregjw•1h ago•0 comments