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.

'The EU runs on Microsoft' – and Uncle Sam could turn it off

https://www.theregister.com/2026/02/04/eu_foss_fears/
1•belter•1m ago•0 comments

Geometric Art in Ancient Greece

https://www.metmuseum.org/essays/geometric-art-in-ancient-greece
1•sebg•3m ago•0 comments

meepr

https://meepr.co/
1•bookofjoe•3m ago•0 comments

Show HN: Graph DB-backed game, like Dobble/Spot it to play with Projective Plane

https://github.com/jpadams/fastmatchgame
1•jpadamspdx•4m ago•0 comments

Biggest Hacker News Topics Across Time

https://hn-related.dosaygo.com/?sort=comments&p=1
1•keepamovin•6m ago•0 comments

A First Course in Causal Inference

https://arxiv.org/abs/2305.18793
1•Anon84•7m ago•0 comments

Commodore, IBM, OS/2, ARexx: Deal or No Deal?

https://datagubbe.se/os2/
4•rbanffy•9m ago•0 comments

Caught Trojan Malware in VSCode Extension with 1.3M Downloads

https://vscan.dev/?analysisId=74c4c949-862c-46c8-a8be-e469d4ff1f06
3•shadow-ninja•12m ago•1 comments

TrumpRx: Find the lowest prices on prescription drugs

https://trumprx.gov/
2•0xedb•12m ago•0 comments

Schneier: Data is a toxic asset (2016)

https://www.schneier.com/blog/archives/2016/03/data_is_a_toxic.html
1•insuranceguru•14m ago•0 comments

Show HN: Obooko – Free ebooks with an open-source web reader

https://www.obooko.com
2•gthartley•14m ago•0 comments

Building an ERP in public and looking for early partners

https://www.vantumerp.com/
1•adnan1507•15m ago•1 comments

$300B Evaporated. The SaaS -Pocalypse Has Begun

https://www.forbes.com/sites/donmuir/2026/02/04/300-billion-evaporated-the-saaspocalypse-has-begun/
1•user20180120•15m ago•0 comments

C Isn't a Language

https://faultlore.com/blah/c-isnt-a-language/
2•stickynotememo•16m ago•1 comments

Before Crisis Strikes – Smartwatch Tracks Triggers for Opioid Misuse

https://today.ucsd.edu/story/before-crisis-strikes-smartwatch-tracks-triggers-for-opioid-misuse
1•geox•18m ago•0 comments

Show HN: AcidTest – Security scanner for AI agent skills

https://github.com/currentlycurrently/acidtest
1•cloudmanager•19m ago•1 comments

Show HN: IncidentFox, AI SRE that auto-builds its own integrations (open source)

https://github.com/incidentfox/incidentfox
1•chiehminwei•22m ago•0 comments

Sudo's maintainer needs resources to keep utility updated

https://www.theregister.com/2026/02/03/sudo_maintainer_asks_for_help/
2•birdculture•22m ago•0 comments

Ask HN: Does global decoupling from the USA signal comeback of the desktop app?

3•wewewedxfgdf•23m ago•0 comments

On history and flattery

https://www.historians.org/perspectives-article/on-history-and-flattery/
1•hhs•26m ago•0 comments

Watch Claude Code debug WebGPU code without a GPU

https://www.youtube.com/watch?v=x791YvPIhFo
1•logicallee•27m ago•1 comments

Show HN: PR Bro – a TUI that helps you decide what PR to review next

https://github.com/toniperic/pr-bro
1•toneric•31m ago•0 comments

The AI boom's surprising winners aren't even tech companies

https://www.axios.com/2026/02/05/ai-walmart-stock-wmt-consumer-goods
2•petethomas•32m ago•0 comments

CIA World Factbook Ends Publication After 6 Decades

https://www.nytimes.com/2026/02/05/us/world-factbook-cia.html
4•doener•32m ago•0 comments

A 16yo's mathematical derivation of Model Collapse (The Ainex Singularity)

https://zenodo.org/records/18434665
1•Mhh1430•34m ago•0 comments

Agents should not use Git anymore

https://ataraxy-labs.github.io/sem/
1•rohan2003•34m ago•1 comments

Show HN: Total Recall – write-gated memory for Claude Code

https://github.com/davegoldblatt/total-recall
2•davegoldblatt•36m ago•2 comments

Fusing communication and compute with new API and copy engine collective in NCCL

https://developer.nvidia.com/blog/fusing-communication-and-compute-with-new-device-api-and-copy-e...
1•teleforce•36m ago•0 comments

Homeland Security is trying to force tech to hand over data about Trump critics

https://www.yahoo.com/news/articles/homeland-security-trying-force-tech-183519291.html
7•c420•37m ago•0 comments

Kilo Pass by Kilo Code

https://kilo.ai/features/kilo-pass
1•satvikpendem•39m ago•0 comments