frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

I use Bubbles to find the quirky stuff

https://dshbx.de/blog/bubbles/entry/c23ac55e-edf3-43c4-af91-f27ea51d7d3a
1•ZacnyLos•1m ago•0 comments

GeoParquet Downloader for QGIS

https://github.com/cholmes/qgis_plugin_gpq_downloader
1•marklit•2m ago•0 comments

Show HN: SleekNav – A hand-reviewed web directory, free to submit

https://sleeknav.com/
1•songtianlun1•3m ago•0 comments

Ask HN: Help to lower AWS bill: CloudFront requests for files that don't exist

https://james.cridland.net/blog/2026/aws-requests-help/
1•DamonHD•4m ago•1 comments

Headless table or ready-made data grid?

https://comparegrids.com/articles/article-headless-table-or-ready-made-grid
1•davidvela•8m ago•0 comments

A computer that outlived its manufacturer by 30 years

https://amiga.kexperiments.com/
1•doener•9m ago•0 comments

Andy Burnham launches campaign against 'headphone dodgers' on public transport

https://www.theguardian.com/politics/2026/oct/19/andy-burnham-launches-campaign-against-headphone...
1•theanonymousone•11m ago•0 comments

Hyperoptimized Engineer (2026) [video]

https://www.youtube.com/watch?v=MhEL9cvLxQE
1•supriyo-biswas•13m ago•0 comments

Accessible Components Library

https://tetralogical.com/blog/2026/10/14/accessible-components-library/
1•mfru•13m ago•0 comments

TamperAudit – Tamper-evident audit logs for AI agents (EU AI Act Article 12)

https://tamperaudit.com
2•leajoy•15m ago•0 comments

Zotac RTX 5090 graphics cards worth $470k disappear at Brazilian airport

https://videocardz.com/newz/two-pallets-of-zotac-rtx-5090-graphics-cards-worth-470000-disappear-a...
1•TMWNN•17m ago•1 comments

Fritz Schumacher: The Man Who Built Modern Hamburg

https://alsterrunde.com/fritz-schumacher-the-man-who-built-modern-hamburg/
1•mertbio•18m ago•0 comments

Why Is Kunio Yanagita Called the Father of Yokai Folklore?

https://www.thecollector.com/kunio-yanagita-yokai-folklore/
1•rdmuser•23m ago•0 comments

CanI.RootMy.TV?

https://cani.rootmy.tv/
2•gregsadetsky•31m ago•1 comments

Persistence Identity and the Possibility of Artificial Culture

https://vlattko.substack.com/p/persistence-identity-and-the-possibility
1•vlad-asis•37m ago•0 comments

OpenCode and OpenAI issue refunds after security breach of OpenCode console

2•indy•40m ago•0 comments

Ask HN: What are you working on? (October 2026)

2•tha_infra_guy•43m ago•1 comments

Writing is no longer proof of work

https://www.seangoedecke.com/writing-is-no-longer-proof-of-work/
5•gpi•50m ago•0 comments

Agent-Skills.market

https://agent-skills.market/
15•agentsmithvl•53m ago•9 comments

Perplexity offers free reverse engineering credits

https://twitter.com/AskPerplexity/status/2111120664543596957
4•tomcam•55m ago•1 comments

Tmux and Claude Code: Sessions, Send-Keys, and the Ceiling

https://crystl.dev/guides/tmux-for-ai-agents/
2•ankitg12•56m ago•0 comments

Composite objects and C++26 the std:polymorphic type

https://mariusbancila.ro/blog/2026/10/18/composite-objects-and-cpp26-the-stdpolymorphic-type/
2•jandeboevrie•57m ago•0 comments

Anthropic and the Changing Nature of Work

https://surfingcomplexity.blog/2026/10/18/anthropic-and-the-changing-nature-of-work/
2•azhenley•1h ago•0 comments

Answering ASL questions: Argon leads a 21-question pilot

https://huggingface.co/datasets/salqadri/argon-asl-question-understanding-eval
1•salqadri•1h ago•0 comments

Amelia Earharts Plane search expedition's Taraia Object hypothesis fails

https://www.purdue.edu/newsroom/2026/Q4/search-for-amelia-earharts-plane-continues-after-taraia-o...
3•fork-bomber•1h ago•0 comments

Nushell – A New Type of Shell

https://www.nushell.sh/
1•emmettbt•1h ago•2 comments

Show HN: Open Mailto Links in Gmail

https://sindresorhus.com/mailway
2•mofle•1h ago•1 comments

An Artist Who Built Japanese Animation

https://animationobsessive.substack.com/p/an-artist-who-built-japanese-animation
1•vinhnx•1h ago•0 comments

I made a combined snake-tron game using free AI

https://steeltoe777.github.io/snakegame/
2•loopz•1h ago•1 comments

Show HN: The Lion King (SNES, 1994), running as a native PC program

https://github.com/kinduff/TLKRecomp
3•kinduff•1h ago•1 comments