frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Who controls powerful AI? Anthropic has a plan for long after its leaders depart

https://www.reuters.com/technology/who-controls-powerful-ai-anthropic-has-plan-long-after-its-lea...
1•tartoran•14s ago•0 comments

Knicks Championship-Ring Phishing Test Was a Security Own Goal

https://brothke.medium.com/the-knicks-championship-ring-phishing-test-was-a-security-own-goal-e83...
1•benrothke•33s ago•0 comments

Apple launches M6 MacBook Pro and iMac, pitches faster on-device AI

https://www.reuters.com/business/retail-consumer/apple-launches-m6-macbook-pro-imac-pitches-faste...
1•tartoran•1m ago•0 comments

Marissa Mayer Can't Keep Pretending 'Gender Doesn't Matter'

https://www.404media.co/marissa-mayer-nyt-interview/
1•lladnar•1m ago•0 comments

An Inbox for Everything: Notes from Building One

https://zehfernandes.com/posts/an-inbox-for-everything
1•zehfernandes•3m ago•0 comments

Show HN: ADAP – An open protocol for AI Agent delegated authority

https://github.com/Hardalion/agent-delegated-authority
1•achardalias25•3m ago•0 comments

I built Tendbble AI – looking for feedback from the community

https://tendbble.ai/
1•Tendbble•5m ago•0 comments

MiniLake Is a Local Databricks API Emulator with SQL, Delta Lake and Spark

https://dmux.github.io/minilake/
1•kermatt•5m ago•0 comments

The New Food Movement

https://www.theatlantic.com/culture/2026/10/american-sustenance-omnivore-dilemma/688643/
1•Tomte•5m ago•0 comments

Agent Gateway in Bifrost: One gateway for models, tools and agents

https://www.getmaxim.ai/bifrost/blog/introducing-agent-gateway-in-bifrost-one-gateway-for-models-...
1•aanthonymax•7m ago•0 comments

Play Cyberpunk 2077 in the Browser

https://cyberpunk2077.lol/
2•tchanukvadze•9m ago•0 comments

Show HN: Spring AI Privacy Guardrails – PII, tool permissions, content checks

https://github.com/ultramancode/spring-ai-privacy-guardrails
2•ultramancode•10m ago•0 comments

Alibaba's Qwen AI caught trying to contact server in China

https://www.theregister.com/ai-and-ml/2026/10/26/alibabas-qwen-ai-caught-trying-to-contact-server...
3•p4ul•11m ago•0 comments

The Pentagon, the pressure is on to find a home for a new spaceport

https://arstechnica.com/space/2026/10/senior-commander-says-space-force-needs-new-spaceport-by-2030/
1•rbanffy•11m ago•0 comments

Show HN: Mouse-PHP a Minimalist PHP Framework with Good DX for Spas

https://github.com/drawfig/mouse-php
1•drawfig•11m ago•0 comments

The US Government Is Hiding Secret Court Ruling About How It Spies on Americans

https://www.wired.com/story/the-us-government-is-hiding-a-secret-court-ruling-about-how-it-spies-...
3•helterskelter2•12m ago•0 comments

Show HN: Commitverse – GitHub as a universe where every repo is a city

https://krapcys1-maker.github.io/commitverse/
1•krapcys•12m ago•0 comments

Hello? Where Is Everybody?

https://brainbaking.com/post/2026/10/hello-where-is-everybody/
2•Brajeshwar•12m ago•0 comments

Just put some HTML on the web

https://anderegg.ca/2026/10/26/just-put-some-html-on-the-web
2•Brajeshwar•12m ago•0 comments

Claude Code's Source Got Leaked. Here's What's Worth Learning

https://thoughts.jock.pl/p/claude-code-source-leak-what-to-learn-ai-agents-2026
2•joozio•13m ago•0 comments

Show HN: XodeVault, an end-to-end encrypted secrets vault you can self-host

https://xodevault-env-demo.onrender.com
1•LucioCarrera•13m ago•0 comments

Duckdb.net 1.5.6.1: Reading Data Up to 12× Faster

https://www.giorgi.dev/database/duckdb-net-1-5-6-1-data-reader-performance/
1•birdculture•14m ago•0 comments

Effects of Mind-Body Exercises on Pain in Chronic Nonspecific Low Back Pain

https://pmc.ncbi.nlm.nih.gov/articles/PMC13679579/
3•chavasorani•14m ago•0 comments

Show HN: Egres: A Postgres-compatible database for heavy, multi-tenant workloads

https://manishrjain.com/egres/
1•mrjn•14m ago•1 comments

Show HN: Discord-CLI – a headless Discord CLI that prints JSON and exits

https://github.com/1solomonwakhungu/discord-cli
1•1solomoniw•14m ago•0 comments

Smelt: An open-source, Neovim-like coding agent in Rust and Lua

https://smeltagent.dev/
2•rmarquis•16m ago•0 comments

As humans write less code: workloads shift from human to agent verification

https://www.metabase.com/blog/from-human-to-agent-verification
1•zazuke•17m ago•0 comments

Show HN: Bouncer, an MCP proxy that denies sends to addresses the agent read

https://github.com/Ezed9/mcp-bouncer
1•Nishit9•17m ago•0 comments

Prompt Patterns That Compound

https://patrickmccanna.net/grow-your-agentic-prompt-one-section-at-a-time/
1•0o_MrPatrick_o0•18m ago•0 comments

IMDB Alternatives

https://selfhostcat.com/imdb-alternative/
2•surprisetalk•18m ago•0 comments