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.

Interactive Trillion Packet Anonymized Network Analysis with the GraphBLAS

https://ieeexplore.ieee.org/abstract/document/11196571
1•teleforce•38s ago•0 comments

Ask HN: Credit card and loyalty analytics, where to start?

1•scipio1551•40s ago•0 comments

Septaro, an AI-powered weekly reset to help you pursue your goals

https://septaro.com
1•buildforwardco•1m ago•0 comments

Newly discovered Bacteria uses Acetone for food

https://phys.org/news/2026-11-arctic-ocean-bacteria-acetone-carbon.html
1•silver92bullet•1m ago•0 comments

I rebuilt my earthquake tracker from scratch

https://klaxon.live/en
1•Accher•3m ago•0 comments

Agentic Network Traffic Monitoring

https://arxiv.org/abs/2609.32778
1•teleforce•5m ago•1 comments

Why Isn't Retatrutide Approved Yet?

https://lettersfrombethlehem.substack.com/p/why-isnt-retatrutide-approved-yet
2•Sniffnoy•8m ago•0 comments

OpenAI spent $1.2M in API cost to fix 948 Linux vulnerabilities

https://twitter.com/OpenAI/status/2118119451401048497
1•sidcool•9m ago•0 comments

The $18B Contingent Workforce Machine

https://burbank-ledger.pages.dev/articles/manpowergroup-experis-corporate-finance-audit/
1•measurablefunc•9m ago•0 comments

The White T-Shirt Experiment

https://whiteteelab.com/
1•Numerlor•10m ago•0 comments

Learnings from Auditing Public Evaluations

https://www.primeintellect.ai/blog/auditing-public-evaluations
1•gmays•16m ago•0 comments

Flims and TV shows created using AI have now reached a high level of quality

https://crateh.com/en
1•haebom•18m ago•0 comments

Magic Layers – Split a flat image into named, editable layers

https://magiclayers.net
1•dotcurio•25m ago•0 comments

Infinite Slop

https://infiniteslop.ai/
1•jdkee•32m ago•0 comments

Visual Positioning System for Trees

https://twitter.com/DjimoSerodio/status/2118157383369396289
2•MetaFarmer•34m ago•1 comments

Order of Adjectives

https://dictionary.cambridge.org/grammar/british-grammar/adjectives-order
1•thunderbong•37m ago•0 comments

Does Pretraining Teach Chemical Language Models Chemistry?

https://arxiv.org/abs/2610.18651
1•yz-exodao•37m ago•0 comments

How does a rocket engine work?

https://www.readfp.com/chapters/how-a-rocket-engine-works
2•shaikhmubin•37m ago•0 comments

Show HN: REMO – Free 4D web network connecting 24 cities with ad-free live radio

https://remoworld.com/
1•and11155•38m ago•0 comments

Transcribe Video to Text – Transcribe Video to Text Online Free

https://videotranscriber.ai/
1•Lemarie•38m ago•0 comments

CVS to Split Off Drugstore Business, Ending Its Bigger-Is-Better Era

https://www.nytimes.com/2026/11/04/business/cvs-health-split.html
1•ChrisArchitect•39m ago•1 comments

Show HN: A table view for all your Trello cards

https://trello.com/power-ups/6ae62312ef3825846d6e2499/table-view-%E2%9D%A4%EF%B8%8F
2•flunhat•40m ago•0 comments

Get answers from what you save

https://notedge.app
1•measurablefunc•43m ago•0 comments

I tricked decision models into thinking they were steering a real trolley

https://www.brennanmceachran.com/trolley-tests/
3•brennanm•46m ago•1 comments

Hacker News Classics (2018)

https://jsomers.net/hn/
3•dsalzman•56m ago•0 comments

Show HN: Edgepad – Turn an Android phone into a trackpad for your laptop

https://github.com/akshit-bansal11/edgepad
1•akshit-bansal11•1h ago•0 comments

TB 26-09 Measured Hammer-Tap Inputs

https://www.nasa.gov/centers-and-facilities/nesc/tb-26-09-measured-hammer-tap-inputs/
1•titusblair•1h ago•0 comments

The New York Times sues Brave web browser for AI scraping and bypassing paywalls

https://www.theverge.com/tech/1024588/brave-lawsuit-ai-scraping-new-york-times
1•erickhill•1h ago•0 comments

Asteroid defense hits a lump in the road

https://newsreleases.sandia.gov/asteroid-defense-hits-a-lump-in-the-road/
1•titusblair•1h ago•0 comments

Kimi K3.1 Pro's open weights: 1.56 TB, MXFP4, and a $20M revenue license line

https://theainewsreport.com/2026-11-04-kimi-k3-1-pro-open-weights-license-hardware-explained.html
1•titusblair•1h ago•0 comments