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.

How to Cut an Infinitely Large Pancake into as Many Pieces as Possible

https://www.nytimes.com/2026/01/20/science/infinite-pancake-math-puzzle.html
1•bookofjoe•52s ago•1 comments

Sync/Backup/Install AI Agent configs across machines

https://sync-conf.dev/
1•zanreal•5m ago•1 comments

Wall Street Grapples with New Risk: A European Buyers' Strike

https://www.bloomberg.com/news/articles/2026-01-24/wall-street-grapples-with-new-risk-a-european-...
2•saubeidl•8m ago•1 comments

FR#150 – On ICE, Verification, and Presence as Harm

https://connectedplaces.online/reports/fr150-on-ice-verification-and-presence-as-harm/
1•colinprince•9m ago•0 comments

WA state is turning your 3D printer into a cop

https://blog.adafruit.com/2026/01/25/washingtons-3d-printing-bills-are-bad-for-stem-bad-for-busin...
1•ptorrone•9m ago•0 comments

Software 3.0 is the era of Thinkers

https://twitter.com/theOpusLABS/status/2015465349206941711
1•opuslabs•10m ago•0 comments

Monopsony, Markdown, and Minimum Wages [pdf]

https://eml.berkeley.edu/~schoefer/schoefer_files/FLS_MMM.pdf
1•jandrewrogers•11m ago•0 comments

Show HN: Graft – binary file patcher in Rust

https://github.com/sam-mfb/graft
1•sam256•11m ago•0 comments

You Are an Agent – Try Being a Human LLM

https://youareanagent.app/
1•robkop•16m ago•1 comments

Show HN: We built a hidden micro-bearing system inside a 2mm ring

2•spinity•16m ago•1 comments

Nano agent: a minimalistic Python library for building AI agents using DAGs

https://github.com/NTT123/nano-agent
2•xcodevn•18m ago•1 comments

Show HN: RealXV6 – a faithful Unix V6 kernel port to 8086 real mode

https://github.com/FounderSG/RealXV6
1•FounderSG•20m ago•0 comments

Ask HN: What's the best place to look at snow webcams?

1•bastawhiz•21m ago•2 comments

Cleaner air is (inadvertently) harming the Great Barrier Reef

https://phys.org/news/2026-01-cleaner-air-inadvertently-great-barrier.html
1•Brajeshwar•24m ago•0 comments

Ancient Spanish trees reveal Mediterranean storms are intensifying

https://phys.org/news/2026-01-ancient-spanish-trees-reveal-mediterranean.html
1•Brajeshwar•24m ago•0 comments

What happens when you train an LLM only on limited historical data

https://www.popsci.com/technology/this-ai-thinks-its-the-1800s/
1•Brajeshwar•24m ago•0 comments

Hey guys, check out my idea

1•harinand•25m ago•6 comments

XSS –> RCE in Screeps, a programming game on Steam

https://outsidetheasylum.blog/screeps/
2•Tiberium•26m ago•1 comments

Geo Is Not the Next Generation of SEO

https://valarmorghulis.io/view/202601-geo-vs-seo/
1•socrateslee•27m ago•1 comments

Human Approval as a Service

1•mf_taria•28m ago•0 comments

Software patches in NixOS for fun and productivity

https://log.pfad.fr/2026/software-patching-in-nixos/
4•todsacerdoti•30m ago•0 comments

The First Full-Scale Cyber War: 4 Years of Lessons

https://techtrenches.dev/p/the-first-full-scale-cyber-war-4
1•bryanrasmussen•31m ago•0 comments

Heart rhythm problems detected four times more often with smartwatches

https://nltimes.nl/2026/01/22/heart-rhythm-problems-detected-four-times-often-smartwatches
1•giuliomagnifico•32m ago•1 comments

More than a quarter of Britons say they fear losing jobs to AI in next 5 years

https://www.theguardian.com/business/2026/jan/25/more-than-quarter-britons-fear-losing-jobs-ai-ne...
3•chrisjj•33m ago•0 comments

Jack Kerouac on the Steve Allen Show with Steve Allen 1959 [video]

https://www.youtube.com/watch?v=3LLpNKo09Xk
1•aabiji•34m ago•0 comments

Kingdoms of Water: The Mekong River, empire, and the limits of human ingenuity

https://worldhistory.substack.com/p/kingdoms-of-water
1•crescit_eundo•36m ago•0 comments

Ice cream is one of the healthiest foods in existence

https://twitter.com/Outdoctrination/status/2015449347920396347
4•bilsbie•37m ago•0 comments

Inside Apple's AI Shake-Up and Its Plans for Two New Versions of Siri

https://www.bloomberg.com/news/newsletters/2026-01-25/inside-apple-s-ai-shake-up-ai-safari-and-pl...
2•thm•37m ago•0 comments

Good Taste

https://emsh.cat/good-taste/
1•embedding-shape•38m ago•2 comments

AMD Releases MLIR-AIE 1.2 Compiler Toolchain for Targeting Ryzen AI NPUs

https://www.phoronix.com/news/AMD-MLIR-AIE-1.2
2•pella•39m ago•0 comments