frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Core Devices (Pebble Watch) Keeps Stealing Our Work

https://rebble.io/2025/11/17/core-devices-keeps-stealing-our-work.html
1•jdauriemma•2m ago•0 comments

Rust9x Unofficial "Tier 4" Rust Target for Windows 9x/Me/NT/2000/XP/Vista

https://github.com/rust9x/rust
2•kristianp•9m ago•0 comments

Unauthenticated WireGuard Key Registration in PIA Infrastructure

https://twitter.com/MagicalTux/status/1990611940151021998
1•MagicalTux•9m ago•0 comments

The Bezzle

https://en.wikipedia.org/wiki/The_bezzle
1•toomuchtodo•10m ago•1 comments

Star Wars Theatrical OT Official Restoration Leaked Clips

https://www.youtube.com/watch?v=4Z6TpkT-6AY
1•layer8•11m ago•0 comments

Stanford Prison Experiment

https://www.prisonexp.org
1•netule•22m ago•0 comments

Gulf of Mexico – The Perfect Progamming Language

https://github.com/James-HoneyBadger/GulfOfMexico
2•honeybadger1965•23m ago•1 comments

Crypto Could Trigger the Next Financial Crisis

https://www.theatlantic.com/ideas/2025/11/cryptocurrency-economy-financial-crisis/684960/
1•harambae•26m ago•0 comments

UC Berkeley scientists hail breakthrough in decoding whale communication

https://www.sfgate.com/bayarea/article/scientists-breakthrough-decoding-whales-21184413.php
3•daegloe•26m ago•1 comments

An intrinsic magnetic field does not protect a planet against atmospheric escape

https://www.aanda.org/articles/aa/full_html/2018/06/aa32934-18/aa32934-18.html
1•thunderbong•28m ago•0 comments

Show HN: Discussion of ICT Model – Linking Information, Consciousness and Time

https://www.academia.edu/s/8924eff666
1•DmitriiBaturo•30m ago•0 comments

The first-ever protocol for websites and AI browsers to cooperate

https://astral.cleobrowser.com
1•pablooomvc•33m ago•0 comments

Exposing and Exploiting Incomplete Branch Predictor Isolation in Cloud

https://comsec.ethz.ch/research/microarch/vmscape-exposing-and-exploiting-incomplete-branch-predi...
1•abhisek•36m ago•0 comments

Minimal Periodic Task Runner in Elixir

https://www.jasontokoph.com/tidbits/minimal-periodic-task-runner-in-elixir/
1•todsacerdoti•38m ago•0 comments

Ask HN: What insight or habit has led to a breakthrough in your mental health?

2•Schroedingers2c•41m ago•4 comments

Selection and transmission of gut microbiome alone can shift mammalian behavior

https://www.nature.com/articles/s41467-025-65368-w
1•bookofjoe•44m ago•0 comments

My Professional X Account Was Hacked and X Support Won't Help Me

2•Bobdavies•47m ago•1 comments

DoorDash Says Personal Information Stolen in Data Breach

https://www.securityweek.com/doordash-says-personal-information-stolen-in-data-breach/
3•Bender•52m ago•0 comments

Europol takes down more than 1k malicious servers in Operation Endgame

https://www.scworld.com/news/europol-takes-down-more-than-1000-malicious-servers-in-operation-end...
1•Bender•52m ago•0 comments

Amazon finds 150K NPM packages linked to token-farming campaign

https://www.scworld.com/news/amazon-finds-150k-npm-packages-linked-to-token-farming-campaign
1•Bender•53m ago•0 comments

Apple to Change Designs and Release Schedule

https://www.bloomberg.com/news/videos/2025-11-17/apple-to-change-designs-and-release-schedule-video
2•mgh2•54m ago•0 comments

How We Built and Launched an MVP During Techstars

https://substack.com/inbox/post/178297555
1•azhenley•55m ago•0 comments

Linus Pauling

https://en.wikipedia.org/wiki/Linus_Pauling
1•CGMthrowaway•1h ago•0 comments

Building a Crowd-Feeding platform. Donors fund meals, Restaurants serve them

https://www.bridgemeals.com/
2•willingo•1h ago•0 comments

Show HN: I built and AI phone system and wrote a step by step instructions

https://www.yadalog.com/blog/ai_phone_system_voicemail
2•bacdor•1h ago•0 comments

Soccer, emeralds and cocaine: The 'new' Colombian drug lords with ties to Spain

https://english.elpais.com/international/2025-11-02/soccer-emeralds-and-cocaine-the-new-colombian...
2•PaulHoule•1h ago•0 comments

U.N. Security Council Adopts U.S. Peace Plan for Gaza

https://www.nytimes.com/2025/11/17/world/middleeast/un-security-council-gaza-peace-plan.html
3•vladgur•1h ago•1 comments

'Ugly' Technicals Put the US Stock Rally at Risk of Correction

https://www.bloomberg.com/news/articles/2025-11-17/-ugly-technicals-put-the-us-stock-rally-at-ris...
1•zerosizedweasle•1h ago•0 comments

Randomized Banner Icons with JavaScript and Astro Framework

https://www.nickstambaugh.dev/posts/random-banner-icons
1•sieep•1h ago•0 comments

Lower Than London

https://diamondgeezer.blogspot.com/2025/11/lower-than-london.html
1•zeristor•1h ago•0 comments