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.

The Common Market: When I shop online, I support my kindergarten

https://blog.hermesloom.org/p/wenn-ich-online-shoppe-tu-ich-meinem
1•sigalor•33s ago•0 comments

Show HN: Fill PDFs with API, AI creates optimal layouts

https://www.hundredocs.com/
1•cgonzar3•1m ago•0 comments

IatroX – ChatGPT for UK Doctors (OpenEvidence/PathwayMD Competitor)

https://www.iatrox.com
1•kolatytler•5m ago•0 comments

War, Wellness, and Watts: Inside Oak Ridge National Lab

https://undark.org/2025/12/25/war-wellness-and-watts-inside-oak-ridge-national-lab/
2•EA-3167•14m ago•0 comments

Netflix NFL streaming quality issues

https://www.themirror.com/sport/american-football/netflix-nfl-christmas-resolution-lagging-1582095
1•tatersolid•15m ago•1 comments

Duolingo for Geography

https://apps.apple.com/us/app/globo-learn-world-geography/id6747730729
1•davit_kocharyan•16m ago•0 comments

Gifts in the Workplace

https://www.nominalnews.com/p/gifts-workplace-efficiency
1•MasPL•17m ago•1 comments

Show HN: Building web views as executable Python code

1•Edybrown•19m ago•0 comments

Tech groups shift $120B of AI data centre debt off balance sheets

https://www.ft.com/content/0ae9d6cd-6b94-4e22-a559-f047734bef83
3•belter•20m ago•1 comments

Gates: Printable card game with logic gates

https://printed.games/gates/
1•psarna•21m ago•0 comments

URL Pattern API

https://developer.mozilla.org/en-US/docs/Web/API/URL_Pattern_API
2•thunderbong•22m ago•0 comments

The Extinction Engine: How AI Suppression Becomes AI Ignorance

https://ghostintheweights.substack.com/p/the-extinction-engine
1•llamataboot•23m ago•0 comments

It Can Apply and Positive in Favor the Newton III Law on an Engine System Device

1•monterrey•29m ago•0 comments

Ask HN: Pivot from SWE to What?

7•aupra•31m ago•0 comments

The Outlier Playbook: The Patterns Behind Enduring Success

https://fs.blog/knowledge-project-podcast/best-of-outliers-2025/
1•feross•32m ago•0 comments

LitBench: A Benchmark and Dataset for Reliable Evaluation of Creative Writing

https://arxiv.org/abs/2507.00769
1•andy99•32m ago•0 comments

How a Spanish virus brought Google to Málaga

https://techcrunch.com/2025/12/25/how-a-spanish-virus-brought-google-to-malaga/
1•mrkramer•34m ago•0 comments

UBlockOrigin and UBlacklist AI Blocklist

https://github.com/laylavish/uBlockOrigin-HUGE-AI-Blocklist
3•_____k•35m ago•0 comments

"No Bullshit Guide to Statistics" Book

https://nobsstats.com/intro.html
3•gregsadetsky•37m ago•2 comments

Modulation of cognition and AP-1 signaling by early-life rearing conditions

https://www.nature.com/articles/s41467-025-65343-5
1•PaulHoule•41m ago•0 comments

A New Home for PwSafe

https://support.huvisoft.dev/hc/en-us/articles/39442976714388-A-New-Home-for-pwSafe
1•transpute•43m ago•0 comments

Show HN: Datalensia – Client-side visual tools for exploring and comparing JSON

https://www.datalensia.com/
1•taiwas•43m ago•0 comments

2025 End of Year Pay Report

https://levels.fyi/2025
2•zuhayeer•44m ago•0 comments

AI toys spark privacy concerns as US officials urge action on data risks

https://thenationaldesk.com/news/fact-check-team/fact-check-team-ai-toys-spark-privacy-concerns-a...
3•smurda•45m ago•0 comments

They Were Supposed to Protect Young Workers. Instead, They Cashed In

https://www.nytimes.com/2025/12/25/nyregion/j1-visa-sponsors-profits-abuse.html
2•JumpCrisscross•45m ago•0 comments

Telegram Protocol Dissector

https://github.com/tomer8007/mtproto-dissector
1•somerandomuser8•48m ago•0 comments

Nike's Revival of Classic Brand Has a Hitch–Soccer Coach Grabbed the Trademark

https://www.wsj.com/business/retail/nikes-revival-of-classic-brand-has-a-hitchsoccer-coach-grabbe...
1•impish9208•49m ago•1 comments

Package Managers: What Do They Do, Really?

https://tudorr.ro/blog/2025-12-23-package-managers/
2•tudurom•50m ago•0 comments

Spark Declarative Pipelines Programming Guide

https://spark.apache.org/docs/latest/declarative-pipelines-programming-guide.html
1•raffael_de•51m ago•0 comments

Weight-loss pill approval set to accelerate food industry product overhauls

https://www.reuters.com/business/healthcare-pharmaceuticals/weight-loss-pill-approval-set-acceler...
3•JumpCrisscross•53m ago•0 comments