frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Iran Is Hunting Down Starlink Users to Stop Protest Videos from Going Global

https://www.wsj.com/world/middle-east/iran-is-hunting-down-starlink-users-to-stop-protest-videos-...
2•Agreed3750•5m ago•2 comments

CLI's completion should know what options you've typed

https://hackers.pub/@hongminhee/2026/optique-context-aware-cli-completion
2•dahlia•5m ago•0 comments

D2TS – Differential Dataflow in TypeScript (Used by TanStack DB and ElectricSQL)

https://github.com/electric-sql/d2ts
1•jpereira•8m ago•0 comments

House GOP unveils new legislation on stock trading ban

https://thehill.com/homenews/house/5684843-house-gop-stock-trading-ban/
5•1659447091•17m ago•0 comments

Uber faces bellwether assault trial in federal court in Arizona

https://www.reuters.com/sustainability/society-equity/uber-faces-sexual-assault-trial-arizona-tha...
1•1vuio0pswjnm7•19m ago•1 comments

Agent Safety is a [bounded] Box

https://brooker.co.za/blog/2026/01/12/agent-box.html
1•honorable_coder•20m ago•1 comments

F-22 Raptor Is Getting So Many Upgrades It Will Fly Until 2060

https://www.19fortyfive.com/2026/01/the-f-22-raptor-stealth-fighter-is-getting-so-many-upgrades-i...
2•Gaishan•22m ago•0 comments

Ask HN: AGI Suicide?

2•mudil•25m ago•1 comments

Meta unveils 'Meta Compute' initiative to build AI infrastructure

https://www.reuters.com/technology/meta-build-gigawatt-scale-computing-capacity-under-meta-comput...
1•haberdasher•25m ago•0 comments

Anthropic Shipped Cowork in 10 Days Using Its Own AI

https://karozieminski.substack.com/p/claude-cowork-anthropic-product-deep-dive
3•zenonBz•34m ago•0 comments

Powell vows to stand firm against 'unprecedented' administration threats

https://www.pbs.org/newshour/politics/watch-powell-vows-to-stand-firm-against-unprecedented-trump...
3•JumpCrisscross•35m ago•0 comments

Database Development with AI in 2026

https://www.brentozar.com/archive/2026/01/database-development-with-ai-in-2026/
2•gmays•40m ago•0 comments

Show HN: A custom little core CPU architecture with a unique pipeline design

https://github.com/futureisAJASU/Architecture
2•AJASU•40m ago•0 comments

Show HN: ToolsAid – A privacy-first developer utilities hub built with Golang

https://toolsaid.com/
2•raihaninfo•49m ago•0 comments

Eureka launches two robotic vacuum cleaners and a floor-steamer stick vacuum

https://www.notebookcheck.net/Eureka-launches-two-robotic-vacuum-cleaners-and-a-floor-steamer-sti...
1•akg130522•55m ago•0 comments

Show HN: I built an image-to-3D tool optimized for 3D printing and game asset

https://www.imgto3d.ai
2•stewardyunn•1h ago•2 comments

Seeing Geologic Time: Exponential Browser Testing

https://tjid3.org/paper/time
1•TimothyMJones•1h ago•3 comments

What I'd do if I was 18 again

https://www.aadillpickle.com/blog/18-again
1•aadillpickle•1h ago•0 comments

DataOlllo: Private AI Data Analyst

https://apps.microsoft.com/detail/9nc4vdmwgxd8?hl=en-US&gl=US
1•olllo•1h ago•1 comments

Aliasing Alias

https://jordaneldredge.com/aliasing-alias/
2•Fudgel•1h ago•0 comments

Gavin Newsome moves to neutralize tax on billionaires

https://www.politico.com/news/2026/01/12/gavin-newsom-moves-neutralize-tax-billionaires-00723633
11•RickJWagner•1h ago•1 comments

Signal and WhatsApp Now Working on T-Mobile Satellite (Starlink)

https://cascadialink.com/confirmed-signal-and-whatsapp-now-working-on-t-mobile-satellite-starlink/
1•mrdeke•1h ago•0 comments

The marula and elephant intoxication myth

https://pmc.ncbi.nlm.nih.gov/articles/PMC10581541/
1•thunderbong•1h ago•0 comments

Byu Talk about Miracles

https://speeches.byu.edu/talks/matthew-cowley/miracles/
1•marysminefnuf•1h ago•0 comments

Meta admits to Instagram password reset mess, denies leaks

https://www.theregister.com/2026/01/11/infosec_news_in_brief/
1•maxloh•1h ago•1 comments

Show HN: Selfhosted – One click self hosted apps

https://github.com/zdunecki/selfhosted
2•zdunecki•1h ago•0 comments

Show HN: PrivateLink – Stop TikTok and others from embedding your info in links

https://private-link.com
7•huppp•1h ago•0 comments

I Try to Be Kind

https://hedgehogreview.com/web-features/thr/posts/why-i-try-to-be-kind
2•herbertl•1h ago•0 comments

Surface optimization governs the local design of physical networks

https://www.nature.com/articles/s41586-025-09784-4
2•dboreham•1h ago•0 comments

PawSense: Catproof Your Computer

http://bitboost.com/pawsense/
15•zdw•1h ago•4 comments