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.

Hooray for the Sockets Interface

https://blog.apnic.net/2026/07/28/hooray-for-the-sockets-interface/
1•jruohonen•52s ago•0 comments

The BBC Tetris Companion

https://www.leadedsolder.com/2026/07/28/bbc-bridge-companion-part-1-overview.html
1•zdw•1m ago•0 comments

SpaceXAI Sues Minnesota AG ban on nudifying apps

https://gizmodo.com/grok-creator-spacexai-sues-minnesota-ag-to-stop-ban-on-nudification-2000792143
1•pseudosavant•2m ago•0 comments

Show HN: Lean4 Datalog DSL Based on Google Zanzibar for AI Projects

https://github.com/jagg-ix/zil-lean
1•kbradero•4m ago•0 comments

Ask HN: What agentic AI ad optimization tool do you use?

1•smb06•5m ago•0 comments

Show HN: AiTextToHandwriting] – Text to Handwriting Converter

https://aitexttohandwriting.com/
1•zhou594660747•13m ago•0 comments

France Approves Automated, Real-Tim

https://torrentfreak.com/france-approves-automated-real-time-blocking-of-pirate-sports-streams/
1•gslin•14m ago•0 comments

Show HN: SeaTicket – Duplicate Bug Finder for GitHub

2•Daniel-Pan•14m ago•0 comments

Show HN: MegaDSP an effects plugin for DAWs with 50 effects

1•locusofself•17m ago•0 comments

Bartz et al. vs. Anthropic PBC

https://dockets.justia.com/docket/california/candce/3:2024cv05417/434709
1•uneven9434•22m ago•0 comments

Revealed: The Authors Whose Pirated Books Are Powering Generative AI (2023)

https://www.theatlantic.com/technology/archive/2023/08/books3-ai-meta-llama-pirated-books/675063/
2•uneven9434•24m ago•0 comments

The tired you can't explain

https://nathanielfishel.substack.com/p/the-tiredness-you-cant-explain-to
2•bigfish2026•29m ago•0 comments

Satyress "Threehalves Superhumanoid General Purpose Robot"

https://www.satyress.com/
3•King-Aaron•30m ago•3 comments

Chip stocks slide in US and Asia as AI jitters rattle investors

https://www.bbc.com/news/articles/cly8zng43npo
2•yogthos•30m ago•0 comments

Pakistan accused of fatally shooting 30 unarmed protesters in Kashmir

https://www.theguardian.com/world/2026/jul/28/pakistan-accused-firing-unarmed-protesters-kashmir-...
1•Cider9986•30m ago•0 comments

Trump administration bans new Chinese humanoid robots

https://www.bbc.com/news/articles/cp9e2ex3ekyo
9•BIackSwan•32m ago•5 comments

New sw dev tool for safe local AI usage

https://github.com/alexs60/safe-sde
1•alexfg93•32m ago•0 comments

Open AI announces two new models- GPT-transcribe and GPT-live-transcribe

https://developers.openai.com/api/docs/models/gpt-transcribe
1•rochansinha•34m ago•0 comments

LearnVector – Andrew Ng's AI company building one‑to‑one learning experiences

https://learnvector.ai/
14•ajhai•37m ago•6 comments

Autoclaw

https://autoclaw.z.ai/
2•qainsights•39m ago•1 comments

I Built a Free AI Pain Point Miner

https://github.com/joatsaint/pain-point-miner
1•movieman32•40m ago•0 comments

Ask HN: How many AI agents do you actively use?

2•rjpruitt16•43m ago•4 comments

Symbols That Speak Color

https://colorsym.com/
1•mooreds•47m ago•0 comments

LeanScreen: Lean Verification

https://www.millenniumresearch.ai/leanscreen.html
30•Hdjlaf•47m ago•3 comments

Show HN: MD Flow – native macOS/iOS app for browsing and reading Markdown files

https://www.markdownfinder.app/en
2•azambrano•47m ago•0 comments

Modern Software Registries Are a Trust Service

https://redmonk.com/kholterhoff/2026/07/28/modern-software-registries-trust-service/
1•mooreds•48m ago•1 comments

RuntimeWire launches a live dashboard with auditable readership metrics

https://runtimewire.com/article/runtimewire-launches-a-live-dashboard-with-auditable-readership-m...
1•ryanmerket•50m ago•0 comments

Qector v3: Exact parity quantum error correction on a 3GB dual-core edge CPU

https://zenodo.org/records/21501377
1•QECTOR•51m ago•0 comments

Show HN: OpenOTP – Autofill email verification codes on your Mac

https://openotp.app
1•jeninh•54m ago•0 comments

Show HN: DeskDock (Always on Dock) – A tool a treat for Windows productivity

https://sites.google.com/view/deskdock
1•doc-picks-apps•54m ago•1 comments