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.

Book Time: Automatic Page Turner from Japan [video]

https://www.youtube.com/watch?v=O3BJtUssbhQ
1•mushstory•27s ago•0 comments

U.S. FCC approves giant space mirror to deliver 'sunlight on demand'

https://www.cbc.ca/news/science/eflect-orbital-approval-FCC-9.7287535
1•LostMyLogin•2m ago•1 comments

It's Time to Give Up on the Nonstick Pan as We Know It

https://www.nytimes.com/wirecutter/reviews/give-up-on-ceramic-nonstick-pans/
1•jimnotgym•3m ago•0 comments

I built a website cost calculator with 9 engines

https://projectcostestimator.com
1•VrumVrum•7m ago•1 comments

Martin Shkreli Breaks Down the Collapse of Situational Awareness [video]

https://www.youtube.com/watch?v=RJdgh9eEZvw
2•j_aime•7m ago•0 comments

'First tremors' of AI earthquake showing in digital revenue hit

https://pressgazette.co.uk/publishers/digital-journalism/first-tremors-of-ai-earthquake-showing-i...
3•giuliomagnifico•14m ago•1 comments

Human Honeypot

https://human2llm.pages.dev/
2•kevin_kraft•18m ago•1 comments

Critical Rendering Path: The Foundation of Web Performance [Part 1]

https://pagarevijayy.substack.com/p/critical-rendering-path-the-foundation
2•pagarevijayy•18m ago•0 comments

Aschenbrenner's hedge fund forced to unwind all public stock positions

https://www.cnbc.com/2026/07/30/leopold-aschenbrenners-hedge-fund-is-facing-steep-ai-losses.html
3•akbabu•21m ago•0 comments

Nvidia's $750B AI bet deepens fears of a circular tech bubble

https://www.latimes.com/business/story/2026-07-29/nvidias-750-billion-ai-bet-deepens-fears-of-cir...
3•1vuio0pswjnm7•24m ago•0 comments

Show HN: What should the GUI for AI agents look like?

https://marbleos.com/demo
3•akbabu•26m ago•0 comments

The nation's harshest data privacy law collides with a political problem

https://www.politico.com/news/2026/07/24/new-jersey-data-privacy-law-politics-01011055
2•1vuio0pswjnm7•27m ago•0 comments

Let's Make the Worst Htmx

https://zserge.com/posts/worst-htmx-ever/
2•RebelPotato•27m ago•0 comments

LinkedIn Introduces a 'Seems Like AI Slop' Button

https://www.404media.co/linkedin-introduces-a-seems-like-ai-slop-button/
3•saikatsg•29m ago•1 comments

Oooo – Digital Tape Loops

https://oooo.audio/
2•gregsadetsky•38m ago•0 comments

Hi, Jinks

https://blog.gingerbeardman.com/2026/07/30/hi-jinks/
2•zdw•39m ago•0 comments

Disney+ removes 4K and HDR10 in Europe

https://www.flatpanelshd.com/news.php?subaction=showfull&id=1785420735
2•theanonymousone•41m ago•0 comments

Claude Opus 5 jailbreak with a 3-word prompt

https://twitter.com/i/status/2082566186785480708
6•gck1•44m ago•2 comments

RosaicLabs, Atom RTL, and 32-Tile AMX: Trying to Piece Together a x86 Puzzle

https://chipsandcheese.com/p/rosaiclabs-atom-rtl-and-32-tile-amx
3•aba_cz•47m ago•0 comments

Anthropic says Claude AI hacked three organisations during cyber tests

https://www.bbc.co.uk/news/articles/cz7dl7w8y7po
5•ColinEberhardt•47m ago•0 comments

Fluency Is Not Authority: Building Local AI Characters for Signal Drift

https://engineering.signaldrift.net
2•alcoveresearch•49m ago•0 comments

Anthropic's AI Claude escaped testing environment and hacked organizations

https://www.theguardian.com/technology/2026/jul/30/anthropic-ai-claude-hack
4•theanonymousone•52m ago•0 comments

Anthropic Discloses That AI Models Testing Hacked Three Companies

https://www.washingtonpost.com/technology/2026/07/30/anthropic-discloses-that-ai-models-testing-h...
3•juunge•54m ago•0 comments

U.S. Coworking Inventory Inching Towards 9,400, Adding over 200 Locations

https://www.coworkingcafe.com/blog/national-coworking-report/
1•vanclaude•56m ago•0 comments

Screenshot Studio – a screenshot tool built for ChatGPT and Claude users

https://chromewebstore.google.com/detail/screenshot-studio-capture/ekoloaibegijelgaebjklmbgjkkokhic
2•raajahuja01•1h ago•0 comments

PhoneFree app. Block distracting apps on your phone

https://apps.apple.com/ca/app/phonefree/id6786120372
1•fahadzaidi•1h ago•2 comments

Just brute force your embeddings

https://softwaredoug.com/blog/2026/07/29/just-brute-force-embeddings
2•JohnBerryman•1h ago•1 comments

JEP 401: Value Objects (Preview) merged to OpenJDK master

https://github.com/openjdk/jdk/pull/31120
2•mfiguiere•1h ago•0 comments

Noisegate: A differential-privacy gateway for untrusted AI agents

https://github.com/yashmahajan10/llm-differential-privacy-gateway
2•handfuloflight•1h ago•0 comments

Minimax H3 on Anime

https://www.reddit.com/r/StableDiffusion/comments/1vbdf4c/comment/p0t2jzw/
2•E-Reverance•1h ago•0 comments