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.

Show HN: MeetingIntro, a macOS menu bar app that interrupts you before a meeting

https://github.com/templegit9/MeetingIntro
1•black_observer•6m ago•0 comments

Stripe's August 2026 Investor Letter

https://www.documentcloud.org/documents/28565866-stripes-august-2026-investor-letter/
1•dayve•7m ago•0 comments

Jugwalking: Exercise Your Mind, Your Body, and the Neural Network That Connects

https://davidsmaynard.com/blog/juggle-walking
1•andsoitis•9m ago•0 comments

Gardner police discontinue Flock cameras as license plate readers face scrutiny

https://www.kmbc.com/article/gardner-kansas-flock-cameras-license-plate-readers-privacy/73468724
4•cocacola1•12m ago•0 comments

More tales of App Store curation

https://lapcatsoftware.com/articles/2026/8/9.html
3•cdrnsf•16m ago•0 comments

What makes leaders effective?

https://www.raphaelbauer.com:443/posts/drucker-on-effective-leaders/
1•teleforce•23m ago•0 comments

Remember When Everyone Was Using Solar Energy? (2016)

https://www.nationalgeographic.com/science/article/160225-solar-calculator-history-energy-objects
1•Eridanus2•23m ago•0 comments

Winning a contentious soft fork in 2026

https://getcofund.com/research/what-is-an-economic-node
1•setzeus•24m ago•0 comments

AI-Powered Cyberattacks

https://www.opswat.com/blog/ai-powered-cyberattacks
1•bps1418•25m ago•0 comments

The Arduino UNO Q is an almost perfect Hermes Agent host

1•etoxin•27m ago•0 comments

The Calculus of Value

https://www.oaktreecapital.com/insights/memo/the-calculus-of-value
1•rzk•29m ago•0 comments

AI Hurtles Ahead

https://www.oaktreecapital.com/insights/memo/ai-hurtles-ahead
1•rzk•29m ago•1 comments

Technical leaders should have the largest AI exhaust

https://schipper.ai/posts/technical-leaders-should-have-the-largest-ai-exhaust/
2•schipperai•38m ago•0 comments

I still hand write my commit messages

https://www.jvt.me/posts/2026/08/17/hand-write-commits/
2•birdculture•41m ago•0 comments

NASA estimates the size of the hole SpaceX made in the moon

https://www.theregister.com/offbeat/2026/08/19/nasa-estimates-the-size-of-the-hole-spacex-made-in...
1•Bender•42m ago•0 comments

ASML tries to keep employees until 2030 with €20K stock

https://www.dutchnews.nl/2026/07/asml-staff-get-e20000-in-shares-if-they-stay-until-2030/
2•jbverschoor•43m ago•5 comments

The Point

https://jamie.ideasasylum.com/2026/08/19/the-point
2•mooreds•44m ago•0 comments

NASA's rescue mission for the Swift space telescope has failed

https://www.scientificamerican.com/article/nasa-calls-off-rescue-mission-for-its-falling-swift-sp...
4•sohkamyung•45m ago•0 comments

"Half-Day": 0-Day in the Age of AI

https://margin.re/2026/08/introducing-the-half-day-0-day-in-the-age-of-ai/
1•aaronsdevera•46m ago•0 comments

SvelteKit 3 puts heat on Next.js with approach to RPCs

https://www.theregister.com/devops/2026/08/19/sveltekit-3-puts-heat-on-nextjs-with-radical-approa...
2•Bender•47m ago•0 comments

Drivers are so used to speeding many ignore posted limits, study finds

https://www.freep.com/story/news/local/michigan/2026/08/18/speeding-drivers-traffic-deaths/913503...
3•mandaroVH•50m ago•1 comments

Solving the Flat Cube

https://mathenchant.wordpress.com/2026/08/19/solving-the-flat-cube/
3•jamespropp•51m ago•1 comments

Jason Kelce pleads with NFL fans to send urine to AI data centers

https://www.themirror.com/sport/american-football/jason-kelce-pee-garage-beers-1989324
1•farrisa•51m ago•0 comments

Threshold Schnorr signs in 3ms, threshold ECDSA in 5s

https://808bits.com/articles/the-ecdsa-tax/
1•meehow•52m ago•1 comments

Automated 500 to PR with Hermes and Buzz

https://twitter.com/chadarimura/status/2090208429264826560
3•carimura•53m ago•0 comments

Rick Scott Walked Away from $1.7B Health Care Fraud Case, Then Got a Senate Seat

https://www.uncensoredobjection.com/p/senator-rick-scott-walked-away-from
7•petethomas•58m ago•0 comments

I had to unplug from AI to rediscover my love of writing

https://www.science.org/content/article/i-had-unplug-ai-rediscover-my-love-writing
1•prabal97•59m ago•0 comments

The Future of CSS: Target Multiple Classes with the Class Prefix Selector

https://www.bram.us/2026/08/20/the-future-of-css-target-multiple-classes-with-the-class-prefix-se...
6•cdrnsf•59m ago•1 comments

Digging Out of a Deep Hole: Saving Billions on 125th Street

https://www.etany.org/reports/digging-out-deep-hole-sas-west
3•petethomas•1h ago•0 comments

From Quantum Relative Entropy to the Semiclassical Einstein Equations

https://journals.aps.org/prl/abstract/10.1103/lmq8-nsty
1•dayve•1h ago•0 comments