frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Ardalambion – Of the Tongues of Arda, the Invented World of JRR Tolkien

https://www.ardalambion.org/
1•saberhagen•40s ago•0 comments

News Espressif Introduces ESP32-E22, First Wi-Fi 6E Connectivity Co-Processor

https://www.espressif.com/en/news/ESP32_E22_Announcement
1•hasheddan•2m ago•0 comments

Small Kafka: Tansu and SQLite on a free t3.micro

https://blog.tansu.io/articles/broker-aws-free-tier
1•rmoff•4m ago•0 comments

I Forked Google Flatbuffers

https://digitalarsenal.github.io/flatbuffers/
1•tjkoury•4m ago•1 comments

Sony and Tcl Sign Memorandum of Understanding for Strategic Partnership

https://www.sony.co.jp/en/news-release/202601/26-0120E/
1•ksec•11m ago•0 comments

Show HN: Async HTTP handler plugin for the AWS SDK for Ruby, built on async-HTTP

https://github.com/thomaswitt/aws-sdk-http-async
1•thomas_witt•12m ago•0 comments

Show HN: Pikchr.pl – Make Pikchr diagrams using Prolog

https://github.com/exlee/pikchr.pl
2•xlii•12m ago•0 comments

Mars was half covered by an ocean

https://mediarelations.unibe.ch/media_releases/2026/media_releases_2026/mars_was_half_covered_by_...
1•amenghra•17m ago•0 comments

Revolut to Enable Checkout Across Agentic Commerce Platforms

https://www.revolut.com/news/revolut_to_enable_frictionless_checkout_across_all_agentic_commerce_...
1•codingbuddy•18m ago•0 comments

Guix for Development

https://dthompson.us/posts/guix-for-development.html
1•clircle•27m ago•0 comments

AI Adoption Is a Trap

https://dentro.de/ai/blog/2026/01/19/ai-adoption-is-a-trap/
1•myk-e•30m ago•1 comments

Show HN: IP over Avian Carriers with Quality of Service

https://www.rfc-editor.org/rfc/rfc2549.html
1•mig4ng•36m ago•0 comments

Meredith Whittaker – AI Agent, AI Spy

https://www.youtube.com/watch?v=0ANECpNdt-4
1•max_•40m ago•0 comments

Confronted over Greenland Europe is ditching its softly-softly approach to Trump

https://www.bbc.co.uk/news/articles/c0lx7j1lrwro
1•treadump•42m ago•0 comments

I ported the OpenAI Codex review prompts to Gemini CLI

https://github.com/grainier/gemini-cli-codex-reviews
1•grainier•45m ago•1 comments

Software as Fast Fashion

https://tante.cc/2026/01/15/software-as-fast-fashion/
1•pantalaimon•45m ago•0 comments

A static site generator written in POSIX shell

https://aashvik.com/posts/shell-ssg/
1•todsacerdoti•46m ago•0 comments

VidSnoop – browser-based video analyzer (codec checker, metadata viewer)

https://www.vidsnoop.com/
1•androidparanoid•47m ago•1 comments

The Military Is Being Forced to Plan for an Unthinkable Betrayal

https://www.theatlantic.com/ideas/2026/01/us-military-trump-greenland/685677/
11•breve•47m ago•2 comments

My thoughts on Gas Town after 10k hours of Claude Code

https://simonhartcher.com/posts/2026-01-19-my-thoughts-on-gas-town-after-10000-hours-of-claude-code/
1•birdculture•51m ago•0 comments

Show HN: NetNerve AI-powered packet analysis that analyses.cap files

https://www.netnerve.online/
1•bhatshubham•52m ago•0 comments

Beddel Protocol serve as the 'API' for AIs to build secure software for us

https://www.npmjs.com/package/beddel
1•mesenga•54m ago•0 comments

A Personal AI Maturity Model (Paimm)

https://danielmiessler.com/blog/personal-ai-maturity-model
2•alexandrp001•55m ago•0 comments

Building an ECS Golden Path with Claude Code and AWS CDK

https://medium.com/zar-engineering/building-an-ecs-golden-path-with-claude-code-and-aws-cdk-d4a58...
1•obiefernandez•55m ago•0 comments

I'm Addicted to Being Useful

https://www.seangoedecke.com/addicted-to-being-useful/
2•swah•55m ago•1 comments

Show HN: Gemini-live-react – Real-time voice AI that works in the browser

1•loffloff•57m ago•0 comments

Sony's TV business is being taken over by Tcl

https://www.theverge.com/news/864263/sony-tcl-tv-business-partnership-takeover-announcement
5•phantomathkg•1h ago•0 comments

I built a typing practice tool because typing tests never helped me improve

1•amitgajbhare•1h ago•0 comments

Show HN: Leta – Command-line LSP client for agentic coders

https://github.com/andreasjansson/leta
1•fagerhult•1h ago•0 comments

Computer-Using Agents Are Transforming Lead Data Research

https://www.louisamayhanrahan.com/p/ai-advancements-in-data-research
1•louisamayh•1h ago•0 comments