frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Small plugin for the PI agent – you might be able to say goodbye to your IDE

https://github.com/examplecode/pi-coderef
1•chengkaime•2m ago•0 comments

A Gallery of World Models

https://kleytoncosta.com/picks/wm-gallery
1•kleytondacosta•3m ago•0 comments

Pretraining a 2500 Elo chess Transformer

https://erikschluntz.com/software/2026/11/28/chess-transformer.html
1•eschluntz•5m ago•1 comments

Show HN: InkFolio – Handwritten notes app with AI for iPad and iPhone

https://apps.apple.com/us/app/inkfolio-handwritten-notes/id6830744573
2•rahul_cloudhike•11m ago•1 comments

Show HN: Genie – any model, at any text box

https://github.com/Lazarus-931/genie
1•alazarmb•13m ago•0 comments

Geely EV cars to more than double UK sales despite trade barrier

https://www.ft.com/content/85cca83f-8a68-40df-9372-6bc92e391562
3•Alien1Being•19m ago•1 comments

Understanding why 1 and 2 and 3 and 4 and 5 and = -1/12 (rather than ∞)

https://www.lesswrong.com/posts/F7DNBdoADqktjn2Hk/understanding-why-1-2-3-4-5-1-12-rather-than-in...
3•eatitraw•22m ago•0 comments

Lithography War: Never Interrupt the Enemy

https://lithowar.com/lithography-war/
2•walterbell•24m ago•0 comments

$0.10 per-user per-year: Nighttroop

https://maori.geek.nz/posts/2026/nighttroop/
2•grahar64•25m ago•0 comments

Kanon 2.5 Embedder, a legal embedding model

https://isaacus.com/blog/introducing-kanon-2-5-embedder
2•ubutler•25m ago•0 comments

Hinge users seek $1.7B class-action lawsuit

https://nypost.com/2026/11/28/us-news/1-75b-lawsuit-accuses-hinge-of-unfairly-banning-men-from-da...
4•circumcised•29m ago•1 comments

Polynomial Time Factoring Algorithm

https://geohot.github.io//blog/jekyll/update/2026/03/16/polynomial-time-factoring.html
1•verysmart•32m ago•0 comments

Building a game engine with coding agents

https://medium.com/@eigenbom/building-a-game-engine-with-coding-agents-4707aa26039d
1•eigenbom•34m ago•0 comments

FAFO: Don't Get Got

https://neonronin.sh/blog/stop-putting-model-keys-in-the-sandbox/
2•pandemicsyn•34m ago•0 comments

Text Compression Through the Looking Glass

https://utndatasystems.github.io/blog/2026/text-compression/
1•hazemdawahi•36m ago•0 comments

Realtime Waterbending: Cinematic 3D Fluid Simulation (UE5) [video]

https://www.youtube.com/shorts/T_pHILE075Q
1•shroom1•39m ago•0 comments

Why I Switched from Godot to Ebitengine (2025)

https://en.bioerrorlog.work/entry/devlog-4-godot-vs-ebitengine
1•indigodaddy•40m ago•0 comments

SocialSpec – The future of social media is websites

https://socialspec.org/
3•rd07•42m ago•0 comments

Egg production kills more animals than beef, pork, and lamb combined

https://ourworldindata.org/egg-production-kills-more-animals-than-beef-pork-and-lamb-combined
3•sohkamyung•42m ago•1 comments

Remote tech jobs that show which countries can apply

https://guruitjobs.com/
4•renan_dev_br•47m ago•0 comments

Reflections on training a 1B LLM on 100B tokens from scratch

https://www.marginalintelligence.com/posts/malama-model
1•jakobfrick•48m ago•1 comments

The Second Great Stock Market Rotation of 2026

https://ehwazwaves.substack.com/p/the-second-great-stock-rotation-of
1•_ehwaz•55m ago•0 comments

Swarm: Serverless, end-to-end encrypted messaging and calls

https://github.com/CWNE88/swarm
2•l8rlump•1h ago•0 comments

Show HN: Zero-retention pre-payment IBAN/VAT/LEI CSV checker and XDR-1 receipts

https://hcrb.in/shield
1•drjsarat•1h ago•0 comments

Lessons from Charlie Munger's Harvard School Commencement Speech

https://www.umr.io/blog/charlie-munger-harvard-speech
8•umerf•1h ago•0 comments

Show HN: Teaching agents your secure development practices over MCP

https://zeroquarry.com/research/skills-over-mcp/
1•eskibars•1h ago•0 comments

Sovdeflate – A Zero-Dependency C11 Deflate/ZLIB/Gzip Engine with SIMD

https://github.com/KanatoreIT/SOVDEFLATE
1•KanatoreIT•1h ago•0 comments

We Built an AI Coworker at YC That You Can Use for Free [video]

https://www.youtube.com/watch?v=2e0fJekCBxk
2•pss314•1h ago•1 comments

LVD – A framework that orders software architecture by rate of change

https://www.amazon.com/dp/B0HSNBHH5Y
3•neuralnair•1h ago•1 comments

Farewell Etaoin Shrdlu [video]

https://www.youtube.com/watch?v=1MGjFKs9bnU
2•nomagicbullet•1h ago•0 comments