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.

'Really Simple Licensing' (RSL) – Open Licensing Standard for AI Crawlers

https://en.wikipedia.org/wiki/Really_Simple_Licensing
1•evolve2k•13m ago•0 comments

The AI Transformation Framework

https://zapier.com/playbooks/ai-transformation-framework
1•swolpers•15m ago•0 comments

Shifting Security Left for AI Agents with GitGuardian MCP

https://blog.gitguardian.com/shifting-security-left-for-ai-agents-enforcing-ai-generated-code-sec...
1•umairnadeem123•15m ago•0 comments

Feature-Sliced Design

https://feature-sliced.design/
1•saikatsg•16m ago•0 comments

High Speed Rail by Country 2026

https://worldpopulationreview.com/country-rankings/high-speed-rail-by-country
1•thunderbong•18m ago•0 comments

Trend Is Concerning

https://techcrunch.com/2026/02/26/jack-dorsey-block-layoffs-4000-halved-employees-your-company-is...
1•melvinodsa•19m ago•0 comments

Show HN: I built a Chrome extension to record demo videos without editing

https://zoomflow.rovelin.com/
1•hritik7742•25m ago•0 comments

Judge says he will order Greenpeace to pay $345M in oil pipeline case

https://apnews.com/article/greenpeace-energy-transfer-dakota-access-pipeline-30bfb9939dea06f1e976...
1•e2e4•32m ago•0 comments

Model Collapse Ends AI Hype

https://www.youtube.com/watch?v=ShusuVq32hc
3•signa11•34m ago•0 comments

Research suggests mating direction bias between Neanderthals and humans

https://www.theguardian.com/science/2026/feb/26/male-neanderthals-human-females-mating-research-d...
2•uxhacker•34m ago•1 comments

Seeing Is Not Believing: Benchmarking AI Image Detectors

https://blog.succinct.xyz/ai-image-detection-benchmark/
1•ncb9094•35m ago•0 comments

Pakistan bombs targets in Afghan cities, minister calls it 'open war'

https://www.reuters.com/world/asia-pacific/pakistan-strikes-afghanistan-targets-clashes-intensify...
1•petethomas•36m ago•0 comments

How Stupid Would It Be to Put Data Centers in Space?

https://spectrum.ieee.org/orbital-data-centers
1•amaks•38m ago•1 comments

Upload 23andMe,myHeritage, Myancestry, get 1,200 GRS score and a longevity prot

1•HelixSequencing•39m ago•0 comments

Dear Time Lords: Freeze Computers in 1993

https://graydon2.dreamwidth.org/322461.html
5•zdw•47m ago•0 comments

Reduce Claude Token Usage by 50%

https://ham-pro.vercel.app/
2•Luseniik•47m ago•1 comments

Making Video Games in 2025 (without an engine)

https://www.noelberry.ca/posts/making_games_in_2025/
2•alvivar•48m ago•0 comments

How do you evaluate a person's ability to use AI?

3•imwoody•50m ago•1 comments

Secure-Massively-Multiplayer-Snake

https://eieio.games/blog/secure-massively-multiplayer-snake/
2•colinprince•51m ago•0 comments

Claude-search – grep, resume your Claude Code session history from the CLI

https://github.com/pi-netizen/claude-search
2•pi-netizen•51m ago•1 comments

Under Secretary of Defense Emil Michael Response to Dario Amodei

https://xcancel.com/uswremichael/status/2027211708201058578?s=12
4•grej•53m ago•2 comments

Green Eggs and HAM (Hierarchical Agent Memory)

https://github.com/kromahlusenii-ops/ham
3•Luseniik•54m ago•1 comments

Energym: Move the World

https://www.aicandy.be/giorgio-1
2•zdw•56m ago•0 comments

Tonka beans are illegal in the US

https://www.bbc.com/future/article/20170620-the-delicious-flavour-with-a-toxic-secret
5•valzevul•57m ago•0 comments

OpenClaw Hype: Analysis of Chatter from the Dark Web (BleepingComputer)

https://www.bleepingcomputer.com/news/security/the-openclaw-hype-analysis-of-chatter-from-open-so...
2•ildar•1h ago•0 comments

Any Website Can Take Full Control of Your OpenClaw Agent (Oasis)

https://www.oasis.security/blog/openclaw-vulnerability
1•ildar•1h ago•0 comments

Show HN: Cecil – open-source memory and identity protocol for AI

https://github.com/johnkf5-ops/cecil-protocol
1•JohnKnopf•1h ago•1 comments

New AirSnitch attack breaks Wi-Fi encryption in homes, offices, and enterprises

https://arstechnica.com/security/2026/02/new-airsnitch-attack-breaks-wi-fi-encryption-in-homes-of...
1•brie22•1h ago•1 comments

Show HN: HeadshotAI – Professional AI headshots in 30 seconds for £4.99

https://headshotai-mvp-jb.netlify.app
1•jackbventures•1h ago•3 comments

China piles pressure on Japan after Takaichi Sanae's triumph

https://www.economist.com/asia/2026/02/26/china-piles-pressure-on-japan-after-takaichi-sanaes-tri...
3•petethomas•1h ago•0 comments