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: Armour – A secure stdio MCP proxy, written in Go

https://github.com/fuushyn/armour
1•devel12•27s ago•0 comments

Much of the World Facing 'Water Bankruptcy,' U.N. Report Warns

https://e360.yale.edu/digest/water-bankruptcy-report
2•speckx•27s ago•0 comments

Lindsey Vonn's new titanium knee helped her become Olympic-bound once again

https://www.nytimes.com/athletic/6948010/2026/01/19/lindsey-vonn-olympics-skiing-knee-replacement/
1•ChrisArchitect•1m ago•0 comments

Livelihood Traps

https://kovasap.github.io/docs/society/livelihood-traps/
1•a-curious-crow•2m ago•0 comments

Reliable Signals of Honest Intent

https://zanlib.dev/blog/reliable-signals-of-honest-intent/
1•todsacerdoti•2m ago•0 comments

IPv6 is not insecure because it lacks a NAT

https://www.johnmaguire.me/blog/ipv6-is-not-insecure-because-it-lacks-nat/
1•johnmaguire•2m ago•0 comments

Without benchmarking LLMs, you're likely overpaying 5-10x

https://karllorey.com/posts/without-benchmarking-llms-youre-overpaying
1•lorey•3m ago•0 comments

Moving Overseas via Educational Path

https://relocateme.substack.com/p/study-to-get-a-job-abroad-relocation
1•andrewstetsenko•4m ago•0 comments

Show HN: Coi – A language that compiles to WASM, beats React/Vue

1•io_eric•4m ago•0 comments

Stewart Brand – Maintenance: Of Everything, Part One

https://press.stripe.com/maintenance-part-one
1•mitchbob•5m ago•0 comments

Hotnews – Daily hottest news aggregator

https://news.lucianmarin.com/
3•lcnmrn•5m ago•0 comments

Free webinar 1/29: PostgreSQL 18 performance, indexing, & replication features

https://us02web.zoom.us/webinar/register/WN_0235AXQERaybzWCKYHaovQ
2•pgedge_postgres•6m ago•0 comments

The Old World Order Is Dead

https://musgrave.substack.com/p/the-old-world-order-is-dead
2•coloneltcb•7m ago•1 comments

SEO → RAO → Access = Lead

https://medium.com/@antonbm/seo-rao-access-lead-2429234c15eb
1•antonmb•8m ago•0 comments

South Carolina Measles Cases Push US Toward Losing Health Title

https://www.bloomberg.com/news/articles/2026-01-20/south-carolina-measles-cases-push-us-toward-lo...
2•helsinkiandrew•9m ago•0 comments

Ensemble Listening Model

https://www.ensemblelisteningmodel.com/
1•durron•9m ago•0 comments

A daily practice for writing better taglines

https://www.instagram.com/accounts/login/?next=https%3A%2F%2Fwww.instagram.com%2Ftaglinesandcopy&...
1•leeclowtaughtme•10m ago•1 comments

Show HN: A Lightweight Event Sourcing Starter for Next.js and GenesisDB

https://github.com/genesisdb-io/genesisdb-io-nextjs-es-starter
4•patriceckhart•12m ago•0 comments

Build Broad, Refine Later

https://opuslabs.substack.com/p/build-broad-refine-later
1•opuslabs•12m ago•0 comments

Training Your Own LLM on a MacBook Pro

https://opuslabs.substack.com/p/training-your-own-llm-on-a-macbook
2•opuslabs•13m ago•0 comments

Llms.txt didn't boost AI traffic for 10 sites; growth was coincidental

https://searchengineland.com/does-llms-txt-matter-467740
2•speckx•13m ago•0 comments

Show HN: Afelyon – AI agent that turns Jira tickets into GitHub PRs

https://afelyon.com/
1•AbduNebu•16m ago•0 comments

Turbopack: Building Faster by Building Less

https://nextjs.org/blog/turbopack-incremental-computation
2•feross•17m ago•0 comments

Show HN: OpsCompanion – Live production context for teams and agents

https://opscompanion.ai/
1•kennethops•17m ago•0 comments

EU-Inc: An API-first corporate structure as alternative to Delaware C-Corps

https://www.eu-inc.org/
1•matesz•18m ago•1 comments

The fix for a segfault that never shipped

https://www.recall.ai/blog/the-fix-for-a-segfault-that-never-shipped
4•davidgu•18m ago•0 comments

Check this futuristic Architecture for ecommerce: composable commerce

https://bagisto.com/en/composable-commerce/
1•anikeshkumar•20m ago•0 comments

Krazam: Rare Data Hunters [video]

https://www.youtube.com/watch?v=IU4ByUbDKNc
3•Topfi•22m ago•0 comments

Cow Tools

https://en.wikipedia.org/wiki/Cow_tools
2•pleurotus•22m ago•0 comments

Show HN: Quadrastack – All-in-one CLI for mocking and testing APIs

https://quadrastack.com
2•yuegui•23m ago•0 comments