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.

Dipolar Geometry of the Primes and ζ(s) – 42-Module Lean4 Formalization

https://github.com/airoman/riemann-dipolar-geometry
1•airomanxr•1m ago•0 comments

LinkedOut – a browser extension to fix LinkedIn jobs

https://github.com/ibizabroker/linkedout
1•makizuto•1m ago•1 comments

HumanFish: Predicting Who Wins a Human Chess Game

https://humanfish.vercel.app/methodology
1•marc__1•3m ago•0 comments

Razar and Janax = LUMA Vera (Razonamiento Artificial)

https://nicoconvoz.github.io/Janax/
1•nicoconvoz•6m ago•0 comments

Show HN: Turkey Trot

https://turkey-trot.replit.app/
1•rootforce•10m ago•1 comments

Show HN: Glassveil – Browse and Copy iPhone Photos on a Windows PC over Wi-Fi

https://glassveil.app
1•ftwelve•11m ago•0 comments

Buster the hamster home safe after hitching ride in Halloween pumpkin

https://www.cbc.ca/news/canada/ottawa/buster-the-hamster-home-safe-after-hitching-ride-in-hallowe...
3•madihaa•11m ago•0 comments

Show HN: Track people in video locally without AGPL or cloud APIs

https://github.com/obendidi/trackflow
1•ouail_bendidi•13m ago•1 comments

Ask HN: What's Going on with the Minecraft Rust Rewrite? (Horizon)

2•goodmythical•14m ago•1 comments

Show HN: Shirudo.today: Virtual eBPF patching on stock Linux

https://shirudo.today/blog/launch.html
5•avrecum•18m ago•0 comments

Token River – About the Record

https://tokenriveralbum.com/about
1•mayank•20m ago•1 comments

Ryanair quietly changes seating policy for passengers who do not pre-book seat

https://www.irishtimes.com/life-style/travel/2026/11/23/ryanair-quietly-changes-seating-policy-fo...
1•Markoff•23m ago•0 comments

Sleep Is Death was ahead of its time (2018)

https://www.eurogamer.net/sleep-is-death-was-ahead-of-its-time
1•bpierre•26m ago•0 comments

I asked Siri to team up with Claude and read me the news

https://automato.substack.com/p/siri-plus-claude-plus-shortcuts
1•andrewstetsenko•34m ago•0 comments

Temporal.io: Durable Execution Against Distributed Chaos

https://srdata.dev/en/escritos/temporal-io-orquestacion-ejecucion-durable.html
1•bastiates•34m ago•0 comments

Thoughts on the Future of Web Browsers

https://sarahjamielewis.com/log/2026/future-of-web-browsers.html
1•birdculture•37m ago•0 comments

UK to launch first licensing scheme for knife sales

https://www.theguardian.com/uk-news/2026/nov/25/uk-knife-sales-licensing-scheme
1•baal80spam•37m ago•1 comments

Peano Arithmetic cannot determine BB(173) thanks to Quine's New Foundations

https://twitter.com/ElliotGlazer/status/2126047123288322548
2•ogogmad•39m ago•0 comments

3DGS Training in <10 Seconds

https://electronicarts.github.io/perf-gs/
1•nozbufferHere•39m ago•0 comments

You are mad at AI for the wrong reasons

https://benedictide.com/posts/ai/
2•paulpauper•39m ago•0 comments

Global lake synchrony signals rising planetary-scale ecological coupling

https://www.pnas.org/doi/10.1073/pnas.2611874123
2•geox•39m ago•0 comments

Novita, StreamLake, etc. are ByteDance Ark wrappers

https://kendell.dev/blog/arkwrapper/
3•leafingensemble•42m ago•0 comments

Facebook is wrong, text is deathless (2016)

https://kottke.org/16/06/facebook-is-wrong-text-is-deathless
1•rajeev1123•43m ago•0 comments

Nvidia's 'great man theory' is flawed

https://www.ft.com/content/6a01bdc9-f242-4272-b5e3-a147af5b125c
4•bookofjoe•45m ago•1 comments

Mozilla/Firefox-MCP

https://github.com/mozilla/firefox-mcp
1•super256•47m ago•0 comments

Why is every agent orchestrator the exact same app?

https://kevinak.se/blog/3602153a-3f9c-4ce0-8a4b-fc8e0abceb4f
2•kevinak•50m ago•0 comments

"Kid in the Cockpit” (1994)

https://en.wikipedia.org/wiki/Aeroflot_Flight_593
1•abbadadda•53m ago•2 comments

Should I Buy a Gun?

https://robbiewmthompson.com/blog/buying-a-gun/
1•paulpauper•53m ago•2 comments

AI made self-hosting fun again

https://janlukas.blog/posts/ai-self-hosting
56•jlelse•54m ago•35 comments

What Is the Point of Mountains

https://wadapan.substack.com/p/all-souls-exam
1•paulpauper•54m ago•0 comments