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.

Synology urges users to update DSM following ransomware attacks

https://www.synology.com/en-global/company/news/article/C3RB3R
1•campuscodi•40s ago•0 comments

Ask HN: Estimate for SWE wages in 10 years?

1•NotAnOtter•1m ago•0 comments

Show HN: Epochnbd – Crash-Consistent Linux NBD Block Device on GCS/Google Drive

https://github.com/c789a123/epochnbd
1•c789a123•4m ago•0 comments

ATmega-compatible 8-bit core in SystemVerilog on Skywater 130nm

https://github.com/wkoszek/akmega
1•barelyusable•10m ago•0 comments

Trinity Desktop R14.1.7 Released

https://wiki.trinitydesktop.org/Release_Notes_For_R14.1.7
1•calvinmorrison•10m ago•0 comments

Can light be used to cyberattack?

1•izbizi•10m ago•0 comments

John Leeson (the voice of K9) has died

https://www.doctorwhonews.net/2026/11/john_leeson_1943_2026.html
3•slyrus•15m ago•1 comments

Friday: Run Claude Code and Codex as a crew on your computer (open source)

https://github.com/splashxmoon/friday
1•arundass•15m ago•0 comments

Show HN:Type It Big – full-screen signs, timer, scoreboard and teleprompter

https://typeitbig.com/
2•fineenoughstudi•16m ago•0 comments

Show HN: Quarter – A TUI launcher to browse, search and launch your games

https://github.com/stronxyo/quarter
1•stronxyo•22m ago•0 comments

Mastodon 5.0: A closer look – Mastodon Blog

https://blog.joinmastodon.org/2026/10/5.0-a-closer-look/
2•rbanffy•23m ago•0 comments

"Theranos 2.0" startup has patents, investors but no product

https://fortune.com/2026/10/25/elizabeth-holmes-partner-billy-evans-haemanthus-blood-testing-star...
3•gosen•23m ago•1 comments

Show HN: AirFlick Drop – AirDrop for Windows with zero mobile app installs

https://github.com/k39139215-gif/airflick-drop
1•Snapmotion3421•25m ago•0 comments

Turn Superlogical's Rex into an AI Harness

https://github.com/alpcanaydin/rexmod/
1•alpcan•25m ago•1 comments

Qt Bridges Technology Preview: Qt Quick UIs for Rust and C# Developers

https://www.qt.io/blog/qt-bridges-technology-preview-rust-csharp
4•leonardsouza•26m ago•0 comments

A Global Anti-Right Backlash

https://www.newyorker.com/news/global-notes/a-global-anti-right-backlash
2•rbanffy•26m ago•0 comments

Copper is an operating system for robots

https://github.com/copper-project/copper-rs
2•ahamez•27m ago•0 comments

People 'Vibe Manufacturing'

https://www.businessinsider.com/vibe-manufacturing-ai-prototypes-cad-coding-2026-11
2•theanonymousone•29m ago•2 comments

Rivian, Ford and GM are using 3D printing to reduce costs

https://www.cnbc.com/2026/10/31/automakers-3d-printing-ford-rivian-gm.html
1•andrewstetsenko•31m ago•0 comments

I indexed 37h of my YouTube library using an RTX 4090 and local ML models in 24h

3•iliashad•33m ago•3 comments

Quill Cowork

https://www.youtube.com/watch?v=M97H4umU3pE
1•Bhavishya2011•34m ago•0 comments

FerroUI a cross-platform UI framework for Rust

https://ferroui.github.io/website/
3•wiso•35m ago•1 comments

The Linux 7.4 Merge Window Is Easily the Largest in Kernel History

https://www.phoronix.com/news/Linux-7.4-Massive-Window
3•acossta•37m ago•0 comments

The Future of the Internet

https://erikjohannes.no/posts/20261023-the-future-of-the-internet/index.html
4•speckx•40m ago•2 comments

Artists hiding painted pebbles around the world

https://www.bbc.com/news/articles/c674pvkkp2eyo
1•1659447091•41m ago•0 comments

How to Conquer a Nation Without Fighting [video]

https://www.youtube.com/watch?v=6NTDGriJZmU
2•PorciiVorbesc•43m ago•0 comments

Nouli – A decision model TUI to branch and blend multiple histories

https://github.com/nouli-ai/nouli
2•SurfingAlpaca•44m ago•0 comments

Windows 95 is still being used to sort eggs in Germany

https://www.pcgamer.com/hardware/windows-95-arguably-the-first-pc-gaming-os-is-still-being-used-t...
2•speckx•50m ago•1 comments

Are We All Too Cynical for Star Trek?

https://www.thebulwark.com/p/are-we-all-too-cynical-for-star-trek
3•Bluestein•51m ago•1 comments

Moving an RTS from Godot to Lobster

https://age-of-anything.com/devlog/2026-10-30-moving-to-lobster
1•Drache93•51m ago•1 comments