frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Compute price/performance is flat. Now what?

1•Animats•1m ago•0 comments

Show HN: I built a simple web server with ACME support

https://github.com/cozis/BlogTech
1•cozis•2m ago•0 comments

Thoughts on AI intimacy features: the real risk isn't ethics – it's rollout pace

1•OracleKitten•3m ago•0 comments

Are these AI prompts damaging your thinking skills?

https://www.bbc.com/news/articles/cd6xz12j6pzo
1•01-_-•5m ago•0 comments

Starlink satellite just exploded and left 'trackable' debris

https://www.engadget.com/science/space/a-starlink-satellite-just-exploded-and-left-trackable-debr...
1•ljf•14m ago•0 comments

Trump Media and TAE Technologies unite in a high-stakes mega fusion deal

https://comuniq.xyz/post?t=635
2•01-_-•19m ago•0 comments

Show HN: Contextual Engineering – Patterns for Building Offline-First AI Agents

https://github.com/tflux2011/contextual-engineering-patterns
1•tflux3011•21m ago•1 comments

Girl Names That Were Originally More Popular for Boys (2021)

https://www.scarymommy.com/girl-names-formerly-boy-names
1•Tomte•22m ago•0 comments

Waymo temporarily suspends service in SF amid power outage

https://www.sfgate.com/bayarea/article/waymo-temporarily-suspends-service-sf-amid-power-21254917.php
2•drfuchs•23m ago•1 comments

Das Flugsimulator 5 Buch

https://www.thomas-huehn.com/das-flugsimulator-5-buch/
1•Tomte•28m ago•0 comments

Gallery of Babel

https://www.echohive.ai/gallery-of-babel/
1•ustad•29m ago•0 comments

Glory (Optical Phenomenon)

https://en.wikipedia.org/wiki/Glory_(optical_phenomenon)
1•thunderbong•31m ago•0 comments

Profile of Josh Woodward, head of Google Labs that turned around Gemini app

https://www.cnbc.com/2025/12/20/josh-woodward-google-gemini-ai-safety.html
2•thoughtpeddler•35m ago•0 comments

Inca Stone Masonry

https://www.earthasweknowit.com/pages/inca_construction
3•jppope•44m ago•0 comments

What features can we add to a black hole?

2•DinakarS•45m ago•0 comments

Clair Obscur has 'Game of the Year" title stripped away over AI use

https://www.thegamer.com/clair-obscur-expedition-33-indie-game-awards-goty-stripped-ai-use/
5•anigbrowl•55m ago•0 comments

Mochi: New window insulation blocks heat, but not your view

https://www.colorado.edu/today/2025/12/11/new-window-insulation-blocks-heat-not-your-view
3•bluehat974•55m ago•0 comments

Ruby's Website Got an Overhaul

https://www.ruby-lang.org/en/
12•psxuaw•1h ago•1 comments

Show HN: Free Roblox GamePlay Video Generator Tool (Inreels.ai)

https://www.inreels.ai/tools/free-roblox-video-maker
2•Onekiran•1h ago•0 comments

Driving Xiaomi's Electric Car: Are We Cooked? [video]

https://www.youtube.com/watch?v=Mb6H7trzMfI
2•baxtr•1h ago•0 comments

How the Resistors Put Computing into 1960s Counter-Culture

https://spectrum.ieee.org/teenage-hackers
2•oldnetguy•1h ago•0 comments

Weight-loss jabs: What happens when you stop?

https://www.bbc.com/news/articles/cn98pdpyjz5o
3•notepad0x90•1h ago•0 comments

Chinese surveillance based on US tech is used to silence Tibetans in Nepal

https://apnews.com/article/china-tibet-nepal-surveillance-technology-silicon-valley-eadac8211c5d0...
6•aspenmayer•1h ago•1 comments

New study reveals that AI cannot write like a human

https://www.ucc.ie/en/news/2025/new-study-reveals-that-ai-cannot-fully-write-like-a-human.html
3•giuliomagnifico•1h ago•0 comments

Stop Losing Intent: Absent, Null, and Value in Rust

https://minikin.me/blog/presence-rs
3•lukastyrychtr•1h ago•0 comments

Show HN: Unrag (open-source) – Add RAG as source files, not dependencies

https://unrag.dev
2•tanaylakhani•1h ago•0 comments

"Koyunbaba Op. 19" (Carlo Domeniconi) Performed by Julia Schüler

https://www.youtube.com/watch?v=xIheg9EM8xM
2•ofalkaed•1h ago•0 comments

Nvidia X Palantir, Global Surveillance, "Pre-Crime" Arrests, & AI [Video]

https://www.youtube.com/watch?v=5lYsO4k7OIY
3•IlikeKitties•1h ago•0 comments

The hidden cost of video-call glitches

https://www.nature.com/articles/d41586-025-03820-z
2•XzetaU8•1h ago•0 comments

Elon Musk's SpaceX bought millions worth of Cybertrucks Tesla can't sell

https://electrek.co/2025/12/18/elon-musks-spacex-bought-tens-of-millions-worth-of-cybertrucks-tes...
7•MilnerRoute•1h ago•0 comments