frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Hack discovered at (NL) Ministry of Finance; Unclear if data was accessed

https://nltimes.nl/2026/03/24/hack-discovered-ministry-finance-unclear-data-accessed
1•mvdwoord•4m ago•0 comments

USA bans all new routers for consumers

https://www.heise.de/en/news/USA-bans-all-new-routers-for-consumers-11222049.html
2•esher•4m ago•0 comments

Synaps a self-hosted personal health monitor as a weighted knowledge grap

https://github.com/scerelli/SYNAPS
1•succo•6m ago•1 comments

Steve Jobs, speech at the Apple campus (1999) [video]

https://www.youtube.com/watch?v=EoM2Y2KO6kU
1•rbinv•7m ago•0 comments

Can It Resolve Doom? Game Engine in 2k DNS Records

https://core-jmp.org/2026/03/can-it-resolve-doom-game-engine-in-2000-dns-records/
1•Einenlum•8m ago•0 comments

Yann LeCun's LeWorldModel: Stable End-to-End JEPA from Pixels

https://le-wm.github.io/
2•matthieu_bl•9m ago•1 comments

America's Chief Financial Officers Say AI Is Coming for Admin Jobs

https://www.wsj.com/tech/ai/ai-admin-job-market-6a1c3436
1•cebert•11m ago•1 comments

The Wrong Abstraction

https://sandimetz.com/blog/2016/1/20/the-wrong-abstraction
2•mihau•14m ago•1 comments

A Mysterious Numbers Station Is Broadcasting Through the Iran War

https://www.wired.com/story/a-mysterious-numbers-station-is-broadcasting-through-the-iran-war/
2•iamben•14m ago•1 comments

OpenBSD.Amsterdam

https://openbsd.amsterdam/
2•sph•15m ago•0 comments

The orderbook dynamics of prediction markets

2•sharp_runner_84•16m ago•0 comments

Donut Lab's solid-state battery could barely hold a charge after getting damaged

https://www.theverge.com/transportation/898881/donut-lab-solid-state-battery-damage-test
1•tromp•19m ago•1 comments

Show HN: Offline-first UK train planner

https://railraptor.com
1•marcusdev•23m ago•0 comments

curl > /dev/sda: How I made a Linux distro that runs wget | dd

https://astrid.tech/2026/03/24/0/curl-to-dev-sda/
2•astralbijection•25m ago•0 comments

More Magic Math from OpenAI?

https://om.co/2026/03/23/more-magic-math-from-openai/
2•MindGods•25m ago•0 comments

Asyncio is neither fast nor slow

https://blog.changs.co.uk/asyncio-is-neither-fast-nor-slow.html
1•tmarice•28m ago•0 comments

Emerging SaaS trends that are accelerating

1•rado-bogdaonv•31m ago•1 comments

Show HN: CertPulse – Certificate monitoring for multi-cloud teams

https://certpulse.dev
1•xdsai•31m ago•1 comments

Show HN: The complete Open Library catalog in clean, analysis-ready Parquet

https://huggingface.co/datasets/open-index/open-library
1•tamnd•31m ago•0 comments

Make better decisions based on criteria that are important to you

https://www.decidit.de/en
1•bjoern_live•35m ago•0 comments

Are Strings Still Our Best Hope for a Theory of Everything?

https://www.quantamagazine.org/are-strings-still-our-best-hope-for-a-theory-of-everything-20260323/
3•isaacfrond•37m ago•1 comments

SIDN chooses European-based infrastructure for .nl DNS anycast (i3D.net)

https://www.i3d.net/sidn-partners-with-i3dnet-dns-infrastructure/
2•sgaduuw•39m ago•1 comments

Important Updates to GitHub Copilot for Students

https://github.com/orgs/community/discussions/189268
3•tamnd•39m ago•0 comments

The ancient reason there are 60 minutes in an hour

https://www.bbc.com/future/article/20260320-the-ancient-reason-there-are-60-minutes-in-an-hour-an...
2•jeffwass•41m ago•0 comments

I built a 30-second dinner decider because weeknight decision fatigue is real

https://orla-app-swart.vercel.app/
2•HoneyBarny•43m ago•0 comments

ISS: Instant Space Switcher for macOS

https://github.com/joshuarli/iss
2•fanf2•49m ago•0 comments

Head-mounted VR hardware will never happen, says Neal Stephenson

https://www.theregister.com/2026/03/24/neal_stephenson_death_of_metaverse/
3•jjgreen•52m ago•0 comments

Arm to host 'Arm Everywhere' event and webcast

https://newsroom.arm.com/news/arm-everywhere-event
3•makerofthings•53m ago•1 comments

Show HN: Using the Web Audio API to simulate various tuning forks

https://github.com/evoluteur/healing-frequencies
1•evoluteur•54m ago•0 comments

Local Bernstein theory, and lower bounds for Lebesgue constants

https://terrytao.wordpress.com/2026/03/23/local-bernstein-theory-and-lower-bounds-for-lebesgue-co...
2•jjgreen•55m ago•0 comments