frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

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.

Attitudes toward same-sex marriage and transgender issues are shifting

https://apnews.com/article/gallup-poll-same-sex-marriage-morality-e12acb151446ac1b7970c0825bf1d072
1•petethomas•1m ago•0 comments

Shopify Theme Detector,What Shopify Theme Is This?

https://storetrace.link/tools/theme-detector
1•bitvvip•4m ago•0 comments

Lawsuit Seeks to Stop the UFC Fight on the White House South Lawn

https://apnews.com/article/white-house-ufc-fight-lawsuit-trump-birthday-da95554d7137ca297dd47951a...
2•petethomas•7m ago•0 comments

Teenage Engineering: Introducing APC-2

https://teenage.engineering/products/apc-2
13•vthommeret•8m ago•0 comments

Ask HN: What are the best examples of good technical writing you know of?

2•CSMastermind•8m ago•0 comments

Making Software – Image Compression

https://www.makingsoftware.com/chapters/image-compression
2•Brajeshwar•12m ago•0 comments

Pleasant Reader – a cozy reader mode for Chrome/Brave – (Chrome extension)

https://github.com/rajveerb/pleasant-reader
1•rajveerb•12m ago•0 comments

7.8 magnitude earthquake shakes part of southern Philippines. Tsunami possible

https://www.yahoo.com/news/weather-news/articles/as--philippines-earthquake-001322726.html
8•mikhael•13m ago•1 comments

Bayesian Thinking for Toddlers: The Cartoon

https://www.bayesianspectacles.org/bayesian-thinking-for-toddlers-the-cartoon/
2•jruohonen•13m ago•0 comments

Tech Worker Ethical Concerns Research Project

1•jonleuen•16m ago•0 comments

last30days-skill

https://github.com/mvanhorn/last30days-skill
1•pykello•18m ago•0 comments

Ask HN: What's good for VR these days, free and paid

3•razorbeamz•22m ago•2 comments

How Gambling Turned Your Feed into Slop

https://www.youtube.com/watch?v=Lw6gpYJzujE
4•flakespancakes•29m ago•0 comments

How does anybody test phone applications that do location tracking?

https://twitter.com/LyleMakes/status/2063784301594853657
1•lylejantzi3rd•30m ago•2 comments

Atlas: Metadata-first proof chains for critical digital actions

https://github.com/rodriguezaa22ar-boop/atlas-trust-infrastructure/discussions/85
2•astra_omnia•33m ago•0 comments

I asked a few hundred builders how they decide it's time to launch

https://nicklaunches.com/blog/week-5-recap-june-07-2026/
1•nicklaunches•34m ago•0 comments

Against Taste

https://minutes.substack.com/p/against-taste
3•el_benhameen•40m ago•0 comments

Parallel Intelligence and Cognitive Warfare

https://jackson-t.com/parallel-intelligence-and-cognitive-warfare/
2•jthuraisamy•41m ago•0 comments

EMILIAProtocol-an open standard for human sign-off on irreversible agent actions

https://www.emiliaprotocol.ai/spec
2•EmiliaStar•42m ago•0 comments

Does the Web Use DNSSEC and Email Authentication? Scanning TopM Domains

https://www.pwndefend.com/2026/06/06/email-and-domain-security/
1•882542F3884314B•54m ago•0 comments

JavaNext Language Features

https://www.youtube.com/watch?v=fFocVFEe2TI
2•birdculture•57m ago•0 comments

Opus 4.8 Part 2: Model Welfare

https://thezvi.wordpress.com/2026/06/01/opus-4-8-part-2-model-welfare/
1•gmays•57m ago•0 comments

Show HN: Dense NYT style crossword puzzle generation with Rust and Claude

https://github.com/ekorbia/xword-pipeline
2•ekorbia•59m ago•0 comments

Self-Healing Materials: Mechanisms, Properties, and Applications

https://www.mdpi.com/2227-9717/14/9/1436
3•PaulHoule•1h ago•0 comments

Ask HN: What Happened to Hide?

2•what•1h ago•2 comments

Congress wants to tie the United States to Israel with this new legislation

https://www.theguardian.com/commentisfree/2026/jun/05/congress-us-israel-legislation
7•kldavis4•1h ago•0 comments

lab leak straight out of 2080 - time travel in html 5

https://rogmash.neocities.org/torusfield
1•rogmash•1h ago•1 comments

Show HN: Synstax – Smart Supplement Scheduling for iPhone

https://synstax.uppervoid.app/
1•reloading2008•1h ago•0 comments

The sneaky way companies get new chemicals into our food

https://www.npr.org/2026/05/29/nx-s1-5836552/fda-gras-food-chemicals-additives-rfk
5•nkzednan•1h ago•1 comments

BetterSSC is a Discord-style client for Substack Chat

https://github.com/inder/betterssc
1•inderrr•1h ago•1 comments