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.

OpenAI DevDay 2026

https://devday.openai.com
1•almyk•1m ago•0 comments

Tokenomics: Fable 5.1's reduced cache read makes it cheaper than Opus

https://skids.dev/blog/fable-cache-tokenomics/
1•ryanskidmore•3m ago•0 comments

Data Seeding in .NET

https://learn.microsoft.com/en-us/ef/core/modeling/data-seeding
1•Brysonbw•4m ago•0 comments

Ask HN: Taxes while living abroad as a US citizen

1•wnolens•4m ago•0 comments

Analysis: Why Rollback Netcode Is Better (2020) [video]

https://www.youtube.com/watch?v=0NLe4IpdS1w
1•CharlesW•5m ago•0 comments

Israeli Effective Altruism Firm Behind OpenAI, Anthropic, and Meta Cyberattacks

https://twitter.com/brianchau57/status/2099580981271318606
2•MrBuddyCasino•11m ago•0 comments

Show HN: Oversold.com – Analyze a stock in 30 seconds

https://oversold.com/
3•wolfman1•17m ago•0 comments

ICE Attacks Two U.S. Citizen Minors on Their Way to Enlist in Marines

https://newrepublic.com/post/215393/ice-attacks-us-citizen-minors-enlist-marines
4•HotGarbage•20m ago•0 comments

People We Meet Along the Way – The Story of April and Gary

https://rinaldoj.substack.com/p/the-people-we-meet-along-the-way-829
1•jrinaldo68•22m ago•0 comments

Newfound fossils are spilling Denisovan secrets

https://www.sciencenews.org/article/new-fossils-denisovan-secrets-china
1•yannoninator•22m ago•0 comments

Vibe Coding is the new Internet Dating

https://joecmarshall.com/posts/vibecoding-is-the-new-internet-dating/
2•flancrest•31m ago•0 comments

There are so many bones everywhere: The whale graveyards that transform deep sea

https://www.bbc.com/future/article/20260225-the-whale-graveyards-that-transform-the-deep-sea
1•blondie9x•32m ago•0 comments

The Top Crisis of 2026

https://www.globalcrisisindex.com/the-strait-of-hormuz-the-top-crisis-of-2026.html
1•GlobalCrisisIdx•32m ago•0 comments

Raid on Bungeling Bay

https://en.wikipedia.org/wiki/Raid_on_Bungeling_Bay
3•Bluestein•38m ago•0 comments

Show HN: Sunk Cost – How long until a local LLM rig pays for itself?

https://sunkcost.ai/
3•rlindsey123•39m ago•0 comments

Building an Application Security Program That Works

https://collinsthoughts.substack.com/p/building-an-application-security
1•smurda•40m ago•0 comments

How Modern Technology Helped a Man Find a Cure to Save His Life

https://medium.com/swlh/doug-lindsay-the-man-who-cure-himself-12d40d3f643e
1•raynchad•40m ago•0 comments

StackBlitz is joining the Open Source Pledge

https://blog.stackblitz.com/posts/stackblitz-joins-osspledge/
3•noobplus•40m ago•0 comments

The UX Research Methodology subway map trap

https://www.reddit.com/r/UXDesign/comments/1wercj5/the_ux_research_methodology_subway_map_trap/
1•s4074433•44m ago•1 comments

Open the Frontier

https://twitter.com/jack/status/2099649359017046048
8•doppp•49m ago•1 comments

VoiceStudio – local open-source ElevenLabs alternative

https://github.com/debpalash/VoiceStudio
6•iamsyr•50m ago•0 comments

OpenDisplay – open-source Sidecar for Mac to iPhone/iPad

https://github.com/peetzweg/opendisplay
3•iamsyr•51m ago•0 comments

Nicobo robot companion: less than a tool, more than a toy

https://www.nippon.com/en/japan-topics/g02619/
1•anigbrowl•57m ago•0 comments

Mapping Medieval Merv

https://www.sidestone.com/books/mapping-medieval-merv
1•zeristor•57m ago•0 comments

Duplex Cue: Does a voice agent adapt while speaking?

https://besimple.ai/blogs/duplex-cue/
1•matt_d•1h ago•0 comments

Jellyfin v12.0 Release

https://github.com/jellyfin/jellyfin/releases/tag/v12.0
6•jdironman•1h ago•0 comments

Nvidia Unveils RTX Pro 5500 Blackwell Workstation Edition GPU

https://www.nvidia.com/en-gb/products/workstations/professional-desktop-gpus/rtx-pro-5500/
2•pelcg•1h ago•0 comments

Microsoft removes the COPILOT function from Excel

https://support.microsoft.com/en-us/excel/functions/copilot-function
4•fourfire•1h ago•1 comments

US military reveals it has weapons in space

https://www.ft.com/content/09d62f21-8697-4518-bca0-3699b4dd866c
14•JumpCrisscross•1h ago•3 comments

The Engineer/Manager Pendulum (2017)

https://charity.wtf/p/the-engineer-manager-pendulum
3•vismit2000•1h ago•0 comments