frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

AI Is Tipping the Scales Toward Hackers After Mythos Release

https://www.nbcnews.com/tech/security/anthropic-claude-mythos-ai-hackers-cybersecurity-vulnerabil...
1•thywis•35s ago•0 comments

Transaction-level provenance for AI art certificate/signed/license on every sale

https://arcvelvetos.web.app/verify?id=AwCCW6DLQgA4s5XVZ86X&type=sale
1•PCasinoAVOS•1m ago•0 comments

AI still can't figure out PowerPoint

https://www.perspectives.plus/p/ai-still-cant-figure-out-powerpoint
1•jukkan•1m ago•0 comments

LPM 1.0 – Video-Based Character Performance Model

https://large-performance-model.github.io/
1•LopRabbit•1m ago•0 comments

Anyone know how I can cancel this? I dont want it

https://old.reddit.com/r/wallstreetbets/comments/1siq4m2/anyone_know_how_i_can_cancel_this_i_dont...
2•simonpure•13m ago•0 comments

Tell HN: See the AI Doc

2•linsomniac•16m ago•1 comments

Why Aren't We Uv Yet?

https://aleyan.com/blog/2026-why-arent-we-uv-yet/
2•birdculture•21m ago•1 comments

Apple Silicon and Virtual Machines: Beating the 2 VM Limit

https://khronokernel.com/macos/2023/08/08/AS-VM.html
22•krackers•22m ago•0 comments

Heartbeat – open implementation of KAIROS, the always-on agent hiden in Claude C

https://github.com/uameer/heartbeat
1•usmame•22m ago•1 comments

The Polycorp Poly 1. New Zealand's school computer

https://www.classic-computers.org.nz/collection/poly1.htm
2•rbanffy•24m ago•0 comments

Ask HN: Why have we not stepped back on the moon again?

2•chirau•27m ago•2 comments

Ask HN: How did you specialize as a software engineer?

2•legerdemain•33m ago•2 comments

Is "Tokenmaxxing" a Flex?

https://www.businessinsider.com/tokenmaxxing-ai-token-leaderboards-debate-2026-4
2•pascal-maker•38m ago•2 comments

Git fixup is magic (and Magit is too)

https://arialdomartini.github.io/git-fixup
2•fanf2•38m ago•0 comments

Trump's World Liberty Financial borrows $75M using its own token as collateral

https://www.coindesk.com/markets/2026/04/09/trump-s-world-liberty-financial-borrows-usd75-million...
9•JohnTHaller•38m ago•0 comments

Show HN: Beta Testing needed for my package Trustcheck

https://github.com/Halfblood-Prince/trustcheck
1•halfblood1010•42m ago•1 comments

Ask HN: Agentic Permutation of Testing Paths In A System

4•davidajackson•46m ago•0 comments

Amazon Luna Will No Longer Allow Owners to Buy Games, Access Game Stores

https://www.ign.com/articles/amazon-luna-will-no-longer-allow-owners-to-buy-games-access-game-sto...
6•surgical_fire•47m ago•1 comments

Living Memory Inference

https://github.com/alash3al/loci
2•alash3al•50m ago•0 comments

YouTube Premium price increase to take effect in June

https://www.latimes.com/entertainment-arts/story/2026-04-10/youtube-premium-price-increase
2•obilgic•55m ago•0 comments

Open-source MCP server for LinkedIn

https://github.com/stickerdaniel/linkedin-mcp-server
2•arguflow•58m ago•0 comments

Hours Without Internet

https://bsky.app/profile/netblocks.org/post/3mj6hjlonjc2m
1•stupefy•59m ago•1 comments

Top% of users capture 61.5% of engagement in Hezbollah discourse on X

https://arxiv.org/abs/2603.26681
2•soufan•1h ago•0 comments

Several Mac mini and Mac Studio configs are now out of stock at Apple

https://9to5mac.com/2026/04/11/mac-mini-mac-studio-configs-completely-out-of-stock/
8•gnabgib•1h ago•0 comments

Prompt to App

https://prompttoapp.dev/
34•helloww•1h ago•4 comments

Get Users on Autopilot

https://www.usehotdrop.com/
1•Lucnyg•1h ago•0 comments

Producing The Perfect Token

https://blog.luminal.com/p/producing-the-perfect-token
1•jafioti•1h ago•0 comments

A general technique for automating NES games

https://tom7.org/mario/
2•azhenley•1h ago•0 comments

Show HN: PlaneFeed – scroll live flights like TikTok

https://planefeed.app/
1•mind1m•1h ago•0 comments

Canada's Liberal party adopts motion to restrict kids from social media

https://toronto.citynews.ca/2026/04/11/liberal-party-adopts-motion-to-restrict-kids-from-social-m...
2•EmbarrassedHelp•1h ago•0 comments