frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Deepak Jain to Host Two Sessions at Nvidia GTC 2026

1•AiNETFactory•28s ago•0 comments

You Can Stop Marrying for Genes

https://fi-le.net/love/
1•fi-le•1m ago•0 comments

Amazon Employees Say AI Is Just Increasing Workload

https://gizmodo.com/amazon-employees-say-ai-is-just-increasing-workload-a-new-study-confirms-thei...
1•jamesgill•2m ago•0 comments

Ask HN: Developing .NET on Windows but deploying to Linux – common issues?

1•ghostlyInc•3m ago•1 comments

Boeing's Bizarre Planes That Were Never Built

https://www.airlineratings.com/articles/boeings-bizarre-planes-that-were-never-built
1•voxadam•4m ago•0 comments

On Making

https://beej.us/blog/data/ai-making/
1•ingve•5m ago•0 comments

Zeno's Paradox resolved by physics, not by math alone

https://bigthink.com/starts-with-a-bang/zenos-paradox/
1•bryanrasmussen•6m ago•1 comments

What's That? – Photo to personalized audio narrative in under 10 seconds

https://apps.apple.com/us/app/whats-that-ai-audio-guide/id6756409506
1•whatsthatapp•8m ago•1 comments

The Dopamine Trap of Vibe Coding

https://codn.dev/blog/the-dopamine-trap-of-vibe-coding/
2•romanhoffmann•10m ago•2 comments

MCPs, CLIs, and skills: when to use what?

https://jngiam.bearblog.dev/mcps-clis-and-skills-when-to-use-what/
3•jngiam1•10m ago•0 comments

A Snapshotable WASM Interpreter

https://github.com/friendlymatthew/gabagool
1•birdculture•11m ago•0 comments

Looking for Partner to Build Agent Memory (Zig/Erlang)

1•kendallgclark•13m ago•0 comments

Show HN: Codex Symphony – bootstrap OpenAI Symphony and Linear in any repo

https://github.com/Citedy/codex-symphony
1•ntty•14m ago•0 comments

How to use Claude Cowork – Complete guide

https://overtoncollective.com/blog/claude-cowork-complete-guide
1•Ali_Jiwani•14m ago•0 comments

Content Creator

https://fgiesen.wordpress.com/2025/07/06/content-creator/
1•pocksuppet•15m ago•0 comments

Selectively reducing eval awareness and murder in Gemma 3 27B via steering

https://www.lesswrong.com/posts/QfM6SHyBPveDtHAma/selectively-reducing-eval-awareness-and-murder-...
1•gmays•17m ago•0 comments

How Homeowners Are Turning to Adjustable-Rate Mortgages, in Charts

https://www.wsj.com/personal-finance/adjustable-rate-mortgages-charts-18eec3b5
1•JumpCrisscross•17m ago•0 comments

The Dark Story of America's Most Iconic Bicycle [video]

https://www.youtube.com/watch?v=7t7bN04Xryg
1•lisper•17m ago•0 comments

An old photo of a large BBS

https://rachelbythebay.com/w/2022/01/26/swcbbs/
8•xbryanx•18m ago•0 comments

Simple TOTP Generator that works in the browser

https://2fasecure.com
1•techmanager•19m ago•1 comments

The Iron Dome is failing [video]

https://www.youtube.com/watch?v=JJfSHsYfzXk
2•spiderfarmer•20m ago•0 comments

A single AI dev workspace for all your services

https://www.superblocks.com/blog/a-single-dev-workspace-for-ai-agents
1•ranma•22m ago•0 comments

An Ode to Bzip

https://purplesyringa.moe/blog/an-ode-to-bzip/
2•enz•23m ago•0 comments

Zapcode: A TypeScript interpreter in Rust for AI agents (2µs start, sandbox)

https://github.com/TheUncharted/zapcode
1•TheUncharted•23m ago•1 comments

Show HN: Generator SFT and DPO datasets for tool-calling LoRA fine-tuning

https://nothumanallowed.com/datasets
1•senza1dio•23m ago•1 comments

Iranian speed boats approach US Navy ships in the Strait of Hormuz (2012) [video]

https://www.youtube.com/watch?v=E6TTYuAKN5U
1•joebig•23m ago•0 comments

Why the Kids Won't Farm

https://www.nytimes.com/2026/03/12/opinion/who-will-farm-america.html
2•saikatsg•26m ago•1 comments

A Field Guide to Reward Hacking in AI Kernel Generation

https://www.wafer.ai/blog/reward-hacks-field-guide
1•matt_d•27m ago•0 comments

Could a New Kind of Data Center Give Back to the Grid?

https://www.nlr.gov/news/detail/program/2026/could-new-kind-of-data-center-give-back-to-the-grid
1•ph0rque•28m ago•0 comments

Multi-Player Game Networking, Explained

https://www.gabrielgambetta.com/client-server-game-architecture.html
2•ascent817•28m ago•1 comments