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.

new free website for people who are interested in free trading tools

1•Mohamed_Amineio•4m ago•1 comments

Made a Simple Link Extractor from HN Comments

https://hn-links.netlify.app/?id=49684393
1•rammy1234•10m ago•1 comments

The Malicious Use of Artificial Intelligence

https://arxiv.org/abs/1802.07228
1•rasengan0•28m ago•0 comments

Sandbox-API

https://hub.docker.com/r/credkellar/sandbox-api
1•credkellarboop•29m ago•0 comments

System 3 Thinking

1•rexossbear•30m ago•0 comments

The Biology of Claude's Tokenizer

https://tokencontributions.substack.com/p/on-the-biology-of-claudes-tokenizer
1•brimtown•31m ago•0 comments

Opportunistic AI detects colorectal cancer using routine, noncontrast CT

https://radiologybusiness.com/topics/artificial-intelligence/opportunistic-ai-detects-colorectal-...
2•pseudolus•33m ago•1 comments

Show HN: Exploring the intersection of prediction markets and social media

https://www.thevidmarket.com/
1•ryanpere2298•36m ago•0 comments

Jeff-a-Sketch

https://www.drjeffdaniels.com/jeffasketch
2•cebert•40m ago•1 comments

Fossil: Private Branches

https://fossil-scm.org/home/doc/trunk/www/private.wiki
2•thunderbong•45m ago•0 comments

The case against JPEG XL

https://giannirosato.com/blog/post/case-against-jxl/
31•contact9879•47m ago•18 comments

AI is not a normal technology

https://12gramsofcarbon.com/p/ai-is-not-a-normal-technology
2•theahura•50m ago•2 comments

Ask HN: What are some privacy/security tools you wish exist, but don't yet?

3•ToriTech•51m ago•1 comments

Godfather of AI welcomes pause in development (interview with Hinton)

https://www.abc.net.au/listen/programs/radionational-breakfast/godfather-of-ai-welcomes-pause-in-...
2•ggm•51m ago•1 comments

Aviloop: YouTube's Darkest Mystery [video]

https://www.youtube.com/watch?v=DU9JCFMJp8E
1•handfuloflight•1h ago•0 comments

Writing code by hand is not an option anymore

https://rogix.dev/writing-code-by-hand-is-not-an-option-anymore
1•rogix•1h ago•2 comments

Financial literacy and investment: empirical study from Palestine Stock Exchange

https://www.frontiersin.org/journals/behavioral-economics/articles/10.3389/frbhe.2025.1444022/full
1•andsoitis•1h ago•0 comments

The Final Battle for America's Democracy

https://newrepublic.com/article/215198/2027-new-congress-final-battle-american-democracy
3•HotGarbage•1h ago•1 comments

AI Robots – When will they be in our homes

https://spectrum.ieee.org/ai-robots
1•andsoitis•1h ago•2 comments

The librarian, the car keys, and AI

https://eternallyradicalidea.com/p/the-librarian-the-car-keys-and-ai
2•emmelaich•1h ago•2 comments

Worrying about bad AI code is worrying about the wrong problem

https://matthorn.io/posts/2026-09-13-bad-ai/
1•spectraldrift•1h ago•0 comments

Wikipedia's Oberranks Incident

https://boards.4chan.org/g/thread/109811298/wikipedias-oberranks-incident
1•mostcallmeyt•1h ago•2 comments

Lawyer cites fake witnesses in murder case and blames ChatGPT

https://www.reuters.com/legal/government/chatgpt-invented-fake-police-testimony-murder-appeal-new...
7•1vuio0pswjnm7•1h ago•1 comments

Writing a better reality: The case for optimistic sci-fi

https://honisoit.com/2022/03/writing-a-better-reality-the-case-for-optimistic-sci-fi/
2•andsoitis•1h ago•1 comments

Open-Source AI and Open Models Reading List

https://www.interconnects.ai/p/open-source-ai-reading-list
17•simonpure•1h ago•0 comments

AI bubble pops – Cory Doctorow

https://www.youtube.com/watch?v=LiDUg_uylyM
7•lifeisstillgood•1h ago•0 comments

What's in a name? Naming things in OpenStreetMap

https://blog.openstreetmap.org/2026/09/13/whats-in-a-name-naming-things-in-openstreetmap/
2•HotGarbage•1h ago•0 comments

Xi pushes 'Greater BRICS' economic ties to give bloc larger global role

https://www.reuters.com/business/aerospace-defense/xi-pushes-greater-brics-economic-ties-give-blo...
4•_djo_•1h ago•1 comments

Memorable moments in Chromecast's history (2024)

https://blog.google/products-and-platforms/devices/google-nest/chromecast-history/
1•gregsadetsky•1h ago•0 comments

Apple's Dimensional Drawings

https://developer.apple.com/accessories/dimensional-drawings/
2•herbertl•1h ago•0 comments