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.

Show HN: Fitdle – A Curve Fitting Contest

https://fitdle.yonatanbuilds.com/
1•yonatan365•1m ago•0 comments

Accessing Hardware in Rust

https://ferrous-systems.com/blog/hardware-access-rust/
1•fanf2•2m ago•0 comments

Official Dashtera Launch

https://dashtera.com/news/official-dashtera-launch/
1•abhimattoria•4m ago•0 comments

How to identify your Apple keyboard layout by country or region

https://support.apple.com/en-us/102743
1•tosh•4m ago•0 comments

Ask HN: What are some science or tech facts you know?

1•darshi7331•7m ago•1 comments

Show HN: Swik – catalog of asset-specific sentiment inversions for financial NLP

1•multidude•9m ago•0 comments

The new best free project management tool

https://mytracker.today
1•rakanalalami•11m ago•0 comments

AMD GPU-Initiated I/O

https://thegeeko.me/blog/nvme-amdgpu-p2pdma/
1•hatgfx•14m ago•0 comments

I rebuilt Claude Desktop in 10 days. Here's why

https://raulriera.medium.com/i-rebuilt-claude-desktop-in-10-days-heres-why-2efb47133da9
1•ingve•17m ago•0 comments

Been using this Tourist eSIM while traveling, super cheap unlimited data

https://touristesim.net
1•globalnomader•17m ago•0 comments

OpenClaw is just cron, Markdown and a chat bot and that's why it matters

https://twitter.com/efexen/status/2034352992233672945
2•efexen•18m ago•1 comments

Show HN: Get a quick skincare analysis by uploading a photo

https://howolddoyoulook.com/skincare
1•beast200•27m ago•0 comments

Show HN: EasyShot – macOS screenshot thumbnails that don't disappear after 5s

1•amlug•28m ago•0 comments

AI Hairstyle Changer

https://hairstyleaichanger.com/
1•Fsen•34m ago•0 comments

Why Whisper Notes for Mac Left the App Store

https://whispernotes.app/blog/why-whisper-notes-left-mac-app-store
1•mazzystar•36m ago•1 comments

"I hope you don't use Generative AI"

https://rmv.fyi/notes/i-hope-you-don-t-use-generative-ai
2•garblegarble•37m ago•1 comments

The AI Morning Show: Automating German Humor

https://portfolio.bildsignal.de/p_gagflatrate/
2•pahn•38m ago•2 comments

Rippling AI

https://www.rippling.com/blog/introducing-rippling-ai
1•tosh•41m ago•0 comments

The Five Companies You Can Build in 2026

https://www.dylancollins.com/p/the-five-companies-you-can-build
1•dylancollins•42m ago•0 comments

AI Council: run mupliple LLMs on your question, get consolidated opinion

https://github.com/yanbrod/council
1•ianbrode•43m ago•0 comments

TBM 406: Seeing Everything, Understanding Nothing (The Context Trap)

https://cutlefish.substack.com/p/tbm-406-seeing-everything-understanding
1•kiyanwang•47m ago•0 comments

Gea: A Compile-Time Reactive UI Framework That's Just JavaScript

https://github.com/dashersw/gea
1•dokdev•49m ago•0 comments

The Reason Most People Are Terrible Communicators (and How to Fix It)

https://alifeengineered.substack.com/p/the-reason-most-people-are-terrible
1•kiyanwang•49m ago•0 comments

Bombadil: Property-based testing for web UIs by Antithesis

https://github.com/antithesishq/bombadil
1•Klaster_1•50m ago•0 comments

Management in the Age of AI – Stay SaaSy

https://staysaasy.com/management/2026/03/12/ai-management.html
1•kiyanwang•52m ago•0 comments

'Alright mate?': Amazon pins UK hopes on AI upgrade of Alexa

https://www.theguardian.com/technology/2026/mar/19/amazon-uk-ai-upgrade-alexa-voice-assistant-dev...
2•chrisjj•54m ago•0 comments

Wikigacha – Collect cards from articles on Wikipedia and use them in battle

https://wikigacha.com
1•helloplanets•55m ago•0 comments

Taste at scale. Why the hardest part of building products stayed human

https://designexplained.substack.com/p/taste-at-scale
1•kaizenb•58m ago•0 comments

Context Engineering for Coding Agents

https://martinfowler.com/articles/exploring-gen-ai/context-engineering-coding-agents.html
2•BerislavLopac•59m ago•0 comments

Ask HN: Have you cancelled any software subscriptions because AI replaced them?

3•maxim_bg•1h ago•1 comments