frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

1•ramsicandra•11mo 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•11mo 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•11mo 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.

Prepping for the Endgame of the Open Web

https://thehistoryoftheweb.com/prepping-for-the-endgame/
1•Brajeshwar•1m ago•0 comments

Show HN: React-native-Google-maps-plus – Google Maps for React Native

1•pinpong•1m ago•0 comments

MCP Spine – Middleware proxy for MCP with security, routing, and token control

https://github.com/Donnyb369/mcp-spine
1•Mxwell369•2m ago•0 comments

Acute exposure to caffeine improves foraging in an invasive ant (2024)

https://www.cell.com/iscience/fulltext/S2589-0042(24)01157-X?_returnURL=https%3A%2F%2Flinkinghub....
1•bookofjoe•3m ago•0 comments

Discord Read Receipts Exploit: When, How Often, How Long

https://paul.koeck.dev/writeups/discord-read-receipts
1•pauxel•4m ago•0 comments

LaunchWall .Online

1•tamimbuilds•6m ago•0 comments

Nvidia's once-tight bond with gamers is cracking over AI

https://www.cnbc.com/2026/04/18/nvidia-ai-backlash-gamers-geforce-gpu.html
1•Brajeshwar•10m ago•0 comments

InnerZero a local AI assistant where your memory never leaves your machine

https://innerzero.com
1•zotex•11m ago•0 comments

Evaluating Memory Models

https://garden.amcampbell.xyz/Spaced-Repetition/Evaluating-Memory-Models
1•gmays•14m ago•0 comments

Russian-made drones 'disintegrating in the air before reaching their targets'

https://www.tomshardware.com/tech-industry/russian-made-shahed-drones-are-disintegrating-in-the-a...
1•pinewurst•17m ago•0 comments

Recommended GPU Repairshop in Europe (Germany)

1•DogRunner•19m ago•0 comments

Don't Be Evil

https://pluralistic.net/2026/04/11/obvious-terrible-ideas/
1•Klaster_1•19m ago•0 comments

Where Are the Customers' Bots?

https://www.thedrawingboard.net/where-are-the-customers-bots/
1•mooreds•20m ago•0 comments

Another Demo from Boston Dynamics

https://spectrum.ieee.org/boston-dynamics-spot-google-deepmind
1•marc__1•21m ago•0 comments

Germany's Black Forest faces a future of transformation

https://longreads.com/2026/03/12/into-the-darkness/
1•mooreds•21m ago•0 comments

Donut Lab's battery claims reportedly subject of whistleblower complaint

https://www.engadget.com/transportation/donut-labs-battery-claims-reportedly-subject-of-whistlebl...
1•WarmWash•21m ago•1 comments

Lil: A Scripting Language

https://beyondloom.com/decker/lil.html
2•tosh•22m ago•0 comments

Running Qwen 3.6 35B-A3B-4B on MacBook Pro M5 64GB with tools

https://www.youtube.com/watch?v=gJlVjR3G25c
2•elvean•23m ago•0 comments

A New Verdict Rewrites the Rules of Corporate Morality

https://www.nytimes.com/2026/04/17/opinion/lafarge-corporate-terrorism-syria-france.html
1•shrubby•24m ago•0 comments

Notes from the SF Peptide Scene

https://12gramsofcarbon.com/p/notes-from-the-sf-peptide-scene
2•theahura•26m ago•0 comments

Best: "Show HN: Synod–Python tool to use generic BLE wearables as PC macro decks

https://github.com/Abrish-Yir/synod
1•vexki•27m ago•1 comments

The neuroscience of visualization: why it works (but not how you think)

https://twitter.com/jaynitx/status/2045486658938888598
2•gmays•27m ago•0 comments

Generating random numbers using C++ standard library: the problems

https://codingnest.com/generating-random-numbers-using-c-standard-library-the-problems/
1•fanf2•27m ago•0 comments

Who will be the senior engineers of 2035?

https://theengineeringmanager.substack.com/p/who-will-be-the-senior-engineers
1•baranul•29m ago•0 comments

Is BQN Mathematics?

https://mlochbaum.github.io/BQN/commentary/math.html
2•tosh•29m ago•1 comments

Runners vs. robots at China half marathon

https://www.bbc.co.uk/news/videos/cz0e54yrppno
1•mmarian•32m ago•0 comments

Ask HN: How do you share so manny Photos after Trip?

1•gray_wolf_99•32m ago•0 comments

When the cost of verification exceeds the cost of trust

https://twitter.com/shanaka86/status/2045872866944848108
1•toss1•32m ago•0 comments

When moving fast, talking is the first thing to break

https://daverupert.com/2026/04/more-talk-less-grok/
10•Brajeshwar•35m ago•3 comments

Five-year review of BQN design

https://mlochbaum.github.io/BQN/commentary/fiveyears.html
1•tosh•37m ago•0 comments