frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

AI, Vikings and Magic Swords

https://yadin.com/notes/swords/
1•dryadin•1m ago•0 comments

Asahi Linux Progress Linux 7.0

https://asahilinux.org/2026/04/progress-report-7-0/
1•elisaado•3m ago•0 comments

Vacant House Shark: A B-movie created with AI featuring sharks and kung fu [video]

https://www.youtube.com/watch?v=LD4UNHAIQcs
1•nogajun•6m ago•0 comments

Chornobyl: 40 years after disaster, nuclear site still at risk

https://www.theguardian.com/news/ng-interactive/2026/apr/25/chornobyl-power-plant-at-risk-amid-ru...
1•Anon84•9m ago•0 comments

Show HN: Nice TUI for Go Pprof

https://github.com/owenrumney/lazypprof
1•rumno0•10m ago•0 comments

A List of Post-Mortems

https://github.com/danluu/post-mortems
1•carlos-menezes•10m ago•0 comments

Mystery Around Venezuelan Cyberattack Deepens, with New Highly Destructive Wiper

https://www.zetter-zeroday.com/hwiper-targeting-venezuelas-state-oil-company-discovered/
1•campuscodi•10m ago•0 comments

Show HN: no look

https://www.hyper-frame.art/console
1•keepamovin•11m ago•0 comments

Sebastian Sawe breaks two-hour mark in marathon world record

https://www.bbc.co.uk/sport/athletics/live/cjd9xpmnvj3t
2•beejiu•14m ago•0 comments

Ask HN: When did Spotify become YouTube/TikTok?

2•binarypixel•19m ago•1 comments

The Paradox of Karl Popper

https://www.scientificamerican.com/blog/cross-check/the-paradox-of-karl-popper/
1•baxtr•21m ago•0 comments

I factored the number RSA1024-1 using my home-built QPU stack

https://twitter.com/veorq/status/2048320115075137864
1•keepamovin•21m ago•0 comments

Craving work-life balance is a red flag, says Fortune 500 Europe CEO

https://fortune.com/2026/04/22/work-life-balance-bupa-fortune-500-ceo-barack-obama-work-weekend/
1•thisislife2•23m ago•0 comments

Car Dependency in Urban Accessibility

https://arxiv.org/abs/2604.01019
1•Anon84•27m ago•0 comments

What Made Lisp Different (2002)

https://paulgraham.com/diff.html
2•tosh•33m ago•0 comments

Magnet with near-zero external field could reshape future electronics

https://phys.org/news/2026-04-magnet-external-field-reshape-future.html
1•rbanffy•35m ago•0 comments

Web UI in Go? Nothing Can Stop Me

https://medium.com/@mailbox.sq7/web-ui-in-go-nothing-can-stop-me-60d75c4cd4f0
1•alzhi7•39m ago•1 comments

Show HN: Axle – a11y/WCAG CI that proposes real source-code fixes via Claude

https://axle-iota.vercel.app
1•swapvideo•39m ago•1 comments

The Podcast Where You Can Eavesdrop on the A.I. Elite

https://www.nytimes.com/2026/04/26/business/dwarkesh-patel-podcast-ai.html
3•pilooch•43m ago•0 comments

Telegram Launches Managed Bots

https://twitter.com/telegram/status/2048098691391852966
1•hestefisk•44m ago•0 comments

The incredible double life of a spyware salesman turned spy

https://www.ft.com/content/fef3bc59-358a-4e43-aef1-e61194d8b908
1•Anon84•50m ago•1 comments

Designing for Agents

https://twitter.com/teddy_riker/status/2047312986696454584
1•talboren•50m ago•0 comments

It's OK to Use Floating Point for Money

https://suricrasia.online/blog/its-ok-to-use/
1•edent•52m ago•0 comments

The slow death of purposeless walking (2014)

https://www.bbc.com/news/magazine-27186709
1•downbad_•53m ago•1 comments

'Athens cannot operate as a hotel':mayor vows to rescue capital from overtourism

https://www.theguardian.com/world/2026/apr/25/athens-cannot-operate-as-a-giant-hotel-mayor-vows-t...
2•mschuster91•1h ago•0 comments

Show HN: A free ESG stock screener that publishes its losses and methodology

https://jumpstartsignal.com/
1•irldexter•1h ago•0 comments

Could creativy in LLM emerge by reframing language?

1•nopelican•1h ago•0 comments

21-year-old Polish Woman Fixed a 20-year-old Linux Bug

https://itsfoss.com/news/kamila-enlightenment-e16-bug/
1•stared•1h ago•2 comments

Show HN: DSS, a lightweight TUI spreadsheet editor and dashboard in Go

https://github.com/VincenzoManto/DSSGo
5•databasa•1h ago•0 comments

Statecharts: hierarchical state machines

https://statecharts.dev/
15•sph•1h ago•0 comments