frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Railway Is Having a Major Outage

https://status.railway.com/#/
1•kgraves•27s ago•0 comments

Why College Grads Are Booing Their Commencement Speakers

https://www.nytimes.com/2026/05/18/opinion/ai-boo-commencement-speeches.html
1•65•1m ago•0 comments

Android CLI Now Stable 1.0

https://android-developers.googleblog.com/2026/05/android-cli-stable-1-0-agent-development.html
1•vyrotek•2m ago•0 comments

Yet Another AI Teammate

https://yaat.sh/
1•e7h4nz•4m ago•0 comments

Intelligent eyewear is coming this fall

https://blog.google/products-and-platforms/platforms/android/android-xr-io-2026/
2•decimalenough•10m ago•0 comments

Build managed agents with the Gemini API

https://blog.google/innovation-and-ai/technology/developers-tools/managed-agents-gemini-api/
1•ddl•10m ago•1 comments

Lighthouse Attention

https://nousresearch.com/lighthouse-attention
1•gmays•10m ago•0 comments

Anthropic Announces Managed Agents – what this means for consumers

https://www.greatworkeveryone.com/writing/cheap-cloud-ai-was-never-going-to-last
1•connorrandazzo•14m ago•0 comments

Why raw blockchain logs are no longer enough

https://blog.bridgexapi.io/bxruntime-terminal-the-rise-of-execution-observability-for-evm-systems
1•Bridgexapi•18m ago•0 comments

State of Crypto 2025: The year crypto went mainstream

https://a16zcrypto.com/posts/article/state-of-crypto-report-2025/
2•Anon84•20m ago•0 comments

Trump Mobile has a real phone, but it may also have a real data leak

https://www.androidauthority.com/trump-mobile-customer-data-link-3668917/
1•bilekas•22m ago•0 comments

Trusted Publishing for NPM Packages

https://docs.npmjs.com/trusted-publishers/
1•healsdata•23m ago•0 comments

Show HN: Reckoner – a semantic browser for structured data

https://reckoner-production.up.railway.app/
1•abk9811•24m ago•1 comments

Ask HN: Natural Language App Testing?

6•devstein•26m ago•0 comments

Google's SynthID AI watermarking tech is being adopted by OpenAI, Nvidia

https://arstechnica.com/google/2026/05/googles-synthid-ai-watermarking-tech-is-being-adopted-by-o...
3•jonbaer•27m ago•0 comments

Agentic PCB Design Sucks

https://github.com/hpm-registry/registry
2•JoonSPP•28m ago•1 comments

Remove AI Watermarks

https://github.com/wiltodelta/remove-ai-watermarks
23•janalsncm•31m ago•9 comments

Show HN: Remindy – A Spotlight-style reminder app for Mac

1•fayecat910•31m ago•0 comments

Language Models Can Autonomously Hack and Self-Replicate

https://palisaderesearch.org/blog/self-replication
2•cassianoleal•33m ago•0 comments

Censorship Request from Coin Insider (2024)

https://support.bitchute.com/blog/bitchute-receives-censorship-request-from-coin-insider
2•aand16•34m ago•0 comments

RemotePower – Self-hosted Linux fleet manager (no database, pure Python)

https://github.com/tyxak/remotepower
1•tyxak•34m ago•1 comments

Show HN: Trophy UI, Open-source gamification components built on Shadcn

https://ui.trophy.so
1•cbrinicombe•36m ago•0 comments

Show HN: AgentWing – make AI agents complete tasks faster

1•12ziyad•37m ago•0 comments

Google Antigravity Python SDK

https://github.com/google-antigravity/antigravity-sdk-python
1•devhouse•37m ago•0 comments

Detecting Relaxed Memory Concurrency Bugs in C and C++ Compilers

https://lukegeeson.com/publications/2026-04-28-Thesis/
3•matt_d•44m ago•0 comments

IRS Must Drop Audits of Trump and Family

https://www.nytimes.com/2026/05/19/us/politics/trump-irs-doj-lawsuit-audit.html
13•duxup•45m ago•2 comments

The downgrading semantics of memory safety (Extended version)

https://arxiv.org/abs/2507.11282
2•matt_d•48m ago•0 comments

Self-Improving Reward Models

https://www.canvas.inc/research/reward-models
2•essamsleiman•50m ago•0 comments

AI and the Practical Scientist

https://scholarlyfutures.substack.com/p/ai-and-the-practical-scientist
1•JohnHammersley•52m ago•0 comments

React Performance Isn't About UseMemo – It's About Render Boundaries

https://reactdevelopment.substack.com/p/react-performance-isnt-about-usememo
1•javatuts•53m ago•1 comments