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.

Viral stardom saves 'Trump' buffalo from sacrifice in Bangladesh

https://www.france24.com/en/asia-pacific/20260527-viral-stardom-saves-trump-buffalo-from-sacrific...
1•mooreds•19s ago•0 comments

Will AI Break the University?

https://rorytruex.substack.com/p/will-ai-break-the-university
1•Michelangelo11•1m ago•0 comments

Simple systems are the best systems

https://jerodsanto.net/2026/05/simple-systems-are-the-best/
1•mooreds•2m ago•0 comments

Most Americans Aren't Convinced Humans Are Causing Climate Change New Data Shows

https://www.forbes.com/sites/maryroeloffs/2026/05/28/most-americans-arent-convinced-humans-are-ca...
1•mooreds•3m ago•0 comments

Show HN: React-Rewrite – A visual editor for React that writes code, no LLM

https://github.com/donghaxkim/react-rewrite
1•donghaxkim•7m ago•0 comments

The Shrinking Synthesis: a 2037–2047 window for AI's institutional reformation

https://papers.ssrn.com/abstract=6731418
1•danielz-p•7m ago•0 comments

Show HN: Use Kimi and OpenAI Subscriptions in Claude Code

https://github.com/raine/claude-code-proxy
1•rane•7m ago•0 comments

Show HN: 500 years of Joseon court omens as an observability dashboard

https://ajin.im/is/building/omen.ops/
1•poppypetalmask•8m ago•0 comments

The Shell University

https://www.chronicle.com/article/the-shell-university
1•Michelangelo11•13m ago•0 comments

Grifters, cynics, and true believers: The family tree of vaccine opponents

https://arstechnica.com/science/2026/05/grifters-cynics-and-true-believers-the-family-tree-of-vac...
2•rbanffy•17m ago•0 comments

2D Particles sandbox using WebGPU

https://2sandbox.tulv.in/
1•atulvi•18m ago•0 comments

Bubbles: From "tronics" to "dot com" (1999)

https://www.forbes.com/1999/01/14/mu3.html
3•jumploops•18m ago•1 comments

Impossibly rare tropical bird swooped over California. Experts fear a trend

https://www.sfgate.com/california/article/rare-tropical-species-southern-california-22282620.php
2•c420•18m ago•0 comments

Show HN: leaf – one month later: website, releases and lots of improvements

https://leaf.rivolink.mg
1•RivoLink•22m ago•0 comments

Boston Mystery Boom

https://www.wcvb.com/article/did-you-feel-it-viewers-report-hearing-mystery-boom-across/71450011
2•benwhitehead21•23m ago•0 comments

Possible meteor/bolide atmospheric entry east of Boston

https://twitter.com/NStewWX/status/2060792766997655903
2•twoodfin•24m ago•0 comments

Meta is reportedly developing an AI pendant

https://techcrunch.com/2026/05/30/meta-is-reportedly-developing-an-ai-pendant/
2•rishikeshs•26m ago•0 comments

In Pursuit of Understanding the Crash in Me/CFS

https://likeannopeningbandforthesun.substack.com/p/in-pursuit-of-understanding-the-crash
1•brandonb•26m ago•0 comments

Fluid Fire Simulation Sandbox

https://www.escapemotions.com/experiments/fluid_fire_3/
2•Leftium•27m ago•0 comments

Development and validation of a digital biomarker for peripheral artery disease

https://www.nature.com/articles/s41746-026-02655-w
1•brandonb•27m ago•0 comments

Compare AI Model Pricing Across 9 Providers (385 Models)

https://silkdock.ai/rankings
3•aqib-public•27m ago•1 comments

Ernst & Young published cybersecurity report full of hallucinations

https://gptzero.me/investigations/ey
26•smartmic•28m ago•9 comments

Thiel moves family to Milei's libertarian Argentina

https://www.ft.com/content/3d7ab893-1842-4c6c-a3d9-26871d79dde4
3•mmarian•29m ago•1 comments

How Does Google Maps Work? [video]

https://www.youtube.com/watch?v=kS-CGkiPetQ
1•tacon•31m ago•0 comments

People Keep Damaging Garbage Trucks by Throwing Car Batteries in the Trash

https://www.thedrive.com/news/people-keep-throwing-away-car-batteries-in-dumpsters-and-thats-a-re...
3•01-_-•31m ago•0 comments

What the OSS Summit Says About OSS in 2026

https://redmonk.com/sogrady/2026/05/19/oss-summit-2026/
1•mooreds•32m ago•0 comments

United flight diverted to Wisconsin after passenger's attempts to breach cockpit

https://www.cnn.com/2026/05/30/us/united-airlines-unruly-passenger-wisconsin
2•mooreds•32m ago•1 comments

SoftBank pledges €75B to build Europe's biggest AI facility in France

https://www.ft.com/content/1022f9bd-5b6d-44a5-9303-c8b05b8c6463
2•bazzmt•32m ago•1 comments

Getting changes from FusionAuth theme updates

https://fusionauth.io/community/forum/topic/3105/getting-changes-from-theme-updates
1•mooreds•33m ago•0 comments

DHS says it's 'drawing up plans' to ban international arrivals at SFO

https://www.sfgate.com/travel/article/dhs-ban-international-arrival-sfo-22280740.php
5•consumer451•34m ago•2 comments