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.

Rapida – an open-source, self-hosted voice AI orchestration platform

https://github.com/rapidaai/voice-ai
2•p_srivastav•2m ago•1 comments

Children with cancer scammed out of millions fundraised for their treatment

https://www.bbc.com/news/articles/ckgz318y8elo
1•1659447091•5m ago•0 comments

Show HN: Atlas – AI YouTube Dubbing and Enhancement

https://chromewebstore.google.com/detail/atlas-ai-youtube-dubbing/ggkjmkjofkcbaobpohgciabgaocebefc
1•gagarwal123•6m ago•1 comments

Crises the world can't ignore in 2026 – The IRC

https://www.rescue.org/article/top-10-crises-world-cant-ignore-2026
1•janandonly•7m ago•0 comments

Stickerbox - AI sticker printer for kids

https://stickerbox.com/
1•cloudking•9m ago•0 comments

Volkswagen to End Production at German Plant, a First in Company History

https://www.nytimes.com/2025/12/16/business/volkswagen-dresden-factory-closed.html
1•erickhill•11m ago•0 comments

Fraud allegations fly as Vancouver startup UpMeals descends into bankruptcy

https://www.ctvnews.ca/vancouver/article/fraud-allegations-fly-as-vancouver-startup-upmeals-desce...
1•foofoo55•11m ago•0 comments

Bonsai: A Voxel Engine, from scratch

https://github.com/scallyw4g/bonsai
1•jesse__•16m ago•0 comments

The Hottest Toy of the Year Is Made by a Tech Startup You've Never Heard Of

https://www.wsj.com/business/entrepreneurship/nex-playground-toy-game-console-holiday-shopping-af...
1•jerezzprime•18m ago•0 comments

Free AI Dictionary Tools

https://aijet.cc
1•osek•19m ago•0 comments

SQLite for VMs: embeddable AI agent sandboxing

https://github.com/boxlite-labs/boxlite
1•dorianzheng•20m ago•0 comments

And Your Little Dog, Too

https://www.newyorker.com/magazine/2025/12/15/and-your-little-dog-too
1•petethomas•22m ago•0 comments

Enhancing LLMs with LoRA – Standardized Recipes for Capability Enhancement

https://huggingface.co/blog/codelion/ellora-lora-recipes
2•codelion•26m ago•0 comments

A former tech executive killed his mother. Family says ChatGPT made her a target

https://www.washingtonpost.com/technology/2025/12/11/chatgpt-murder-suicide-soelberg-lawsuit/
1•1vuio0pswjnm7•29m ago•0 comments

She blew the whistle at Meta. Then her career fell apart

https://www.washingtonpost.com/technology/2025/12/15/big-tech-whistleblowers-speak-out/
2•1vuio0pswjnm7•32m ago•0 comments

Top AI-Tech Startup Ideas for 2026

1•shilpasanny•35m ago•0 comments

How to Write a 21st Century Proof (2011) [pdf]

https://lamport.azurewebsites.net/pubs/proof.pdf
2•User23•36m ago•0 comments

Neil Postman, Informing Ourselves to Death

https://web.archive.org/web/20031029211844/http://www.frostbytes.com/~jimf/informing.html
2•cal_dent•37m ago•1 comments

Live from re:Invent it's Stack Overflow

https://stackoverflow.blog/2025/12/16/live-from-re-invent-it-s-stack-overflow/
1•quapster•39m ago•0 comments

3D Environments from Single Images

https://www.spaitial.ai/
1•yodon•42m ago•0 comments

Show HN: A Claude Code plugin that auto-export plans for multi-model workflows

https://github.com/kenryu42/claude-code-plan-export
1•kenryu•42m ago•0 comments

AI-Augmented Memory for Groups

1•vishal-ds•46m ago•0 comments

Modalz Modalz Modalz

https://modalzmodalzmodalz.com/
3•iamwil•55m ago•0 comments

Netflix Live Origin

https://netflixtechblog.com/netflix-live-origin-41f1b0ad5371
1•mfrw•57m ago•0 comments

Substack forces users to download app to read content

https://twitter.com/gergelyorosz/status/1999241496005066755
4•lleims•58m ago•0 comments

Show HN: The App SEO Pros Are Quietly Switching To

https://www.gitpage.site
1•ambitiouz•58m ago•0 comments

What is more important than working hard?

https://himanshusinghbisht.substack.com/p/what-is-more-important-than-working
1•gilfoyle_7•58m ago•0 comments

Nvidia aquires SchedMD – developer of Slurm HPC scheduling software

https://www.heise.de/en/news/Nvidia-acquires-open-source-provider-SchedMD-11115881.html
2•samuell•58m ago•0 comments

Chinese Name Generator

https://chinesenamehub.com/
1•zidana•1h ago•0 comments

A Simple Recommendation System

https://angelocortez.com/blog/recsys
2•telecomhacker•1h ago•0 comments