frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Control Flow Integrity for Computer Use Agents

https://arxiv.org/abs/2601.09923
1•iliaishacked•1m ago•1 comments

Kamal: Deploy Web Apps Anywhere

https://kamal-deploy.org/
1•ndr•2m ago•0 comments

Show HN: Codex Plus – Turbocharged OpenAI Codex for Headless Workflows

https://github.com/aperoc/codex-plus
1•SafeDusk•4m ago•0 comments

The Discoveries of Continuations [pdf]

https://homepages.inf.ed.ac.uk/wadler/papers/papers-we-love/reynolds-discoveries.pdf
1•fanf2•5m ago•0 comments

I built a tool to help me stop refreshing this site

https://hn-buddy.com/
1•gaborme•6m ago•1 comments

If a Tree Falls – The Trial of the Sycamore Gap Killers

https://harpers.org/archive/2026/01/if-a-tree-falls-rosa-lyster-sycamore-gap/
1•bcraven•6m ago•0 comments

Browser Built with Cursor Agents in Just One Week

https://quasa.io/media/cursor-s-ai-revolution-building-a-browser-from-scratch-with-gpt-5-2-agents...
2•roboboffin•9m ago•0 comments

Artificial StupidIntelligence and Airport Sinks

https://www.deobald.ca/essays/2026-01-13-artificial-stupidintelligence-and-airport-sinks/
1•vishnukvmd•12m ago•0 comments

Product Documentations for AI SEO

1•udit_50•16m ago•0 comments

The spectrum of isolation: From bare metal to WebAssembly

https://buildsoftwaresystems.com/post/guide-to-execution-environments/
3•ThierryBuilds•20m ago•1 comments

I Made Adobe CC Installers Work on Linux

https://old.reddit.com/r/linux_gaming/comments/1qdgd73/i_made_adobe_cc_installers_work_on_linux_p...
3•XzetaU8•23m ago•0 comments

Opening the AWS European Sovereign Cloud

https://aws.amazon.com/blogs/aws/opening-the-aws-european-sovereign-cloud/
4•notmine1337•24m ago•3 comments

How WhatsApp Took over the Global Conversation

https://www.newyorker.com/magazine/2026/01/19/how-whatsapp-took-over-the-global-conversation
1•petethomas•26m ago•0 comments

PostgreSQL in Gleam with pog, squirrel, and cigogne

https://nulltree.xyz/articles/basic-postgres-setup-in-gleam/
2•todsacerdoti•33m ago•0 comments

Some 20-sided dice from Ptolemaic Egypt (ca.140BC)

https://mathstodon.xyz/@dpiponi/115770670004578550
1•aebtebeten•34m ago•0 comments

Ask HN: How to work with Claude Agent SDK durability?

1•spacemnstr42069•34m ago•0 comments

Fake It – Guess the Imposter

https://fakeitgame.com
1•SFGWisdow•38m ago•1 comments

Catching API regressions with snapshot testing

https://kreya.app/blog/api-snapshot-testing/
2•CommonGuy•40m ago•0 comments

pf: Make af-to less magical

https://undeadly.org/cgi?action=article;sid=20260116085115
4•defrost•40m ago•0 comments

Personal Intelligence: Connecting Gemini to Google Apps

https://blog.google/innovation-and-ai/products/gemini-app/personal-intelligence/
1•thunderbong•41m ago•0 comments

I was a top 0.01% Cursor user. Here's why I switched to Claude Code 2.0

https://blog.silennai.com/claude-code
2•Areibman•41m ago•0 comments

Ask HN: Why do we wait for PR to review obvious slop

1•moshetanzer•42m ago•0 comments

Windows? Linux? Browser? Same Executable

https://hackaday.com/2026/01/15/windows-linux-browser-same-executable/
2•birdculture•45m ago•0 comments

Show HN: GitHub – Burn – Rust tensor library and deep learning framework

https://github.com/tracel-ai/burn
2•criexe•46m ago•0 comments

ACX 2025 prediction contest retrospective

https://entropicthoughts.com/acx-2025-prediction-contest-retrospective
2•kqr•47m ago•0 comments

Software Crisis

https://en.wikipedia.org/wiki/Software_crisis
2•tin7in•49m ago•0 comments

Life Levels – Make choices and see your life in 15 years

https://mathgameshero.com/life-levels/
1•absurdwebsite•55m ago•1 comments

How the New Yorker Became Irrelevant

https://www.persuasion.community/p/how-the-new-yorker-lost-its-soul
2•simonebrunozzi•1h ago•0 comments

What does it mean to be a programming language, anyhow? (2021) [pdf]

https://dl.acm.org/doi/pdf/10.1145/3480947
1•hun3•1h ago•0 comments

Show HN: SharpSkill- Realign markets needs and developers

https://sharpskill.fr/en
1•MakeMilk•1h ago•0 comments