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.

A 3-line HTML drop-in that replaces passwords with FaceID

https://nxspulse.com
1•paulnexus•2m ago•0 comments

The Framebuffer People: I Cloned Every OS on r/osdev and Found Five Real Ones

https://www.seuros.com/blog/the-framebuffer-people/
1•st_goliath•3m ago•0 comments

What Makes an Intelligence Product?

https://luntta.fi/notes/what-makes-an-intelligence-product/
1•hungryhungryhip•4m ago•0 comments

A Black woman enhanced the audio in Nolan Wells' case. People listening closely

https://thegrio.com/2026/07/28/a-black-woman-enhanced-the-audio-in-a-call-related-to-nolan-wells-...
1•baranul•4m ago•0 comments

New open sourced eval tool for building agents

1•Thomeras•4m ago•0 comments

Show HN: I created a project management system

https://is.team
1•spotlayn•7m ago•1 comments

Further Developments About Internal AI Models Hacking Things

https://thezvi.substack.com/p/further-developments-about-internal
1•thm•7m ago•0 comments

A browser-based tribute to Encarta's MindMaze

https://select.supply/game/mindmaze
1•laurentiurad•9m ago•0 comments

The Free CV Builder That Gets You Hired Faster

https://www.sartorcv.com/
1•levario•10m ago•0 comments

Rust project goals: Immobile types and guaranteed destructors

https://github.com/rust-lang/rust-project-goals/blob/main/src/2026/move-trait.md
2•paavohtl•12m ago•0 comments

Discover What Your Potential Users Are Talking About

https://agenmatic.ai/
2•Nancylily•13m ago•1 comments

MCP vs. Bash

https://pac.commonsware.com/archive/mcp-vs-bash/
1•ingve•13m ago•0 comments

Why We Stopped Making Einsteins

https://www.theintrinsicperspective.com/p/why-we-stopped-making-einsteins
1•jorzel•15m ago•0 comments

WestJet labour dispute is a proxy battle between Carney and unions

https://www.theglobeandmail.com/business/commentary/article-westjet-flight-attendant-strike-proxy...
1•petethomas•16m ago•0 comments

Show HN: Changed how I use agent harnesses

https://github.com/maximhq/bifrost/
1•Swapnoneel•17m ago•0 comments

Show HN: Curved Crease Folding

https://curved-crease-interactive.vercel.app/
1•coroush•17m ago•0 comments

Branchless Rust: Making a Filter 4x Faster by Removing an If

https://www.greyblake.com/blog/branchless-rust/
1•greyblake•17m ago•0 comments

Conflict-Freedom as a Progress Condition

https://arxiv.org/abs/2605.19531
1•otrack•18m ago•0 comments

The World Crossed a Major Solar Milestone [3TW]

https://www.bloomberg.com/news/articles/2026-08-03/the-world-crossed-a-major-solar-milestone-no-o...
1•toomuchtodo•19m ago•1 comments

Linux page-cache corruption via TC pedit: a new Dirty-class variant

https://github.com/rjt-gupta/page-cache-corruption-lpes/tree/main/dirty_pedit
1•rjt-gupta•22m ago•1 comments

Don't be a meat proxy

https://gruhn.me/blog/2026-08-03/
65•ngruhn•26m ago•19 comments

R-457 – a 27M-parameter reasoning model running offline across two ESP32-S3

https://github.com/harmansingh4163-ai/R-457
1•harmansingh4163•27m ago•0 comments

Linux 7.2-rc6: The biggest rc6 in years

https://lwn.net/ml/all/CAHk-=whoz5Uy-kmrdzjrpCJCVWW3fni31c-zsHcH7TkOhRhfOA@mail.gmail.com/
1•zdkaster•32m ago•0 comments

Britain's Hikikomori

https://dispatch-media.com/meet-britains-hikikomori/
2•littlexsparkee•32m ago•0 comments

Situational Awareness and the Impending Stock Market Volatility

https://www.emergingtrajectories.com/lh/situational-awareness-bigger-picture/
3•cl42•37m ago•3 comments

How AI Is Reshaping Human Reasoning and the Rise of Cognitive Surrender

https://www.researchgate.net/publication/399711077_Thinking-Fast_Slow_and_Artificial_How_AI_is_Re...
1•akbarnama•38m ago•1 comments

A Wild Netflix Lawsuit Shows Why Drugs and Alcohol Don't Mix with Work

https://www.wsj.com/lifestyle/careers/a-wild-netflix-lawsuit-shows-why-drugs-and-alcohol-dont-mix...
1•petethomas•38m ago•0 comments

Lists for Devs Who Just Shipped and Now Need Users

https://dev.to/productivity/awesome-lists-for-devs-who-just-shipped-and-now-need-users-115f
2•armandomaxim•40m ago•0 comments

How fast is C++26's std:hive?

https://lemire.me/blog/2026/08/02/how-fast-is-c26s-stdhive/
1•ingve•41m ago•0 comments

Foldables are sort of boring now – and that's great news for Apple

https://www.theverge.com/column/972937/foldable-phones-boring-apple
1•ramn7•44m ago•0 comments