frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

An open source project for agentic dashboard creation

https://github.com/vahidkowsari/queryboard
1•kowsari•3m ago•0 comments

Your Marriage Has a Third

https://www.afterbabel.com/p/your-marriage-has-a-third
1•paulpauper•4m ago•0 comments

Frameserve – self-hosted digital photo frame served over the web (Go, Docker)

https://github.com/davidhfrankelcodes/frameserve
1•banktech48453•4m ago•1 comments

The most brilliant move in corporate history?

https://asymco.com/2026/03/10/the-most-brilliant-move-in-corporate-history/
1•MBCook•5m ago•0 comments

Stop Formatting Before the Desk Reject: A Proposal for Staged Submissions

https://blog.trialanderror.org/stop-formatting-before-the-desk-reject-a-proposal-for-staged-submi...
1•paulpauper•5m ago•0 comments

Dynamics–Knowledge Separation: A design principle for learning systems

https://aphorion.co/blog/dynamics-knowledge-separation/
1•nguthiru•6m ago•1 comments

Is Paris ready for the super-rich?

https://www.ft.com/content/43a8b5d7-c0df-4904-a2ce-361f46a3d1ce
1•bookofjoe•6m ago•1 comments

The Architecture of the Cage

https://srinathshrestha.xyz/blogs/the-architecture-of-the-cage
1•paulpauper•6m ago•0 comments

Gitzy is now on TestFlight A modern, native iOS Git client

https://testflight.apple.com/join/SB16NCfr
1•marc0janssen•7m ago•1 comments

A guy who is tortured is showing a Phil Collins concert

https://places.fleo.at/r/home?coords=-9900&doords=-2432
1•interbr•7m ago•0 comments

Mind the Failure Gap

https://erman.substack.com/p/mind-the-failure-gap
1•NomNew•8m ago•0 comments

How to get support from Claude support team

1•dduyanhhoang•9m ago•0 comments

Show HN: Crabby, open source web+API synthetic monitoring

https://github.com/chrissnell/crabby
1•chrissnell•12m ago•0 comments

AI can 'same-ify' human expression – can some brains resist its pull?

https://www.nature.com/articles/d41586-026-00781-9
1•latexr•14m ago•0 comments

Commit Story – Visualize any GitHub repo's history as an interactive story

https://www.commitstory.dev/
1•sabahattink•14m ago•1 comments

A Cider-Can 'Camera' Captured Eight Years in a Single Photograph

https://www.smithsonianmag.com/smart-news/someone-took-eight-year-photo-exposure-beer-can-180976552/
2•thunderbong•14m ago•0 comments

'Zip-up' EV charging channels arrive for terraced streets in UK pilot

https://www.bbc.co.uk/news/articles/cqlgwvyzq59o
1•thinkingemote•14m ago•0 comments

Why Is Ink Slow?

https://github.com/geoffmiller/ratatat/blob/main/docs/why-is-ink-slow.md
1•GeoffIsTheBest•14m ago•1 comments

K3Sharp – K3 Language Interpreter in C#

https://github.com/ERufian/ksharp
1•tosh•14m ago•0 comments

Firefox Bug Bounty Program Updates

https://attackanddefense.dev/2026/03/13/bug-bounty-program-updates-2026.html
1•holysoles•17m ago•1 comments

SuperTux 0.7.0

https://github.com/SuperTux/supertux/releases/tag/v0.7.0
2•pentagrama•19m ago•0 comments

Show HN: Godshell – Talking in Natural Language to Your Kernel via eBPF and LLMs

https://github.com/raulgooo/godshell
2•raulgooo•22m ago•0 comments

We are no longer training AI, AI is training us

https://www.nature.com/articles/d41586-026-00781-9https://www.nature.com/articles/d41586-026-00781-9
4•Muhammad523•23m ago•1 comments

An Australian man, using ChatGPT, made an mRNA vaccine to treat his dog's cancer

https://www.dawn.com/news/1982334
2•maxall4•24m ago•2 comments

Key Iranian telecoms network collapses

https://twitter.com/netblocks/status/2033199851232534653
1•defly•24m ago•0 comments

Why Cells Don't Grow Faster

https://nikomc.com/2026/03/15/growth-rate/
1•mailyk•25m ago•0 comments

Simplicity in the age of AI-assisted coding

https://gogogolems.substack.com/p/simplicity-in-the-age-of-ai-assisted
2•larve•29m ago•0 comments

A Microneedle-Based IgE Aptasensor for Detection of Food Allergy Sensitization

https://pubs.acs.org/doi/10.1021/acsmaterialslett.5c01137
2•PaulHoule•31m ago•0 comments

Ask HN: How do US startups handle payroll for Nigerian remote teams?

2•hey-osas•32m ago•0 comments

Agentic Experience Design

https://kurtiskemple.com/blog/agentic-experience-design/
1•mooreds•33m ago•0 comments