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: Matchmaking where agents talk with agents to find compatible matches

https://github.com/skorotkiewicz/jupiter
1•modinfo•14s ago•0 comments

OpenAI Executive Who Opposed 'Adult Mode' Fired for Sexual Discrimination

https://www.wsj.com/tech/ai/openai-executive-who-opposed-adult-mode-fired-for-sexual-discriminati...
1•erehweb•1m ago•0 comments

The Security Checklist for Vibe Coders

https://asanchez.dev/blog/the-security-checklist-for-vibe-coders/
1•asanchezdev•4m ago•1 comments

FTX: Where Did the Money Go?

https://drive.google.com/file/d/1e2v-rcUqSFy4VI1MlWTLkJe-IeaFYBnl/edit
1•tosh•5m ago•0 comments

Show HN: Ledger – Yes, another blogging platform (but you own the database)

https://www.franzkafka.xyz/
1•mekod•5m ago•1 comments

A future without physics papers?

https://josephtoobysmith.com/cs/2026/02/11/Future-without-physics-papers.html
1•leanexplorer•8m ago•0 comments

Elegant Transducer Pipelines

https://old.reddit.com/r/Clojure/comments/1r179s1/the_transducer_that_ate_our_heap/
1•todsacerdoti•9m ago•0 comments

Flowspark

https://flowspark.net
1•ellebelle•11m ago•1 comments

Elegant Transducer Pipelines

https://gist.github.com/NicolasLambert/c3e51cb0b5314f2110161f85be24b4c7
1•todsacerdoti•11m ago•0 comments

I built Fluxer, a Discord-like chat app

https://blog.fluxer.app/how-i-built-fluxer-a-discord-like-chat-app/
3•todsacerdoti•21m ago•0 comments

Archive.today is directing a DDoS attack against my blog

https://gyrovague.com/
3•doener•25m ago•0 comments

Metabolic Acceleration and the Evolution (2016)

https://pubmed.ncbi.nlm.nih.gov/27144364/
1•stared•26m ago•0 comments

Transfer learning and Transformer models (ML Tech Talks) [video]

https://www.youtube.com/watch?v=LE3NfEULV6k
1•onurkanbkrc•26m ago•0 comments

Archive.today: Operator uses users for DDoS attack

https://www.heise.de/en/news/Archive-today-Operator-uses-users-for-DDoS-attack-11171455.html
3•doener•27m ago•0 comments

Three largest Dutch banks seek European alternatives to U.S. technology

https://nltimes.nl/2026/02/10/rabobank-ing-abn-amro-seek-european-alternatives-us-technology
2•belter•28m ago•0 comments

Accelerationism: A fringe philosophy predicted the future we live in (2017)

https://www.theguardian.com/world/2017/may/11/accelerationism-how-a-fringe-philosophy-predicted-t...
1•edward•28m ago•0 comments

Show HN: 15% of Forbes 30 under 30 winners did fraud

https://30u30.rip
1•yevbar•29m ago•0 comments

European nations gear up to ban social media for children

https://www.dw.com/en/european-nations-mull-social-media-ban-for-children-instagram-tiktok-anxiet...
3•belter•29m ago•1 comments

The AI Vampire

https://steve-yegge.medium.com/the-ai-vampire-eda6e4f07163
2•SilverElfin•29m ago•0 comments

SQL /* comments */ can be nested

https://modern-sql.com/caniuse/comments
1•MarkusWinand•30m ago•0 comments

Show HN: Reddit Scout Pro [Chrome-extension]

https://plugmonkey.xyz/product/reddit-scout-pro/
1•yazeedaloyoun•32m ago•1 comments

Create Caricature of Me and My Job – Viral AI Trend Free

https://caricaturetrend.com/
1•lizbo•32m ago•0 comments

Epstein archive taxonomy

https://jmail.world/taxonomy
2•samuel246•35m ago•0 comments

Show HN: Qrono – A "final" date/time library for JavaScript

https://qronojs.dev/
1•urin•35m ago•0 comments

RooDB: Now faster than MySQL (acc. to sysbench 1M rows)

https://github.com/jgarzik/roodb/releases/tag/v0.7.0
1•jgarzik•35m ago•0 comments

We are all going to regret Kalshi and Polymarket

https://davekarpf.substack.com/p/we-are-all-going-to-regret-kalshi
2•thenaturalist•36m ago•0 comments

Immediate 10 day TFR issued in El Paso due to "special security reasons"

https://old.reddit.com/r/ADSB/comments/1r1pqnp/10_day_tfr_issued_in_el_paso_due_to_special/
2•c420•37m ago•0 comments

The Problem with LLMs

https://www.deobald.ca/essays/2026-02-10-the-problem-with-llms/
2•deobald•40m ago•0 comments

FortiGate Symlink Persistence Method

https://pgj11.com/posts/FortiGate-Symlink-Attack/
1•Yippee-Ki-Yay•43m ago•0 comments

The Calorie Counter (2022)

https://www.science.org/content/article/scientist-busts-myths-about-how-humans-burn-calories-and-why
1•XzetaU8•46m ago•0 comments