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.

Volkswagen CEO says 50,000 more job cuts may be needed to close competitive gap

https://www.cnbc.com/2026/07/13/volkswagen-ceo-50000-job-cuts-may-be-needed-to-close-competitive-...
1•root-parent•1m ago•0 comments

Revisiting Yliluoma's ordered dither algorithm

https://30fps.net/pages/revisiting-yliluoma-2/
1•ibobev•1m ago•0 comments

Apple Stock Hits Record Territory as Traders Sour on AI Spending

https://www.macrumors.com/2026/07/13/apple-stock-record-territory/
1•datakan•2m ago•0 comments

Oatmeal Spice

https://www.boristhebrave.com/2026/07/05/oatmeal-spice/
1•ibobev•2m ago•0 comments

Commodore 64 Basic Dungeon Crawler

https://retrogamecoders.com/c64-basic-dungeon-part7/
1•ibobev•2m ago•0 comments

Meccha Chameleon

https://store.steampowered.com/app/4704690/MECCHA_CHAMELEON/
1•doener•4m ago•0 comments

Latent Space as a New Medium

https://kk.org/thetechnium/latent-space-as-a-new-medium/
1•arbesman•5m ago•0 comments

crates.io: Development Update

https://blog.rust-lang.org/2026/07/13/crates-io-development-update/
2•dubi_steinkek•6m ago•0 comments

Evaluating the GPT-5.6 Family

https://www.braintrust.dev/blog/gpt56-decision-map
1•mooreds•7m ago•0 comments

I built a website to help artists book their tour

https://stevehofstetter.substack.com/p/i-built-a-website-to-help-artists
1•mooreds•8m ago•0 comments

Spain's mega EU debt proposal sets up showdown with northern European countries

https://www.politico.eu/article/spain-mega-eu-debt-proposal-sets-up-showdown-with-northern-europe...
1•mooreds•8m ago•0 comments

Drawings in untrained neural networks survive the training process

https://twitter.com/willdepue/status/2076581570782056523
1•LorenDB•8m ago•0 comments

Show HN: Build Rules for Claude Code, Cursor, and Codex

https://kastra.ai/
1•carlosjimenez1•9m ago•0 comments

Reading is the art of attention. What a mess we've made of that word

https://aworkinglibrary.com/writing/umyazu
2•speckx•10m ago•0 comments

AI as Search Engine and Printing Press Aid: Local Education Data Munging

https://middlesexgazette.org/news/vermonts-yardstick-moved/
1•mrkiouak•11m ago•1 comments

The American suburbs are better than you think

https://www.noahpinion.blog/p/the-american-suburbs-are-better-than
1•bilsbie•11m ago•0 comments

Vyto: A niche JavaScript-like language and UI Toolkit that compiles to C – fast

https://github.com/vytolang/vyto
1•emuthomi•11m ago•1 comments

Free Web-Based Logic Software

https://dailynous.com/2026/07/13/free-web-based-logic-software-guest-post/
1•jruohonen•12m ago•0 comments

China is dealing with its own manosphere

https://www.economist.com/china/2026/07/12/china-is-dealing-with-its-own-manosphere
1•andsoitis•13m ago•0 comments

Writing an Evasive .NET Shellcode Loader

https://slashsec.at/en/blog/writing-an-evasive-dotnet-shellcode-loader
1•slashcrypto•13m ago•0 comments

SVIEW – 100M Large-Scale Grid & SQLite-free Pivot on an 11yo laptop

1•SVIEW_Architect•13m ago•0 comments

A litmus test for the utility of AI features

https://beyondprobable.com/posts/litmus-test-ai-features.html
1•haritha-j•13m ago•0 comments

Show HN: Crowdmind – open-source tool to test ideas against AI personas

https://github.com/Brokenwatch24/crowdmind
1•jjaramillor•13m ago•0 comments

Discovery/characterization of antitumor gut microbiota from amphibians&reptiles

https://www.tandfonline.com/doi/full/10.1080/19490976.2025.2599562#abstract
1•bookofjoe•14m ago•0 comments

Show HN: Clawk – Give coding agents a disposable Linux VM, not your laptop

https://github.com/clawkwork/clawk
7•celrenheit•14m ago•0 comments

Show HN: Claudelines – Share, install, and sell Claude Code status lines

https://claudelines.com
1•rsproule•15m ago•0 comments

Bottom: Yet another cross-platform graphical process/system monitor

https://github.com/ClementTsang/bottom
1•thunderbong•15m ago•0 comments

Show HN: SearchCue

https://searchcue.com/
1•probst•15m ago•0 comments

Show HN: Honeyprompt, the load-balancing, multi-protocol LLM honeypot

https://github.com/alectrocute/honeyprompt
1•arm32•15m ago•1 comments

When You Didn't Get Picked

https://nik.art/when-you-didnt-get-picked/
1•herbertl•16m ago•0 comments