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.

Rise in violence against women journalists and activists linked to digital abuse

https://apnews.com/article/un-women-report-rise-violence-online-66c38bb80b79d64be18b477f209c2db0
1•binning•1m ago•0 comments

Self Vaping Vape Machines

https://indiantinker.bearblog.dev/adding-more-smoke-to-your-projects/
1•indiantinker•2m ago•0 comments

A Lisp Interpreter Implemented in Conway's Game of Life

https://woodrush.github.io/blog/posts/2022-01-12-lisp-in-life.html
1•fanf2•2m ago•0 comments

Critical design flaw in women's running shoes, scientists warn

https://www.sciencefocus.com/news/critical-problem-womens-running-shoes
1•binning•3m ago•0 comments

Geonimo – AI Geo/SEO Agent

https://www.geonimo.com/
1•guikioky•5m ago•0 comments

Are we stuck with the same Desktop UX forever? [video]

https://www.youtube.com/watch?v=1fZTOjd_bOQ
1•todsacerdoti•8m ago•0 comments

Show HN: Svelte Bash – A lightweight terminal component for Svelte 5

https://github.com/YusufCeng1z/svelte-bash
1•yusufcengiz•8m ago•0 comments

NanoGPT is the first LLM to train and run inference in space

https://twitter.com/AdiOltean/status/1998769997431058927
1•mirzap•8m ago•0 comments

Why do ARM chips have an instruction with JavaScript in the name (FJCVTZS)?

https://stackoverflow.com/questions/50966676/why-do-arm-chips-have-an-instruction-with-javascript...
1•birabittoh•12m ago•0 comments

$262M stolen in account takeover fraud schemes this year, FBI says

https://therecord.media/millions-in-account-takeover-fbi-warns-ahead-of-holidays
1•PaulHoule•14m ago•0 comments

AI Essay Writer for Your Academic Success

https://essaypass.ai
1•osek•16m ago•0 comments

Scientists Propose a Physical Warp Drive

https://darkmatter.website/article/scientists-propose-a-physical-warp-drive/
1•darkmatternews•18m ago•0 comments

Show HN: Built a Global Dating App in 100 Days Using Cursor ($20/Mo)

https://www.wctokyoseoul.com/ko
1•abuskorea•20m ago•0 comments

Looking for a website thats more modern than Hacker News?

https://www.upvote.social
1•upvotenow•25m ago•0 comments

Show HN: A free derivative calculator with step-by-step explanations

https://derivativecalculatortool.online
1•maxnew•27m ago•1 comments

Devgraph

https://devgraph.ai
1•bellamoon544•28m ago•1 comments

Show HN: A Go client for GitHub Models (simple, fast, MIT licensed)

https://github.com/tigillo/githubmodels-go
1•maaydin•29m ago•1 comments

Google DeepMind partners with UK gov to support security in AI era

https://deepmind.google/blog/strengthening-our-partnership-with-the-uk-government-to-support-pros...
2•newsuser•29m ago•0 comments

Kimrio357 Gmail.com

https://github.com/kamranahmedse/developer-roadmap
1•kimrio•33m ago•0 comments

CanIUse Embed

https://caniuse-embed.vercel.app/
1•moebrowne•33m ago•0 comments

Tired of fake AI emotions, so I wrote code to measure its actual "Entropy Loss"

https://silicon-pain-index.streamlit.app/
1•IkanRiddle•37m ago•2 comments

Rust 1.92.0

https://blog.rust-lang.org/2025/12/11/Rust-1.92.0/
3•amalinovic•38m ago•0 comments

Orbit – AI Conversation Archive

https://orbitarchive.com/
1•richarddeetlefs•39m ago•0 comments

Show HN: ClipFlow – Free clipboard history for macOS (like Windows Win+V)

https://github.com/praneeth552/clipflow
1•SaiPraneeth1•45m ago•0 comments

The Asahi Illusion

https://www.futilitycloset.com/2025/12/12/the-asahi-illusion/
1•beardyw•46m ago•0 comments

Harper: AI-powered surf forecast platform built by surfers

https://harper.surf
1•fbenevides•47m ago•0 comments

Ditching PostgreSQL, 10M+ Users Web3 Analytics

https://www.velodb.io/blog/why-ave-ai-chose-velodb-over-clickhouse-snowflake-for-web3-analytics
1•qinchencq•48m ago•0 comments

Layers of Meaning

https://claude.ai/share/dee4a3eb-ca54-4d83-9450-b9c270102ba5
2•stoicfungi•56m ago•2 comments

Greenlandic women's victory in legal fight with Denmark over forced IUD scandal

https://www.theguardian.com/world/2025/dec/11/greenlandic-women-claim-victory-in-legal-fight-with...
3•binning•57m ago•0 comments

The Open Web Index

https://openwebindex.eu/
1•bilegeek•57m ago•0 comments