frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Lot of insights: The biggest mistakes I've made with Lunch Money – Jen

https://lunchbag.ca/lunch-money-mistakes/
1•Mehuleo•2m ago•0 comments

Show HN: Incus-Sandbox-SDK

https://github.com/divyenduz/incus-sandbox-sdk
1•divyenduz•3m ago•1 comments

Higher daylight exposure improves cognitive performance, study finds

https://www.manchester.ac.uk/about/news/higher-daylight-exposure-improves-cognitive-performance-s...
1•XzetaU8•4m ago•0 comments

AI can now 'see' optical illusions. What does it tell us about our own brains?

https://www.bbc.com/future/article/20251218-how-ai-is-shedding-new-light-on-optical-illusions
1•vinni2•8m ago•0 comments

Phind Is Shutting Down

http://phind.com/
1•deepdude•9m ago•0 comments

Elon Musk says saving for retirement is irrelevant: 'It won't matter'

https://finance.yahoo.com/news/elon-musk-says-saving-retirement-221451637.html
1•quantified•10m ago•1 comments

Claude Cowork first impression: Cowork Deleted 11GB of files [video]

https://www.youtube.com/watch?v=_6C9nMvQsGU
1•virgildotcodes•11m ago•0 comments

Detecting MIME Types in Go

https://rnemeth90.github.io/posts/2024-03-27-golang-detect-file-type/
1•todsacerdoti•11m ago•0 comments

QGIS – a geographic information system software that is free and open-source

https://www.qgis.org/
1•merusame•14m ago•0 comments

A 3D-printed watch-cleaning machine

https://adventuresinamateurwatchfettling.com/2025/04/11/a-3d-printed-watch-cleaning-machine/
1•tagawa•17m ago•0 comments

Atlas: An Analytical Topology of Logic, Aims, and Structure

https://zenodo.org/records/18226093
1•takko_the_boss•17m ago•0 comments

A Plea for Silicon Valley to Enter Politics

https://loeber.substack.com/p/30-a-plea-for-silicon-valley-to-enter
1•barry-cotter•22m ago•1 comments

Show HN: A script that turns 365 photos into a square year poster

https://github.com/0plus1/oneyear
1•0plus1•29m ago•0 comments

Missed Connections NYC [video]

https://www.youtube.com/shorts/anRJ_aQzfRQ
2•low_tech_punk•29m ago•0 comments

Bohemian Tragedy: the SF-based society may have logged its old-growth forest

https://archive.vanityfair.com/article/share/895a393a-52cc-46f5-a055-404e91c291bf
2•wahnfrieden•31m ago•0 comments

OpenCode-Container

https://git.jeena.net/jeena/opencode-container
1•jeena•32m ago•1 comments

List of Claude Skills, resources, and tools for customizing Claude AI workflows

https://github.com/ComposioHQ/awesome-claude-skills
2•saikatsg•32m ago•0 comments

For indie hackers: Outsource marketing or do it yourself?

https://amplift.ai/
3•gaygum102•37m ago•2 comments

Dev Browser: A browser automation plugin for Claude Code

https://github.com/SawyerHood/dev-browser
1•saikatsg•37m ago•0 comments

Isaac Newton on Learning Mathematical Thinking and Reasoning

https://learningloom.substack.com/p/isaac-newton-on-learning-mathematical
2•atomicnature•37m ago•0 comments

Unpopular Opinion: Bootstrap is a better front-end framework than Tailwind

2•pyeri•38m ago•1 comments

Planning on Claude: Tips

https://skeltoac.substack.com/p/planning-on-claude
1•skeltoac•39m ago•0 comments

Playwriter: Playwright MCP with 80% less context window

https://github.com/remorses/playwriter
2•saikatsg•40m ago•0 comments

Experience Ceremonial Grade Matcha by Opure

https://opureaustralia.com.au/products/matcha-powder
1•zafarali125•41m ago•0 comments

Every Pill I Took: 2000 – 2001

https://www.counter-print.co.uk/products/every-pill-i-took
3•colinprince•42m ago•0 comments

Rapid morphological change in an urban bird due to Covid-19 restrictions

https://www.pnas.org/doi/10.1073/pnas.2520996122
2•WaitWaitWha•44m ago•0 comments

Magical moment: The enormity of the first U.S. adult heart transplant (2018)

https://med.stanford.edu/stanmed/2018winter/fifty-years-since-first-us-adult-heart-transplant-sta...
2•petethomas•49m ago•1 comments

Europe vs. America: the West at War with Itself [video]

https://www.youtube.com/watch?v=rkUhQq3iPOo
1•dgudkov•50m ago•0 comments

PHP in 2026

https://stitcher.io/blog/php-2026
4•brentroose•58m ago•0 comments

Incus 6.20

https://discuss.linuxcontainers.org/t/incus-6-20-has-been-released/25635
1•indigodaddy•59m ago•0 comments