frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

North Korea's Dystopian Smartphones

https://www.the-express.com/news/world-news/173634/kim-jong-un-bizarre-control-tactics-revealed
1•wjb3•1m ago•0 comments

Russian ICBM Launch Failure

https://twitter.com/liveuamap/status/1994450291740025272
1•defly•3m ago•0 comments

Scientists Just Unlocked Quantum Connections That Reach Across Continents

https://scitechdaily.com/scientists-just-unlocked-quantum-connections-that-reach-across-continents/
2•stOneskull•5m ago•0 comments

Retained vs. Immediate Mode GUI

https://github.com/ocornut/imgui/blob/master/docs/FAQ.md
1•michalsustr•5m ago•0 comments

Tattoo Ink Moves Through Body, Killing Immune Cells, Weakening Vaccine Response

https://www.swissinfo.ch/eng/tattoo-ink-moves-through-the-body,-killing-immune-cells-and-weakenin...
3•CharlesW•6m ago•0 comments

Open Review's Public Data

https://www.openreview.online/
1•sadrasabouri•6m ago•0 comments

Effective harnesses for long-running agents

https://www.anthropic.com/engineering/effective-harnesses-for-long-running-agents
3•diwank•15m ago•0 comments

Control the World for $1

https://www.controltheworld.io
1•hg30•15m ago•0 comments

FluentPrep AI – Practice Toefl Speaking with AI-Powered Feedback

https://www.fluentprep.online/
1•amscotti•17m ago•0 comments

US Energy Department Launches "Genesis Mission" to Transform Science Through AI

https://www.energy.gov/articles/energy-department-launches-genesis-mission-transform-american-sci...
3•sxp•17m ago•0 comments

Juno Neutrino Observatory Releases First Results

https://www.scientificamerican.com/article/juno-neutrino-observatory-releases-first-results/
1•naves•19m ago•0 comments

Show HN: Design a commercial bakery in an afternoon, not for $10k

https://rfmodernbakerydesign.com
2•Rafael_Mauricio•19m ago•0 comments

AWS builds DNS backstop for times flaky US East region fails

https://www.theregister.com/2025/11/27/aws_dns_us_east_workaround/
1•naves•22m ago•0 comments

Airbus issues major A320 recall after flight-control incident

https://www.reuters.com/business/aerospace-defense/airbus-issues-major-a320-recall-after-flight-c...
5•mercenario•22m ago•0 comments

Stopwatch Under the Hood (2016)

https://aakinshin.net/posts/stopwatch/
1•todsacerdoti•23m ago•0 comments

Where Have the International Math Olympiad Gold Medallists Ended Up? Part Three

https://xquant.substack.com/p/where-have-the-international-math-f01
2•paulpauper•24m ago•0 comments

Chartly: Open-Source Odoo Analytics with Agentic AI

https://github.com/AhmadM-DL/oodo-chartly
1•ahmadmmustapha•25m ago•1 comments

Ask HN: How do you verify front-end code in agentic LLM coding loops?

3•eugene-kim•26m ago•0 comments

The first home computer in Europe [video]

https://www.youtube.com/watch?v=aJ6SbvrjxZA
2•megamike•29m ago•0 comments

Why Intel and AMD couldn't play LBP together [video]

https://www.youtube.com/watch?v=hCuT_GXagz0
1•davikr•32m ago•0 comments

Show HN: Local-first RAG for PDF user manuals, datasheets

https://github.com/dymk/askdocs-mcp
1•dymk•33m ago•0 comments

Show HN: Aviation calculators I built while learning

https://joseflys.com
1•jfroma•35m ago•0 comments

DarkVeil – Private, MEV-Resistant Execution Layer on Solana

1•DarkVeil•35m ago•0 comments

Ask HN: How does Hacker News determine which posts appear in the top?

2•s0journed•36m ago•1 comments

Erotic Plasticity and Male Resentment

https://www.robkhenderson.com/p/erotic-plasticity-and-male-resentment
2•paulpauper•36m ago•1 comments

On Seriousness

https://www.punkrockbio.com/p/on-seriousness
2•calvinfo•36m ago•0 comments

Holiday App Festivitas Adds Animated Lights and Snow Effects to Apple Devices

https://www.macrumors.com/2025/11/25/festivitas-holiday-app/
1•akyuu•37m ago•0 comments

Hand-Holding Explained

https://www.betonit.ai/p/hand-holding-explained
1•paulpauper•38m ago•0 comments

Hytale Servers: Everything You Need to Know for Early Access Launch

https://hytaletop100.com/blog/hytale-servers-everything-you-need-to-know-for-early-access-launch
2•doobie12•39m ago•0 comments

I hate Xlib and so should you

https://www.remlab.net/op/xlib.shtml
1•fanf2•39m ago•0 comments