frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Join a community of nature enthusiasts who share your passion for wildlife

https://www.underthehedge.com/
1•RevillWeb•1m ago•0 comments

For the past 2.5 years my AGI timelines were set for 2025. They missed

1•akira_067•1m ago•0 comments

Turso in the Browser

https://turso.tech/blog/introducing-turso-in-the-browser
1•matesz•2m ago•0 comments

KDE like desktop environment, but for Windows 11

https://github.com/crosschainer/shelled
2•crosschainer•3m ago•0 comments

Meta wins US antitrust case and won't have to break off WhatsApp or Instagram

https://www.theguardian.com/technology/2025/nov/18/meta-antitrust-win-whatsapp-instagram
2•n1b0m•3m ago•0 comments

Ask HN: Gemini 3 and the stagnation of coding agents, what gives?

2•akira_067•4m ago•0 comments

Thepromptbook

https://thepromptbook.ai
1•x86i•5m ago•1 comments

Cosmic Paradox Reveals the Awful Consequence of an Observer-Free Universe

https://www.quantamagazine.org/cosmic-paradox-reveals-the-awful-consequence-of-an-observer-free-u...
1•fleahunter•5m ago•0 comments

Show HN: Moneydevkit – The fastest way for anyone to take payments

https://moneydevkit.com
1•npslaney•6m ago•0 comments

Create Your Own Virtual Worlds in Minutes with AI World Generator

https://aiworldgenerator.com/
1•ri-vai•10m ago•1 comments

Google Summer of Code Results 2025 – Rust Blog

https://blog.rust-lang.org/2025/11/18/gsoc-2025-results/
2•sorcercode•12m ago•0 comments

We should rename .unwrap() to .or_panic()

https://hachyderm.io/@Mara/115575199307766755
2•todsacerdoti•12m ago•0 comments

Launch HN: Mosaic (YC W25) – Agentic Video Editing

https://mosaic.so
1•adishj•13m ago•0 comments

Show HN: Vibe Prolog

https://github.com/nlothian/Vibe-Prolog
3•nl•16m ago•0 comments

Appstinence

https://appstinence.org/
1•indus•19m ago•0 comments

James Watson Saw the True Form of DNA. Then It Blinded Him.

https://www.nytimes.com/2025/11/16/opinion/james-watson-dna.html
2•voxadam•20m ago•2 comments

Deep Dive into FFmpeg 8.0

https://www.rendi.dev/post/ffmpeg-8-0-part-1-using-whisper-for-native-video-transcription-in-ffmpeg
5•dutzi•21m ago•4 comments

Chrome extension permission usage stats

https://chrome-stats.com/permission
1•hao1300•22m ago•0 comments

The Only GM EV1 Ever Publicly Sold, and Where It's Going Next

https://www.theautopian.com/how-the-only-gm-ev1-ever-sold-didnt-get-crushed-and-where-its-going-now/
1•zdw•24m ago•0 comments

How to write prompts for voice AI agents

https://layercode.com/blog/how-to-write-prompts-for-voice-ai-agents
1•mooreds•25m ago•0 comments

Legal Restrictions on Vulnerability Disclosure

https://www.schneier.com/blog/archives/2025/11/legal-restrictions-on-vulnerability-disclosure.html
1•zdw•25m ago•0 comments

Rails, Roads and AI Reporting

https://inconvo.com/blog/rails-roads-and-ai-reporting/
2•ogham•26m ago•0 comments

Microsoft's Agent 365 Wants to Help You Manage Your AI Bot Army

https://www.wired.com/story/microsoft-ai-agent-365/
1•mooreds•26m ago•0 comments

Larry Summers resigns from OpenAI board following release of Epstein emails

https://www.nbcnews.com/tech/tech-news/larry-summers-resigns-openai-board-jeffrey-epstein-emails-...
11•ortusdux•27m ago•0 comments

Can Open-Source AI Introspect?

https://joshfonseca.com/blogs/introspection
1•homarp•28m ago•1 comments

This Ain't Yer Grandaddy's C (Tricks for Writing Gorgeous C)

http://spader.zone/tricks/
1•dboon•30m ago•0 comments

Patent for Hexadecimal Abacus

https://patents.google.com/patent/US4812124A/en
1•jbki•30m ago•0 comments

We're (now) moving from OpenBSD to FreeBSD for firewalls

https://utcc.utoronto.ca/~cks/space/blog/sysadmin/OpenBSDToFreeBSDMove
3•zdw•33m ago•0 comments

Guide to responsible AI implementation in healthcare

https://dimesociety.org/ai-implementation-in-healthcare-playbook/
1•debo_•34m ago•0 comments

Discovery of a Special Type of Immune Cell That Slows Aging in Mice

https://www.nature.com/articles/s43587-025-00953-8
1•stevenjgarner•34m ago•0 comments