frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•8mo 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•8mo 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•8mo 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: Sara – Markdown-based requirements traceability tool written in Rust

https://github.com/cledouarec/sara
1•cledouarec•4m ago•0 comments

Show HN: Volume Shader – Free online 3D GPU benchmark and stress test tool

https://volumeshader.pro/
1•tomstig•5m ago•0 comments

Show HN: Fast-resume: a TUI to index and fuzzy search coding agent sessions

https://github.com/angristan/fast-resume
1•angristan•8m ago•0 comments

The Value of Things

https://journal.stuffwithstuff.com/2026/01/24/the-value-of-things/
2•vinhnx•14m ago•0 comments

Nvidia PersonaPlex: Natural Conversational AI with Any Role and Voice

https://research.nvidia.com/labs/adlr/personaplex/
2•wiradikusuma•16m ago•0 comments

Patterns in Konnakol

https://algorithmicpattern.org/patterns-in-konnakol/
3•rajangdavis•18m ago•0 comments

Show HN: Stack Photos – Capture the same place over time and create timelapses

https://stack.photos
2•NeroWolfe•24m ago•0 comments

Lnxqd Regulatory Status (2026 Update): What the Public Records Show

2•Kangaroo_•25m ago•0 comments

Rekko – Recover failed Stripe payments with automated SMS/email

https://rekko.io
3•AlexRST•29m ago•1 comments

Insurance and Climate Risk: From Coverage to Catalyst

https://insurtechamsterdam.com/public/blog/insurance-climate-risk
2•Venesha•33m ago•0 comments

From Strategy to Execution: Building an AI Technology Plan That Works

https://insurtechamsterdam.com/public/blog/building-technology-plan-for-insurers
2•Venesha•33m ago•0 comments

What Are Managing General Agents (MGAs)?

https://insurtechamsterdam.com/public/blog/MGA-Managing-General-Agents-Dutch-Insurance
1•Venesha•34m ago•0 comments

MyBB Docker image with runtime version selection

https://github.com/visualcookie/mybb
1•visualcookie•35m ago•0 comments

Show HN: I built a free macOS menu bar app to track Claude usage

https://www.claudeusagebar.com/
1•Artzain•37m ago•0 comments

A Year of Building Uncloud

https://psviderski.substack.com/p/a-year-of-building-uncloud
2•valyala•41m ago•0 comments

Show HN: Notes from building a startup, shared openly

https://www.chiefletter.com/
7•iamvdev•44m ago•0 comments

Responsibility and Authority

https://functional.computer/blog/responsibility-and-authority
1•mefengl•44m ago•1 comments

Replaced Clay.com with Claude Code Agent

https://github.com/chaitanyya/sales
4•chaitanyya•52m ago•1 comments

Agents.md as a Dark Signal

https://joshmock.com/post/2026-agents-md-as-a-dark-signal/
3•birdculture•54m ago•1 comments

The "Roof of the World," in eight simple lines

https://bigthink.com/strange-maps/roof-of-the-world-map/
2•koolhead17•1h ago•0 comments

In Defense of 'Mantissa'

https://geometrian.com/projects/blog/in-defense-of-mantissa.html
5•enz•1h ago•1 comments

Why is the S combinator an S?

https://blog.plover.com/2021/08/28/
4•fanf2•1h ago•0 comments

Nifty Gateway, One of the oldest NFT trading platform shuts down

https://www.coindesk.com/business/2026/01/24/one-of-the-oldest-nft-trading-platform-which-facilit...
4•thm•1h ago•0 comments

Accept_language 2.2 – RFC 7231/4647 compliant Accept-Language parsing for Ruby

https://github.com/cyril/accept_language.rb
3•cyrilllllll•1h ago•0 comments

Leader of the Snack: How Mars became a food behemoth

https://www.rte.ie/news/business/2026/0125/1554807-mars-bars-chocolate/
4•austinallegro•1h ago•0 comments

How would you approach long term data acquisition from real estate platforms?

2•ashi-sal•1h ago•0 comments

My Claude Code Psychosis – By Jasmine Sun

https://jasmi.news/p/claude-code
3•doppp•1h ago•0 comments

The Uncomfortable Truths About Immigration

https://alexanderkustov.substack.com/p/the-uncomfortable-truths-about-immigration
4•barry-cotter•1h ago•0 comments

Video contradicts Trump's claim man killed in Minneapolis was a 'gunman'

https://www.theguardian.com/us-news/2026/jan/24/minneapolis-shooting-ice
28•gizzlon•1h ago•0 comments

From –$20k to $400k in a year. My LLM options trading experiment

https://scriptedalchemy.medium.com/from-20k-to-400k-in-a-year-my-llm-options-trading-experiment-1...
3•gionn•1h ago•1 comments