frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Spider monkeys pool their knowledge to find the best fruit

https://phys.org/news/2026-01-spider-monkeys-pool-knowledge-fruit.html
1•pseudolus•8m ago•0 comments

Show HN: MinimalistNotes – Offline-first notes app, no account, free forever

https://minimalistnotes.com
1•pghqdev•9m ago•1 comments

Ask HN: Books to learn 6502 ASM and the Apple II

3•abkt•11m ago•0 comments

Show HN: Compressor – native Android video compression using Media3

1•JoshAtticus•11m ago•0 comments

From world police to local fortress

https://eurodivergent.bearblog.dev/from-wold-police-to-local-fortress/
2•Dansvidania•13m ago•0 comments

Resend is boring. It's also one of the most exciting products in tech

https://kimmoihanus.com/posts/007-resend-is-boring-also-most-exciting-product-in-tech
2•ihmissuti•13m ago•0 comments

AI Generated Tests as Ceremony

https://blog.ploeh.dk/2026/01/26/ai-generated-tests-as-ceremony/
2•birdculture•14m ago•0 comments

Archyl – The modern platform for C4 model documentation

https://www.archyl.com/
1•eko•15m ago•1 comments

Anthropic launches interactive Claude apps, including Slack, other tools

https://techcrunch.com/2026/01/26/anthropic-launches-interactive-claude-apps-including-slack-and-...
2•pseudolus•15m ago•0 comments

Rebuilding Europe's Sovereignty (Brux Conference)

https://bruxconference2026.com/
2•aiz0Houp•16m ago•0 comments

Why altcoin season may not happen in 2026 as Bitcoin keeps control

https://thebitgazette.com/why-altcoin-season-may-not-happen-in-2026-as-bitcoin-keeps-control/
1•CapricornQueen•17m ago•0 comments

Ask HN: How do teams maintain understanding of large, long-lived codebases?

1•Parbhat-Kapila•17m ago•0 comments

DeepSeek Engram hits 97% on NIAH using DRAM instead of HBM

https://www.techaffiliate.in/blog/deepseek-engram-ai-memory-breakthrough-explained-2026
1•Aditya_kachhawa•18m ago•0 comments

'Ralph Wiggum' loop prompts Claude to vibe-clone commercial software $10/hour

https://www.theregister.com/2026/01/27/ralph_wiggum_claude_loops/
3•pseudolus•20m ago•1 comments

Standard.site – One schema. Every platform

https://standard.site/
1•mefengl•21m ago•0 comments

For the Love of Troff [pdf]

http://www.schemamania.org/troff/for-the-love-of-troff.pdf
1•signa11•23m ago•0 comments

Zszrun Trading Red Flags: When Slippage and Spikes Don't Look Market-Driven

2•Kangaroo_•25m ago•0 comments

I made my own Git

https://tonystr.net/blog/git_immitation
3•TonyStr•28m ago•0 comments

Add custom tools to Claude Code with YAML (no MCP required)

https://github.com/hexcreator/claude-code-extensions
1•xxayh•28m ago•0 comments

Should you restart or cold boot a Mac?

https://eclecticlight.co/2026/01/27/should-you-restart-or-cold-boot/
2•chmaynard•28m ago•0 comments

Technology in 1776

https://www.a16z.news/p/technology-in-1776
2•7777777phil•30m ago•0 comments

Anthropic raised trademark concerns, so Clawdbot ended up renaming

2•necatiozmen•31m ago•3 comments

As Tech Chiefs Woo Trump, Silicon Valley Seethes over Minneapolis Shootings

https://www.nytimes.com/2026/01/26/technology/ice-minnesota-protests-tech.html
1•m-hodges•34m ago•0 comments

Show HN: Actionbase – A database for likes, views, follows at 1M+ req/min

https://github.com/kakao/actionbase
2•em3s•34m ago•1 comments

Subconscious Could Be in Tune with the Solar System

https://www.popularmechanics.com/science/a70114132/moon-affects-humans-minds/
1•keepamovin•34m ago•0 comments

Autonomous Aquatic Robot Is Smaller Than a Grain of Salt

https://www.wired.com/story/autonomous-aquatic-robot-smaller-than-a-grain-of-salt/
2•ColinWright•36m ago•0 comments

Clawdbot forced to rename by Anthropic – GitHub broke, X handle stolen by bots

https://twitter.com/steipete/status/2016068265391354181
1•joozio•38m ago•1 comments

Sam Altman said OpenAI planning to 'dramatically slow down' its pace of hiring

https://www.businessinsider.com/sam-altman-said-openai-plan-dramatically-slow-down-hiring-ai-2026-1
2•tanganik•39m ago•1 comments

Portabase 1.2.3: backup/restore tool, now with MongoDB and a new storages

2•rambokdev•39m ago•0 comments

Show HN: Code Review Lab – Practice spotting vulnerabilities in code

https://www.codereviewlab.com/
5•parasimpaticki•39m ago•2 comments