frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Make boxes from dried citrus rind

https://pixey.org/p/confluency/917527402164543260
1•sohkamyung•40s ago•0 comments

PAZ O.S. – A "Bio-Civic" Alignment Framework for Ethical LLMs

https://github.com/carropereziago-blip/PAZ-O.S-GLOBAL
1•PiSounds•2m ago•1 comments

FCC Poised to Exempt Amateurs from Foreign Adversary Reporting Requirements

https://www.arrl.org/news/fcc-poised-to-exempt-amateurs-from-foreign-adversary-reporting-requirem...
1•7402•4m ago•0 comments

FTC Finalizes Order Banning GM from Sharing Driver Data

https://www.pcmag.com/news/ftc-finalizes-order-banning-gm-from-sharing-driver-data?test_uuid=04Ip...
4•CGMthrowaway•8m ago•0 comments

Ask HN: Browser extension vs. native app for structured form filling?

1•livrasand•8m ago•0 comments

IETF@40

https://www.ietf.org/blog/ietf-40/
2•sohkamyung•11m ago•0 comments

Re: Mix: open-source repairable blender

https://github.com/openfunkHQ/reMix
1•rishikeshs•13m ago•0 comments

I thought Grammarly was essential. It wasn't

https://www.makeuseof.com/stopped-paying-for-grammarly-once-found-free-open-source-alternative/
2•chilipepperhott•22m ago•0 comments

I have settled on XChaCha20+Blake3 as the AE suite of choice for my projects

https://mccarty.io/chacha20-blake3/
2•enz•26m ago•0 comments

For profit university offers scholarship to RuneScape skill cape recipients

https://usv.edu/max-achievement-scholarship
1•rickcarlino•27m ago•0 comments

Show HN: Neurop Forge: Live Demo /Real AI Action

https://neurop-forge.onrender.com/demo/google
1•LBWasserman•27m ago•0 comments

"Freedom of Speech" Exists – But "Freedom of Reach" Is a Black Box

1•Ayanonymous•28m ago•1 comments

LimX COSA – Agentic OS for Humanoid Robots (YouTube)

https://www.youtube.com/watch?v=0hIqs3TBb5g
2•_medihack_•29m ago•0 comments

Do Cloudflare's Lava Lamps Do Anything?

https://notnotp.com/notes/do-cloudflares-lava-lamps-actually-do-anything/
2•enz•29m ago•1 comments

Emergent topological semimetal from quantum criticality

https://www.nature.com/articles/s41567-025-03135-w
1•bryanrasmussen•30m ago•0 comments

Eddy Goldfarb – The Man Who Invented More Than 800 Iconic Toys [video]

https://www.youtube.com/watch?v=9sKKan_Q9VU
1•embedding-shape•33m ago•0 comments

US moves to strip sovereign wealth investors of US tax perks

https://www.ft.com/content/de1968ab-3db3-4aa6-8489-35e7cdb81be7
4•alephnerd•34m ago•3 comments

Fix the two-party system with proportional representation

https://agrarianparty.org/platform/peoples-act
4•tedkimble•35m ago•1 comments

Before I forget how I got here

https://richhaase.com/blog/2026-01-14-before-i-forget-how-i-got-here/
1•richhhh•36m ago•1 comments

FAA warns on Central, South American flights over potential military actions

https://www.reuters.com/world/americas/faa-issues-warnings-airlines-central-south-american-flight...
5•toomuchtodo•40m ago•2 comments

AT&T collapse:A single typo that shut down the US long-distance network

https://en.wikipedia.org/wiki/1971_AT%26T_network_switching_system_collapse
2•xthe•41m ago•1 comments

Debian gnome team plans to kill GTK2

https://ludditus.com/2026/01/15/debian-has-its-retards-too-they-plan-to-kill-gtk2/
2•jandeboevrie•44m ago•0 comments

Install.md: A standard for LLM-executable installation

https://www.mintlify.com/blog/install-md-standard-for-llm-executable-installation
9•npmipg•46m ago•14 comments

Show HN: ExprTk High-Performance C++ Math Expression Parser Evaluation Engine

https://www.partow.net/programming/exprtk/index.html
3•exprtk•48m ago•0 comments

What happens to cities when the jobs leave?

https://deadneurons.substack.com/p/what-happens-to-cities-when-the-jobs
1•nr378•48m ago•0 comments

Drawbot: Let's hack something cute (2025)

https://www.atredis.com/blog/2025/9/30/drawbot-lets-hack-something-cute
11•notmine1337•48m ago•4 comments

Show HN: Polymcp Implements Ollama for Local and Cloud Model Execution

2•justvugg•49m ago•0 comments

Custom firmware for x4 eInk device

https://github.com/crosspoint-reader/crosspoint-reader
1•xteink•50m ago•0 comments

Radithor

https://en.wikipedia.org/wiki/Radithor
2•radeeyate•52m ago•0 comments

Show HN: Paintracker.ca, a PWA pain tracker that keeps data on device by default

https://www.paintracker.ca/
1•crisiscore_sys•54m ago•1 comments