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: Mango – A native, open-source MongoDB GUI. 7 MB, no Electron

https://mangoquery.com/en/
1•amlug•55s ago•0 comments

SmallestAI Integrated with N8n

https://n8n.io/integrations/smallest-ai/
1•sosumit001•1m ago•1 comments

Which European countries have the best salaries after taxes?

https://www.euronews.com/my-europe/2026/04/02/which-european-countries-have-the-best-salaries-aft...
1•andrewstetsenko•1m ago•0 comments

Do Not Leave Valuables in Env (or your AI Assistant may borrow them)

https://thomas.skowron.eu/blog/do-not-leave-valuables-in-env/
1•thomas-skowron•3m ago•0 comments

Daily roll of Bear Blog's top posts

https://bearroll.dev/
1•ZacnyLos•4m ago•0 comments

The Islands That Give Iran a Stranglehold on the Strait of Hormuz

https://www.wsj.com/world/middle-east/iran-strait-of-hormuz-control-islands-44b82207
1•JumpCrisscross•4m ago•0 comments

Claw Code Architecture Explained

1•suprab•5m ago•0 comments

AI search is atomizing our information, warns government digital designer

https://www.theregister.com/2026/04/02/ai_search_is_atomising_content/
1•Brajeshwar•5m ago•0 comments

Every dependency you add is a supply chain attack waiting to happen

https://benhoyt.com/writings/dependencies/
1•benhoyt•5m ago•0 comments

Show HN: I run a full software company solo with Claude Code agents

https://theonemancompany.com/
2•martinovigiani•9m ago•0 comments

ReactOS Shows Improved Stability and 64-Bit Support at Chemnitz Linux Days 2026

https://old.reddit.com/r/reactos/comments/1sa26yu/back_from_chemnitz_linux_days_2026/
2•jeditobe•12m ago•1 comments

Powering the agents: Workers AI now runs large models, starting with Kimi K2.5

https://blog.cloudflare.com/workers-ai-large-models/
1•Topfi•14m ago•0 comments

Show HN: Playcanvas and Gracia 4dgs

https://demo.gracia.ai/playcanvas.html
1•slimbuck•15m ago•0 comments

Claude induced psychosis is now live for all

https://technotes.substack.com/p/claude-induced-psychosis-is-now-live
1•thepoet•15m ago•0 comments

Show HN: I built a small app for FSI German Course

https://detawk.com/
1•syedmsawaid•17m ago•0 comments

The Moon belongs to all of us – not just countries that can afford to reach it

https://www.nature.com/articles/d41586-026-01051-4
1•salkahfi•17m ago•0 comments

Mavetis, a local-first Git security scanner

https://github.com/pimatis/mavetis
1•Queaxtra•18m ago•0 comments

Ask HN: Is it a good idea to create a new Linux distro in 2026?

1•roschdal•18m ago•1 comments

I handed over my dating life to AI. I don't think she'll see me again

https://www.theguardian.com/lifeandstyle/2026/feb/25/i-handed-over-my-dating-life-to-ai-i-dont-th...
2•beardyw•19m ago•0 comments

Inside Nepal's Fake Rescue Racket

https://kathmandupost.com/money/2026/03/27/inside-nepal-s-fake-rescue-racket
1•lode•20m ago•0 comments

Show HN: Clawd Pets – 90 animated SVG pets, pure CSS, no dependencies

https://github.com/abderrahimghazali/clawd-pet
1•ghazalidotdev•21m ago•0 comments

Markdown Ate the World

https://matduggan.com/markdown-ate-the-world/
1•salmon•23m ago•0 comments

'Tinder for Nazis' and the woman who hacked it

https://www.theguardian.com/news/audio/2026/apr/02/tinder-nazi-whitedate-hacked-martha-root-podcast
1•Brajeshwar•23m ago•0 comments

Show HN: UX Surveys – Run SUS, UEQ, UMUX-Lite, and NASA-TLX for Free

https://survey.helloproduct.ai
1•Dahfab•23m ago•0 comments

Every President Tries It. It Never Works

https://www.nytimes.com/2026/04/02/opinion/trump-manufacturing-industry-liberation-day.html
4•zacharyozer•24m ago•1 comments

Agent Responsibly

https://vercel.com/blog/agent-responsibly
2•bocytron•25m ago•0 comments

Humanity-as-a-Service

https://www.asomo.co/p/humanity-as-a-service
1•Gigamouse•26m ago•0 comments

Windows Clipboard Is Unprotected

https://sibexi.co/posts/windows-clipboard-unprotected/
1•ishqdehlvi•28m ago•0 comments

DiceBear is an avatar library for designers and developers

https://www.dicebear.com/
1•modinfo•31m ago•0 comments

Firestore Schema Visualizer

https://github.com/analyticalmonk/firestore-schema-visualizer
1•akashtndn•33m ago•0 comments