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.

Volcanoes that made history

https://knowablemagazine.org/content/article/physical-world/2026/volcanoes-that-made-history
2•flabber•22s ago•0 comments

Mark Cuban, worth more than $10B, has a bold plan to tackle wealth inequa

https://economictimes.indiatimes.com/news/international/global-trends/mark-cuban-worth-more-than-...
1•newsomix9xl•41s ago•1 comments

Supermicro Super AI Station (GB300, 20 Pflops FP4, 748GB Coherent Memory)

https://www.supermicro.com/en/accelerators/nvidia/super-ai-station
2•peter_d_sherman•1m ago•0 comments

French firms & Rheinmetall targeted by China's export controls

https://www.intelligenceonline.com/asia-pacific/2026/08/25/three-french-firms-targeted-by-beijing...
1•alephnerd•1m ago•0 comments

It's Been 20 Years Since Astronomers Decided Pluto Isn't a Planet

https://www.cnet.com/science/space/20-years-since-astronomers-decided-pluto-isnt-a-planet/
1•speckx•5m ago•0 comments

Headlong: A Microharness for Persistent Agents

https://www.laude.org/updates/headlong-a-microharness-for-persistent-agents
3•lbw1215•15m ago•0 comments

AI algorithm for room temperature superconductor, candidate detected

https://zenodo.org/records/22073633
1•tsagaansuld•17m ago•0 comments

Ask HN: What's the current state of Quantum Vulnerability

1•vednig•17m ago•0 comments

'Normal' research trap limits EU scientific breakthroughs, disruptive innovation

https://www.researchgate.net/publication/408700233_A_%27normal%27_research_trap_limits_scientific...
1•musha68k•21m ago•0 comments

Decentralized Universal Computer

https://github.com/Skills03/universal-computer
4•RIshabh235•22m ago•1 comments

Ask HN: Good large format (>20 inches) touchscreen E-Paper display options?

5•foota•24m ago•0 comments

Self host sheets, docs, drive, email

1•smalltorch•25m ago•0 comments

Show HN: JSON Support – Free JSON and CSV to Excel Conversion Without Signups

https://www.jsonsupport.com/
1•Sharanxxxx•37m ago•0 comments

Have We Forgotten How to Design?

https://lmnt.me/blog/have-we-forgotten-how-to-design.html
3•maguay•40m ago•0 comments

Amazon EC2 Beta (20 years ago)

https://aws.amazon.com/blogs/aws/amazon_ec2_beta/
2•jeffbarr•42m ago•0 comments

Sleepwalker: Passive Backdoor with Its Own Command Language

https://r136a1.dev/2026/08/24/sleepwalker-a-passive-backdoor-with-its-own-command-language/
3•defrost•43m ago•0 comments

Show HN: Safepine OS – A Quantitative Finance Engine in D Language (LGPL)

https://safepine.co/
1•safepine•47m ago•0 comments

How to Design an Animation

https://kciter.so/posts/how-to-design-animation/en/
2•kciter•55m ago•1 comments

Show HN: How to Fish Wiki – Bosses, Fish and Guides

https://how-to-fish-game.wiki
1•light001•56m ago•0 comments

Right-Sized Language Model

https://joecooper.me/blog/cpuhaiku/
2•thatjoeoverthr•59m ago•0 comments

NYC's 911 System Had an Outage. Officials Kept Quiet About It

https://www.nytimes.com/2026/08/24/nyregion/mamdani-911-outage-nyc.html
4•ChrisArchitect•1h ago•2 comments

SiFive's First Server Platform

https://chipsandcheese.com/p/sifives-first-server-platform
2•geerlingguy•1h ago•0 comments

Nvidia Is Spending $6B to Build a Powerful U.S. Alternative to Chinese AI

https://www.wsj.com/tech/ai/nvidia-is-spending-6-billion-to-build-a-powerful-u-s-alternative-to-c...
5•Charles_Wdy•1h ago•0 comments

Show HN: Probability calibration that doesn't flatten your scores

https://github.com/finite-sample/calibre
1•neehao•1h ago•0 comments

The dementia vaccine some of us are taking without realising

https://www.sciencefocus.com/the-human-body/dementia-vaccine-taking-without-realising
4•bananamogul•1h ago•1 comments

Cursor Router chooses the right model for the task

https://cursor.com/blog/how-cursor-router-works
3•gmays•1h ago•0 comments

Thomson Reuters Standard for High Stakes AI

https://www.thomsonreuters.com/en-us/posts/innovation/thomson-reuters-standard-for-high-stakes-ai/
1•andsoitis•1h ago•0 comments

Don't take your subagent's word for it

1•Marvin_RunAI•1h ago•0 comments

IBM Z and LinuxONE Dual-ISA Processor and AI Acceleration at Hot Chips 2026

https://www.servethehome.com/ibm-z-and-linuxone-dual-isa-processor-and-ai-acceleration-at-hot-chi...
2•fork-bomber•1h ago•0 comments

Show HN: Aquifer – Admission control for bursty vLLM traffic

https://github.com/rjpruitt16/aquifer
1•rjpruitt16•1h ago•0 comments