frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Ascend – DevSecOps framework with AI-powered merge conflict sync

https://github.com/venkatapgummadi/ascend
1•venkatapgummadi•2m ago•0 comments

OpenAI reinvents Recall except everything is stored remotely

https://developers.openai.com/codex/memories/chronicle
1•pluc•2m ago•0 comments

Show HN: Individual App Power Usage Tracking on Mac in Native Swift

https://macbookbatterymonitor.com
1•ryan-a•4m ago•0 comments

The Commodification of Travel

https://herman.bearblog.dev/the-commodification-of-travel/
2•HermanMartinus•7m ago•0 comments

Making RAM at Home [video]

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

Meta to start capturing employee mouse movements, keystrokes for AI training

https://tech.yahoo.com/ai/meta-ai/articles/exclusive-meta-start-capturing-employee-162745915.html
3•devonnull•7m ago•0 comments

Kuri – Zig based agent-browser alternative

https://github.com/justrach/kuri
1•sorcercode•8m ago•1 comments

The Silo Problem: Three Teams, Three Truths

https://planetform.io/blog/the-silo-problem
1•rtwo_infra•15m ago•0 comments

Layer 8: The coordination protocol AI agents and embedded devices don't have yet

https://www.ezthrottle.network/blog/operationless-network-for-a-new-world-of-devices
1•rjpruitt16•16m ago•0 comments

Show HN: Govern Anthropic Managed Agents with 3 lines of code

1•inderrr•17m ago•0 comments

Remote (Latam) or San Francisco Onsite – Full Time

https://jobs.hireatomic.com/jobs/192386-ai-principal-software-engineer-us-saas-startup-remote
1•atomichire•23m ago•1 comments

Building a fault-tolerant metrics storage system at Airbnb

https://medium.com/airbnb-engineering/building-a-fault-tolerant-metrics-storage-system-at-airbnb-...
1•gpi•25m ago•0 comments

MAGA Has Exposed a Jesus Problem in US Christianity

https://recklessgrace.substack.com/p/maga-has-exposed-a-jesus-problem
2•TimW57•26m ago•0 comments

Show HN: Benchmarking how AI models write vulnerable code under pressure

https://leaderboard.atella.ai/code-security.html
1•kitdobyns•29m ago•1 comments

NASA's Curiosity rover finds organic molecules on Mars

https://www.theguardian.com/science/2026/apr/21/nasa-curiosity-rover-finds-organic-molecules-mars
4•billybuckwheat•29m ago•0 comments

the_world

https://hesnotjoking.substack.com/p/the-world-c8c
1•ucla_rob•31m ago•0 comments

UK Permanently Bans Tobacco for Anyone Born in 2009 or After

https://www.nytimes.com/2026/04/21/world/europe/uk-smoking-ban-2009.html
2•m-hodges•36m ago•1 comments

Migrate a Legacy Codebase with Sandbox Agents

https://developers.openai.com/cookbook/examples/agents_sdk/sandboxed-code-migration/sandboxed_cod...
1•gmays•39m ago•0 comments

Birthright Citizenship and Youth Crime

https://marginalrevolution.com/marginalrevolution/2026/04/birthright-citizenship-and-youth-crime....
3•pulisse•50m ago•1 comments

NeurIPS Supports Authors with Google's Paper Assistant Tool (Pat)

https://blog.neurips.cc/2026/04/21/neurips-supports-authors-with-googles-paper-assistant-tool-pat/
1•adt2bt•51m ago•0 comments

National Library Workers Day

https://ala-apa.org/nlwd/
1•ynac•52m ago•0 comments

Slash is a tiny helper that keeps you organized and focused

https://getslash.co/
2•igtztorrero•53m ago•0 comments

How to Implement an FPS Counter

https://vplesko.com/posts/how_to_implement_an_fps_counter.html
1•vplesko•53m ago•0 comments

Do This Instead of Rock Paper Scissors If You Are More Than 2 People

https://vplesko.com/posts/zimi_zami_zum.html
1•vplesko•53m ago•0 comments

2× – nine months later: We did it (Intercom)

https://ideas.fin.ai/p/2x-nine-months-later
1•mmarian•54m ago•0 comments

This Slug Can Survive on Photosynthesis [video]

https://www.youtube.com/watch?v=IH_uv4h2xYM
1•CharlesW•55m ago•0 comments

KDE at 30

https://kde.org/anniversaries/30/
2•kristianp•57m ago•0 comments

Apple's Cal AI crackdown signals it's still policing the App Store

https://techcrunch.com/2026/04/21/apples-cal-ai-crackdown-signals-its-still-policing-the-app-store/
3•CharlesW•57m ago•1 comments

The Infamous Coin Toss

https://ergodicityeconomics.com/2023/07/28/the-infamous-coin-toss/
1•efavdb•58m ago•0 comments

Global growth in solar "the largest ever observed for any source"

https://arstechnica.com/science/2026/04/global-growth-in-solar-the-largest-ever-observed-for-any-...
12•tambourine_man•59m ago•2 comments