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.

UniFi Design Center

https://design.ui.com
1•ksec•11m ago•0 comments

Plentiful, high-paying jobs in the age of AI

https://www.noahpinion.blog/p/plentiful-high-paying-jobs-in-the-ff9
1•gmays•14m ago•0 comments

Simonw on Lenny's Podcast

https://www.youtube.com/watch?v=wc8FBhQtdsA
1•doppp•22m ago•0 comments

European Tech Map – European Tech Alternatives

https://europeantechmap.eu/
1•giuliomagnifico•23m ago•0 comments

WTFM – I found Anthropic's pub fix for AI policy comp, then built it myself

https://mpdc.dev/wtfm/
1•ParanoidRV•26m ago•0 comments

Sheets, terminal based spreadsheet tool

https://github.com/maaslalani/sheets
1•marcuskaz•28m ago•0 comments

Paint a Street

https://paintastreet.com/
3•internetguy•29m ago•0 comments

The humanoid company that ships

https://updates.prototypecap.com/p/the-humanoid-company-that-actually
1•JoiDegn•31m ago•0 comments

Mini-Presenter

https://github.com/mitsuhiko/mini-presenter
1•doppp•35m ago•0 comments

Xiaomi MiMo releases token plan

https://platform.xiaomimimo.com/#/docs/news/token-plan-release
1•gainsurier•40m ago•0 comments

EmDash is not what the WordPress community requires

https://yosisubo.com/notes/emdash-is-not-what-the-wordpress-community-requires/
1•alprado50•41m ago•0 comments

A simplified presentation of Einstein's unified field equations (1929)

https://www.gutenberg.org/cache/epub/78343/pg78343-images.html
1•petethomas•41m ago•0 comments

Cleer AI – Phone-Triggered Desktop Automation with Zero Setup

https://cleer.archonic.dev
1•ypadamat•45m ago•0 comments

Can servers use elicitation for HITL scenarios?

https://realarcherl.github.io/posts/mcp_security/
1•ArcherL•50m ago•0 comments

Dlarc: The Radio Geek's Doomscrolling Antidote

https://www.radioworld.com/news-and-business/news-makers/dlarc-the-radio-geeks-doomscrolling-anti...
1•petethomas•50m ago•0 comments

A Rave Review of Superpowers (For Claude Code)

https://emschwartz.me/a-rave-review-of-superpowers-for-claude-code/
2•emschwartz•1h ago•0 comments

It's Time to Take Down Your Smart Cameras [video]

https://www.youtube.com/watch?v=UMIwNiwQewQ
4•latchkey•1h ago•1 comments

Emacs ATX April 2026 meetup [video]

https://www.youtube.com/watch?v=465GSalM6oI
3•iLemming•1h ago•0 comments

The Whispering Earring

https://gwern.net/doc/fiction/science-fiction/2012-10-03-yvain-thewhisperingearring.html
1•shpat•1h ago•0 comments

Microsoft Readies $10B AI Investment Plan in Japan

https://www.bloomberg.com/news/articles/2026-04-03/microsoft-drafts-10-billion-investment-plan-in...
2•petethomas•1h ago•1 comments

Pool compute to run powerful open models

https://docs.anarchai.org/
1•gasull•1h ago•0 comments

Show HN: Minimal Brain Teaser Web Game (Handcrafted, No AI)

https://mehuleo.github.io/encircle/
2•arttaboi•1h ago•0 comments

Government of Verdis

https://verdisgov.org/
2•skogstokig•1h ago•0 comments

Microdrive Anatomy(2008)

https://www.hjreggel.net/hdtechdat/hd-anatomy.html
1•o4c•1h ago•0 comments

How are people treating/deploying on-device AI?

https://minimal-light-theme.yliu.me/
1•romerocruzsa•1h ago•1 comments

Hacker News MOTD shell script

https://gist.github.com/brandonpollack23/db78386cb84389772974edeb9e9e29bc
5•brandonpollack2•1h ago•1 comments

The Grammar Is Not Its Own Ground

https://jimiwen.substack.com/p/si-wu-zi-c36
1•jimiwen•1h ago•0 comments

When a reasoning LLM chooses, which comes first: thought or decision?

https://arxiv.org/abs/2604.01202
1•dotvignesh•1h ago•0 comments

What Would You See Changed in Haskell?

https://blog.haskell.org/what-would-you-see-changed-in-haskell/
2•shpat•1h ago•0 comments

How Squarespace Implemented Support for HTML Video/Audio Lazy Loading

https://engineering.squarespace.com/blog/2026/squarespace-and-web-standards-how-we-helped-bring-h...
1•beeandapenguin•1h ago•0 comments