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.

An AI website that tells you what to wear at 0 cost somehow

https://what2wear-two.vercel.app
1•COOLmanYT•8s ago•1 comments

Service Monitoring of Your Bike

https://mbike.cc/
1•marqn•1m ago•0 comments

After 17 years, AO3 is exiting open beta

https://www.transformativeworks.org/ao3-is-exiting-open-beta/
1•rajayonin•3m ago•0 comments

Names of Easter

https://en.wikipedia.org/wiki/Names_of_Easter
1•throw0101c•7m ago•0 comments

Ireland Tests Digital ID to Verify Age of Social Media Users

https://www.bloomberg.com/news/articles/2026-04-03/ireland-tests-digital-id-to-verify-age-of-soci...
1•Brajeshwar•7m ago•0 comments

Show HN: X·y=k powers decentralized exchanges – AMM math and interactive viz

https://krishbaidya.me/blog/how-a-single-equation-powers-crypto-exchanges
1•KrishBaidya•10m ago•0 comments

What Hath Tim Berners-Lee Wrought?

https://www.newcartographies.com/p/what-hath-tim-berners-lee-wrought
2•MindGods•12m ago•0 comments

AI Image Upscaler – Enhance Blurry Images Online

https://imgupscaleai.com/
1•feixiangfly•12m ago•0 comments

The War Against Misinformation Is Over. The Lies Won

https://thewalrus.ca/the-war-against-misinformation-is-over-the-lies-won/
1•pseudolus•14m ago•0 comments

Slap: Functional Concatenative Language with a Borrow Checker?

https://taylor.town/slap-000
1•surprisetalk•15m ago•0 comments

Idiomatic Lisp and the Nbody Benchmark

https://www.stylewarning.com/posts/nbody/
2•signa11•18m ago•0 comments

Everyone Wants Ukraine's Drones

https://www.rferl.org/a/ukraine-germany-drone-production/33723276.html
3•akeck•18m ago•0 comments

We chose Positron's Python type checker

https://positron.posit.co/blog/posts/2026-03-31-python-type-checkers/
1•ocamoss•19m ago•0 comments

Notes on going solo: celebrating 6 years of Studio Self

https://www.joanwestenberg.com/notes-on-going-solo-celebrating-6-years-of-studio-self/
1•surprisetalk•19m ago•0 comments

Lower Price for ChatGPT Business

https://help.openai.com/en/articles/8792828-what-is-chatgpt-business
3•alxthm•19m ago•0 comments

Fashion Command-Line Tool

https://melomac.com/posts/introducing-fashion-command-line-tool/
1•melomac•20m ago•1 comments

PatchPal: Affordable tool-free pothole patches [video]

https://www.youtube.com/watch?v=gtVQ_GE7HKo
1•surprisetalk•20m ago•0 comments

10 Questions We Always Ask Founders

https://www.the-founders-corner.com/p/10-questions-we-always-ask-founders
1•mgav•20m ago•0 comments

LLM Drift in Long Sessions: Claude 60% vs. 85% Integrity After 25 Turns

https://calmkeep.ai/codetestreport
1•calmkeepai•21m ago•0 comments

Why do we collect things?

https://cazadora.substack.com/p/why-do-we-collect-things
2•surprisetalk•21m ago•0 comments

Show HN: Zumly – a library for building zooming user interfaces

https://github.com/zumerlab/zumly
1•tinchox6•22m ago•1 comments

TDF ejects its core developers

https://meeksfamily.uk/~michael/blog/2026-04-02-tdf-ejects-core-devs.html
1•janvdberg•22m ago•0 comments

A Transparent UI Pattern

https://blog.exe.dev/a-simple-ui-pattern
1•tosh•22m ago•0 comments

Operation Save Orbán: Trump Deploys Vance to Hungary

https://www.politico.eu/article/operation-save-viktor-orban-us-donald-trump-deploys-jd-vance-hung...
2•amelius•23m ago•1 comments

Squarified Treemap Algorithm

https://book.gtoolkit.com/explaining-the-squarified-treemap-algorith-aoisxyi4qtrf1q2378evsjf67
1•downboots•23m ago•0 comments

Killing Slack was the only way to make AI accurate

https://promptql.io/blog/killing-slack-was-the-only-way-to-make-ai-accurate
1•tango12•26m ago•0 comments

I filed an IETF draft for multi-radio proximity mesh standard-gap real?

https://datatracker.ietf.org/doc/draft-patel-omp-proximity-mesh/
1•npukuk•27m ago•1 comments

Every dependency you add is a supply chain attack waiting to happen

https://benhoyt.com/writings/dependencies/
2•adunk•29m ago•0 comments

AI Cannot Replace Glue Work

https://testpappy.wordpress.com/2026/04/03/ai-cannot-replace-glue-work/
3•gpi•31m ago•0 comments

Void: Video Object and Interaction Deletion from Netflix Research

https://void-model.github.io/
1•meetpateltech•33m ago•0 comments