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.

RayforceDB – a pure C analytics database with a Lisp-like syntax

https://rayforcedb.com/
1•sourdecor•1m ago•0 comments

Show HN: Self-bench – build SWE-bench style evals from private repos

https://github.com/mupt-ai/self-bench
2•byhong03•1m ago•0 comments

AI: The Devil on My Shoulder

https://myrkvi.no/ai-the-devil-on-my-shoulder/
1•speckx•2m ago•0 comments

Deferred Token Response

https://datatracker.ietf.org/doc/draft-gerber-oauth-deferred-token-response/
1•mooreds•2m ago•0 comments

Hours of Jingling Keys [video]

https://www.youtube.com/watch?v=sJNK4VKeoBM
1•mooreds•3m ago•0 comments

Show HN: Interactive Product Demos made easy

https://www.rendemo.com
1•jakegargaro17•4m ago•0 comments

AI by Hand

https://www.byhand.ai/
1•sans_souse•4m ago•0 comments

LLM Inference Benchmarking

https://www.digitalocean.com/blog/llm-inference-benchmarking
1•ankitg12•4m ago•0 comments

V-RAE: Rethinking Video Latent Spaces for Generation

https://arxiv.org/abs/2608.13556
2•selimonder•6m ago•0 comments

Why Claude's Watermarking Won't Fix Anything

https://www.plagiarismtoday.com/2026/08/13/why-claudes-watermarking-wont-fix-anything/
1•speckx•7m ago•0 comments

IC 1101 is the largest galaxy identified, so far, in the universe

https://disassociated.com/ic-1101-largest-galaxy-identified/
1•Brajeshwar•8m ago•0 comments

The Jobless Boom Has Arrived

https://www.wsj.com/economy/jobs/the-jobless-boom-has-arrived-41361a06
2•donsupreme•8m ago•0 comments

Dreams, Reflections, and Inceptions

https://papercompute.com/blog/dreams-on-paper/
2•eigenBasis•8m ago•0 comments

Reviving the Famicom Network System – Throaty Mumbo

https://www.youtube.com/watch?v=xiyCKUl93Uo
2•mrinfinite•9m ago•0 comments

Federal government finalizes ownership reporting exemption for US firms

https://www.reuters.com/legal/government/trump-administration-finalizes-ownership-reporting-exemp...
5•anigbrowl•10m ago•0 comments

Hypothetical Document Embeddings

https://arxiv.org/abs/2212.10496
2•kaycebasques•11m ago•0 comments

Ask HN: What are your unfinished projects?

2•adam_gyroscope•11m ago•0 comments

I bought the most expensive cable I could find. It still died

https://medium.com/@pokhts/i-bought-the-most-expensive-cable-i-could-and-it-still-died-welcome-to...
2•PhilYeh75•12m ago•0 comments

Show HN: Online SNMP MIB database - upload/view your own MIBs

https://mib-viewer.com/
2•beefstew123•14m ago•0 comments

"Solving a largely imaginary user goal"

https://unsung.aresluna.org/solving-a-largely-imaginary-user-goal/
4•euthymiclabs•14m ago•0 comments

Is AI Coding Effecting Our Mental Health? [video]

https://www.youtube.com/watch?v=iPUn1Fnfn0k
2•torartc•14m ago•0 comments

The Java Story – The official documentary [video]

https://www.youtube.com/watch?v=ZqGSg4b_cZA
2•kimi•15m ago•0 comments

Show HN: Eve Software Factory Template

https://github.com/vercel-labs/eve-software-factory-template
2•flashbrew•15m ago•0 comments

A Plane Ran Out of Fuel over the Atlantic. The Pilots Saved 306 Lives

https://www.popularmechanics.com/flight/a73413706/air-transat-flight-fuel-glider/
3•ilamont•17m ago•0 comments

Agentic engineering optimizes for rejecting output, not generating it

https://dparkmit.substack.com/p/how-i-used-agentic-engineering-to
2•dparkmit•19m ago•0 comments

Google Is Making Private AI Practical with Homomorphic Encryption

https://blog.google/security/how-google-is-making-private-ai-practical-with-homomorphic-encryption/
2•u1hcw9nx•20m ago•2 comments

Taylor Farms' connections to Trump admin spurs probe into Cyclospora response

https://arstechnica.com/health/2026/08/taylor-farms-connections-to-trump-admin-spurs-probe-into-c...
3•cratermoon•20m ago•0 comments

Qwen 3.8 27B Pelican

https://www.reddit.com/r/LocalLLaMA/comments/1voa3ch/comment/p3o0om9/
2•T0mSIlver•21m ago•0 comments

Activation Energy is a good model for a lot of things

https://homosabiens.substack.com/p/activation-energy-is-a-good-model
2•surprisetalk•23m ago•0 comments

The Other Sean Byrne Doesn't Exist

https://conic.al/writing/the-other-sean-byrne-doesnt-exist/
3•seanieb•24m ago•0 comments