frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Meta is reportedly laying off 3,600 employees

https://croudmid.site/meta-is-reportedly-laying-off-3600-employees/
1•randycupertino•1m ago•0 comments

AI SRE needs better observability, not bigger models

https://clickhouse.com/blog/ai-sre-observability-architecture
5•manveerc•4m ago•1 comments

Cognitive Atrophy as Vendor Lock-In: A 2050 Forecast

https://zenodo.org/records/18118429
1•Post_Flynn•5m ago•1 comments

Show HN: Generating self-enumerating pangrams with constraint solving on the Web

https://www.autogramgenerator.com/
1•nels_martin•6m ago•0 comments

Show HN: Automated video generation pipeline using ClaudeCode in 3 days

https://github.com/prajwal-y/video_explainer
1•prajwal299•6m ago•1 comments

Dell's version of the DGX Spark fixes pain points

https://www.jeffgeerling.com/blog/2025/dells-version-dgx-spark-fixes-pain-points
2•thomasjb•10m ago•0 comments

Couldn't Care Less. Cormac McCarthy in Conversation with David Krakauer [video]

https://www.youtube.com/watch?v=HrUy1Vn2KdI
1•mellosouls•10m ago•0 comments

39C3 – AI Agent, AI Spy [video]

https://www.youtube.com/watch?v=0ANECpNdt-4
1•namenumber•12m ago•0 comments

The Big YouTube Buyout: we just turned down millions of dollars [video]

https://www.youtube.com/watch?v=SWGBN1KvG6c
2•deepserket•15m ago•1 comments

Why fire agencies keep failing to put out blazes that later turn disastrous

https://www.latimes.com/california/story/2025-12-01/holdover-fires
1•PaulHoule•15m ago•0 comments

Pacc: A Better Unix Password Manager

https://jlucas.codeberg.page/posts/20260101-pacc.html
1•jlucas8•16m ago•0 comments

Show HN: Tools for Humans – Public Tracker for Workflows

https://turboops.io/platform/public-tracker
1•dm03514•16m ago•0 comments

Show HN: DailyNote – one-note-per-day app with local-first encryption

1•katspaugh•17m ago•0 comments

Men arrested, accused of cloning $14M of giftcards in monthslong scheme

https://www.kgns.tv/2025/12/30/3-men-arrested-accused-cloning-14-million-worth-gift-cards-monthsl...
1•randycupertino•18m ago•1 comments

Show HN: Browser extension to bulk transcribe YouTube channels

https://chromewebstore.google.com/detail/youtube-transcript-bulk-d/fgpagmikaghagifhoibbgpfcjibjdlfa
1•munjyong•22m ago•0 comments

Trump Has Renamed the Kennedy Center, but a Satirist Owns the URL

https://www.nytimes.com/2026/01/01/us/politics/trump-kennedy-center-website-toby-morton.html
5•mitchbob•23m ago•1 comments

SageBow: A Python Package That Deploys Autonomous Agents into Your Notebook

https://github.com/sagebowsystems/sagebow
1•sagebowsystem•25m ago•0 comments

From "Notebook" to "Production": Building an AI Marketing Engine

https://substack.com/inbox/post/183158020
1•meediax2025•26m ago•0 comments

SP80 – The Challenge

https://sp80.ch/the-challenge/
1•belter•27m ago•0 comments

Show HN: Gene – a Lisp-like language built around a generic "Gene" data type

https://github.com/gene-lang/gene
1•gcao•28m ago•0 comments

How Well Can You Hear Audio Quality? (2015)

https://www.npr.org/sections/therecord/2015/06/02/411473508/how-well-can-you-hear-audio-quality
1•embedding-shape•29m ago•0 comments

Public Domain Day 2026

https://copyrightlately.com/public-domain-2026/
1•gweinberg•30m ago•1 comments

Modern evidence for God from cosmology, astrochemistry, etc. (by a YC alum)

https://www.saintbeluga.org/follow-the-evidence-wherever-it-leads
1•michelangelodev•32m ago•1 comments

Bernie Sanders, Ron DeSantis speak out against data center boom. Bad sign for AI

https://www.cnbc.com/2026/01/01/ai-data-centers-bernie-sanders-ron-desantis-electricity-prices.html
2•1vuio0pswjnm7•33m ago•0 comments

Can AI really help us find love?

https://www.ft.com/content/fd044ddf-9a98-4282-ac0a-f7a9de060f58
1•thm•33m ago•0 comments

Introducing Radial: A Macro-Based Pie Menu for macOS

https://radial.appverge.net
1•Glubker•33m ago•1 comments

2025 Year in Review

https://joelgrus.com/2025/12/22/2025-year-in-review/
1•MurizS•35m ago•0 comments

Finland detains ship and its crew after critical undersea cable damaged

https://www.cnn.com/2025/12/31/europe/finland-estonia-undersea-cable-ship-detained-intl
20•wslh•36m ago•0 comments

Monads in C# (Part 2): Result

https://alexyorke.github.io/2025/09/13/monads-in-c-sharp-part-2-result/
2•polygot•37m ago•0 comments

My Favorite Self-Hosted Apps Launched in 2025

https://selfh.st/post/2025-favorite-new-apps/
2•yboulkaid•39m ago•0 comments