frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Lessons learned from rebuilding a 19-year-old platform in one week with Claude

https://gist.github.com/janit/6559e97ceb00e444b9aecb3c00dfdf16
1•velmu•39s ago•0 comments

Show HN: CoThou v0.2 – Autonomous Superagent

https://cothou.com/
1•MartyD•4m ago•0 comments

DHS demands Google, Reddit, Meta, Discord provide data of users criticizing ICE

https://www.reddit.com/r/politics/s/DR5OaH9CV1
1•wahnfrieden•5m ago•1 comments

I am not joining OpenAI

1•Johnyjohnson123•6m ago•0 comments

Physicist: After 33 billon years, universe 'will end in a big crunch'

https://news.cornell.edu/stories/2025/10/physicist-after-33-billon-years-universe-will-end-big-cr...
1•danny00•6m ago•0 comments

Local AI Assistant for Linux That Runs Commands

https://github.com/zkzkGamal/zkzkAgent
1•Zakaria_Gamal•10m ago•0 comments

Show HN: OpenSustain.Analytics

https://opensustain-analytics.streamlit.app/
1•protontypes•10m ago•0 comments

Show HN: Speed Reader – Chrome extension for RSVP speed reading

https://chromewebstore.google.com/detail/speed-reader/iifbkjcdekfokhpjbiabfgjlloabpdlp
1•GlebShalimov•10m ago•0 comments

Olympic Curling - Super Smash Curling

https://github.com/newell-paul/super-smash-curling
1•paul_n3•14m ago•2 comments

Yates v. United States (2015)

https://en.wikipedia.org/wiki/Yates_v._United_States_(2015)
1•greyface-•15m ago•0 comments

Building Technical Cathedrals for an Audience That No Longer Visits Webs

https://en.andros.dev/blog/58441321/building-technical-cathedrals-for-an-audience-that-no-longer-...
1•andros•15m ago•0 comments

Unitree's Kung Fu Robot Demostration During the Chinese New Year Gala [video]

https://www.youtube.com/watch?v=R40IDdAkRZM
2•zaikunzhang•20m ago•0 comments

Show HN: I made an affiliate site that tracks sales with coupons, not cookies

https://refwaffle.com
1•jakemanger•21m ago•0 comments

Defensive Publication: A $0 Alternative to Patents for Bootstrapped SaaS

1•patentailab•24m ago•1 comments

Programming is no longer the main skill of SWE

https://edwardbx.com/articles/programming-skill/
1•crassus_ed•27m ago•0 comments

Share your core values with Claude Codd every time

https://github.com/albertnahas/claude-core-values
1•albertnahas•34m ago•1 comments

HN is an echo chamber of AI wrappers. Moving to Slashdot

4•mekod•42m ago•2 comments

AI is destroying open source, and it's not even good yet

https://www.youtube.com/watch?v=bZJ7A1QoUEI
1•liberostelios•42m ago•0 comments

Show HN: Toice – Local voice typing for Linux using Whisper

https://github.com/harshsharma2455/Toice
1•zherotz•44m ago•0 comments

Show HN: Game Engine in Julia with 400KB Exports (Vs Unity's 200MB)

https://github.com/sinisterMage/Open-Reality
3•MrNova•46m ago•2 comments

A passionate defense of the M Pro series of chips

https://birchtree.me/blog/a-passionate-defense-of-the-m-pro-series-of-chips/
3•ingve•48m ago•0 comments

Why AI writing is so generic, boring, and dangerous: Semantic ablation

https://www.theregister.com/2026/02/16/semantic_ablation_ai_writing/
2•KnuthIsGod•48m ago•0 comments

Site Runs on 30GB of RAM

https://nebezb.com/30gb-ram/
3•nebezb•51m ago•1 comments

Password managers less secure than promised

https://ethz.ch/en/news-and-events/eth-news/news/2026/02/password-managers-less-secure-than-promi...
1•sschueller•54m ago•0 comments

Prototyping UIs has always been a good fit for vibe coding

https://twitter.com/geoffreylitt/status/2023589705246200289
1•tosh•54m ago•0 comments

Apax pulls out of Pinewood Technologies takeover after AI scare

https://www.thetimes.com/business/companies-markets/article/apax-pinewood-technologies-bid-ai-fqq...
1•petethomas•54m ago•0 comments

Rust-CAS

https://github.com/ibkvictor/Rust-CAS
1•ibk_victor•56m ago•0 comments

Product Lock – A JSON spec that defines what your AI-built product is and isn't

https://github.com/nickjazz/product-lock
1•nickjazz1998•56m ago•0 comments

Bacterial strain from 5k-year-old cave ice has resistance against antibiotics

https://phys.org/news/2026-02-bacterial-strain-year-cave-ice.html
2•janandonly•57m ago•1 comments

Show HN: Free SEO Crawler and Analyze App

https://seodisias.com
1•aligundogdu•58m ago•0 comments