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.

Why people say they're using 'Are You Dead?' (&others)

https://www.npr.org/2026/02/03/nx-s1-5694669/loneliness-isolation-app-are-you-dead-snug-alone
1•defrost•1m ago•0 comments

Crime rates of undocumented-, legal immigrants, & native-born citizens in Texas

https://www.pnas.org/doi/10.1073/pnas.2014704117
1•u1hcw9nx•2m ago•0 comments

Linux Emulation on Apple iPads: iSH, JIT, and the EU Digital Markets Act (2024)

https://ish.app/blog/ish-jit-and-eu
1•walterbell•9m ago•0 comments

NationStates security breach caused by own player

https://web.archive.org/web/20260130051909/https://nationstates.net/
1•aa_is_op•10m ago•0 comments

Show HN: I wrote a Semgrep alternative in Rust with cross-file taint tracking

https://github.com/bumahkib7/rust-monorepo-analyzer
1•bumahkib7•11m ago•0 comments

Curlme.io – Terminal-first HTTP request debugging

https://curlme.io/
1•OMoutaz•11m ago•1 comments

Show HN: Clienwork – Client portal for freelancers and agencies

https://clienwork.com
1•runai•14m ago•0 comments

Devs Need AI Training

1•eibrahim•15m ago•0 comments

Europe's tech sovereignty watch (74% of EU companies depend on US tech services)

https://proton.me/business/europe-tech-watch
4•giuliomagnifico•19m ago•0 comments

A11yJSON: A standard to describe the accessibility of the physical world

https://sozialhelden.github.io/a11yjson/
1•robin_reala•22m ago•0 comments

Show HN: Maths Worksheet Generator – Fun printable worksheets for kids

https://maths.antfie.com
1•antfie•22m ago•0 comments

How to Convert OST Files to PST

https://apps.microsoft.com/detail/9p62fq9z8x7p?hl=en-US&gl=US
1•tieanderson•23m ago•1 comments

The API Tooling Crisis: Why developers are abandoning Postman and its clones?

http://efp.asia/blog/2025/12/24/api-tooling-crisis/
1•birdculture•26m ago•0 comments

EU-based transactional email service

https://lettermint.co
1•Aldipower•27m ago•0 comments

How to Evaluate an Online Tarot Platform (and What Most Users Overlook)

https://medium.com/@enrique_15267/how-to-evaluate-an-online-tarot-platform-and-what-most-users-ov...
1•astroideal•29m ago•0 comments

Erys: Terminal Interface for Jupyter Notebooks

https://github.com/natibek/erys
1•gballan•31m ago•0 comments

Sealos – AI Native Cloud Cloud Operating System

https://github.com/labring/sealos
1•fanux•32m ago•0 comments

Remote Access to Long-Running AI Tasks: A Tmux and Tailscale Setup Guide

https://orangehill.dev/posts/how-to-remotely-monitor-your-claude-code-sessions-from-your-phone
1•tihomiropacic•33m ago•0 comments

Paris prosecutors raid France offices of Elon Musk's X

https://www.bbc.com/news/articles/ce3ex92557jo
11•vikaveri•35m ago•2 comments

Show HN: Nioh guide site – release info, beginner guides, and builds

https://nioh3.net/
1•tanjump•39m ago•0 comments

Show HN: Highguard Wiki – tier lists, wardens and weapons, beginner guides

https://highguard.app/
1•tanjump•40m ago•0 comments

Raw₿It – Visual Raw Transaction Builder and Script Debugger (Open-Source)

https://github.com/rawBit-io/rawbit
1•rawBit_io•42m ago•1 comments

The Vcdiff Generic Differencing and Compression Data Format (2002)

https://datatracker.ietf.org/doc/html/rfc3284
1•yagizdegirmenci•44m ago•0 comments

Even remote Pacific fish are full of microplastics

https://www.sciencedaily.com/releases/2026/02/260203020202.htm
2•ag220906•44m ago•0 comments

LLMs as the new high level language

https://federicopereiro.com/llm-high/
1•swah•44m ago•0 comments

Matchbox: A record matching and entity resolution orchestration tool

https://github.com/uktrade/matchbox
1•RobinL•49m ago•0 comments

European pensions are in dire need of reform

https://www.economist.com/graphic-detail/2025/12/01/european-pensions-are-in-dire-need-of-reform
3•simonebrunozzi•51m ago•1 comments

Show HN: Browser based state machine simulator and visualizer

https://svylabs.github.io/smac-viz/
1•sridhar87•51m ago•0 comments

U.S. Federal Data Is Disappearing

https://www.notus.org/trump-white-house/federal-data-is-disappearing
3•giuliomagnifico•51m ago•0 comments

The Human Web Is Becoming Agent Web

https://www.nibzard.com/agent-web
1•nkko•52m ago•0 comments