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.

RC5-72 / Overall Project Stats

https://stats.distributed.net/projects.php?project_id=8
1•macote•3m ago•0 comments

A single DNA cassette tape could store billions of photos

https://www.popsci.com/technology/dna-cassette-data-storage/
1•Tomte•6m ago•0 comments

Radio observations to date find no evidence of technosignature from 3I/ATLAS

https://phys.org/news/2025-12-sensitive-radio-date-evidence-technosignature.html
1•wglb•6m ago•1 comments

Askap discovers an outflow in a nearby galaxy

https://phys.org/news/2025-12-askap-spectacular-outflow-nearby-galaxy.html
1•wglb•7m ago•1 comments

5th floor at stair 9 in building 2 of CS research group at Bell Labs

https://spinroot.com/gerard/img/5th_floor.gif
1•fisheuler•8m ago•0 comments

New York's incoming mayor bans Raspberry Pi at his inauguration party

https://www.theregister.com/2025/12/31/zohran_mamdani_raspberry_pi_ban/
1•linker3000•10m ago•1 comments

Skunk Works Rules [pdf]

https://www.lockheedmartin.com/content/dam/lockheed-martin/aero/photo/skunkworks/kellys-14-rules.pdf
1•dvrp•12m ago•0 comments

Mitt Romney: Tax the Rich, Like Me

https://www.nytimes.com/2025/12/19/opinion/romney-tax-the-rich.html
2•throw0101c•16m ago•2 comments

Tips for Writing a Technical Book

https://borischerny.com/writing/2019/05/26/Tips-For-Writing-A-Technical-Book.html
2•jxmorris12•20m ago•0 comments

I'm rejecting the next architecture PR that uses a Service Mesh for a team of 4

https://old.reddit.com/r/devops/comments/1pzkibf/im_rejecting_the_next_architecture_pr_that_uses_a/
5•ivewonyoung•21m ago•0 comments

Taking photos of PCBs and electronics [video]

https://www.youtube.com/watch?v=W-j-hvUjfJs
1•zdw•21m ago•0 comments

AI-powered skin analysis platform

https://skinadvisor.ai/en
1•nancynguyen98•23m ago•1 comments

We don't need more contributors who aren't programmers to contribute code

https://discourse.llvm.org/t/rfc-llvm-ai-tool-policy-human-in-the-loop/89159
4•pertymcpert•23m ago•0 comments

Ask HN: Looking for an Invite for Lobster.rs

1•willmorrison•25m ago•0 comments

Antibrittle Agents

https://www.southbridge.ai/blog/antibrittle-agents
1•hrishi•25m ago•0 comments

I built a free tool to explore app market data for indie developers and founders

https://appark.ai/
1•xuechen006•31m ago•1 comments

I curated 25GB of video assets so you don't have to use Stock sites

2•BeyondWalk•35m ago•0 comments

You can now submit fraud claims to the IRS online. Before you had to mail a form

https://twitter.com/shl/status/2005621582677622871
1•raybb•38m ago•1 comments

If childhood is half of life, how should that change how we live?

https://moultano.wordpress.com/2025/12/30/children-and-helical-time/
2•moultano•41m ago•1 comments

Play Free Online Games – No Download Needed – MiniTapFun

https://minitapfun.com
2•heihieih•44m ago•0 comments

MTTR-A: Measuring Cognitive Recovery Latency in Multi-Agent Systems

https://arxiv.org/abs/2511.20663
2•PaulHoule•52m ago•0 comments

OpenSSL Performance Still Under Scrutiny

https://www.feistyduck.com/newsletter/issue_132_openssl_performance_still_under_scrutiny
1•todsacerdoti•56m ago•0 comments

L1TF Reloaded

https://github.com/ThijsRay/l1tf_reloaded
5•Fnoord•56m ago•0 comments

I hope generative AI does away with SEO

https://www.pcloadletter.dev/blog/ai-and-seo/
1•ronbenton•56m ago•0 comments

Spectre in the real world: Leaking your private data from cloud with CPU vulns [video]

https://media.ccc.de/v/39c3-spectre-in-the-real-world-leaking-your-private-data-from-the-cloud-wi...
1•Fnoord•58m ago•0 comments

VL-JEPA: Joint Embedding Predictive Architecture for Vision-Language

https://arxiv.org/abs/2512.10942
2•hbarka•1h ago•0 comments

Using AI generated images to get refunds

https://www.wired.com/story/scammers-in-china-are-using-ai-generated-images-to-get-refunds/
1•MattSayar•1h ago•0 comments

Show HN: WizardFlow – Client-side watermarking for Reddit

https://chromewebstore.google.com/detail/wizardflow-image-assistan/hogehefggenldjhcopnpffpgnoepllii
1•jackking1•1h ago•1 comments

Users are required to log in to Bugzilla even to view existing bug reports

https://lists.gnucash.org/pipermail/gnucash-user/2025-September/117417
1•aendruk•1h ago•0 comments

SomaliScan – US Fraud aggregator sourced from public records

https://www.somaliscan.com/
14•sergiotapia•1h ago•3 comments