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.

What makes a good software factory

https://www.robert-glaser.de/what-makes-a-good-software-factory/
1•youngbrioche•2m ago•0 comments

Reverse engineering a vintage diode-transistor logic chip

https://www.righto.com/2026/11/signetics-st680a-dtl-nand.html
1•rbanffy•5m ago•0 comments

AI Is Destroying the Trust Between Writers and Readers

https://www.theatlantic.com/books/2026/11/ai-literary-scams-amazon/689391/
3•FinnLobsien•5m ago•0 comments

Researcher Finds Evidence Chinese AI Labs Used Restricted Nvidia Blackwell GPUs

https://x.com/i/trending/2121003944226767034
1•goatley•6m ago•0 comments

MCP Tax

https://mcp-tax.com/
1•rwilinski•8m ago•0 comments

What do you think about Microsoft future? // Microsoft products down

2•yanikkumar•9m ago•0 comments

The unlikely seaside town on frontline of AI threat

https://www.theguardian.com/business/2026/nov/13/ai-uk-finance-jobs-bournemouth-tech
1•6LLvveMx2koXfwn•11m ago•0 comments

Artex detection signature pack (from IOC with playbook process)

https://blog.malwaremustdie.org/2026/11/mmd-072-2026-artex-detection-signature.html
1•MalwareMustDie•13m ago•0 comments

Perplexity Decider v1.3 and how Perplexity trains decision models

https://www.perplexity.ai/hub/blog/perplexity-decider
1•emschwartz•14m ago•0 comments

Show HN: Trovemarks – Turn your bookmarks into followable collections

https://trovemarks.com/
2•kehers•15m ago•0 comments

Multiple Microst 365 and consumer services are down

https://status.cloud.microsoft/m365/referrer=serviceStatusRedirect
1•brylie•16m ago•0 comments

llama.cpp v1.0

https://github.com/ggml-org/llama.cpp/releases/tag/v1.0.0
2•pvtmert•19m ago•1 comments

Show HN: A Python PoW ledger with post-quantum, vault and agent-allowance

https://github.com/Sajidsaj/mycoin-research
1•SAWANs•24m ago•0 comments

Microsoft 365 Outage EX1550543: What Deferred Email Means for Senders

https://dmarcguard.io/blog/microsoft-outage-ex1550543-email-deferred/
2•meysamazad•24m ago•0 comments

A new page, in a new book

https://0xff.nu/#note:new-page
1•meysamazad•25m ago•0 comments

Bryntum is now open source (scheduling and calendar library)

https://bryntum.com/blog/whats-new-in-v8/
10•greenpinia•27m ago•2 comments

Detour #001: Squirrel Cage Jail

https://cleberg.net/blog/detour-001-squirrel-cage-jail.html
1•meysamazad•27m ago•0 comments

I wrote an app OS for a 4" e-paper fridge board (ESP32-S3)

https://sticky-os.space.fast/blog/introducing-sticky-os/
1•ovidiuliuta•28m ago•1 comments

Embla-1

https://sevren.ai/blog/embla-1
2•Munksgaard•31m ago•0 comments

Rethinking Memory for AI Inference

https://semiengineering.com/rethinking-memory-for-ai-inference/
1•rbanffy•32m ago•0 comments

Piero Scaruffi

https://www.scaruffi.com/
1•fodmap•35m ago•1 comments

Waves that clear waste from the brain during sleep are impaired in older adults

https://news.mit.edu/2026/waste-clearing-waves-in-brain-during-sleep-are-impaired-in-older-adults...
1•geox•39m ago•0 comments

Show HN: Free Online Tools for PDF, Image, Video, Translation and Calculators

https://tooltea.com/
1•psdk1986•41m ago•0 comments

Will the Eurovision Song Contest Succeed in Asia?

https://www.bbc.co.uk/news/videos/cxvg23n59y1ro
1•zeristor•42m ago•0 comments

Show HN: Created an updating list of iPhone Duo apps

https://www.duotoday.com/
1•nashrashal•43m ago•0 comments

Oraculum Research – Towards Autonomous Quantitative Research

https://orarese.com
1•debrahona•44m ago•0 comments

Ask HN: How much value could a peasant derive from frontier AI?

1•jhylands•50m ago•1 comments

Llama.cpp: 6.5x faster prompt processing on Arm with a NEON kernel

https://github.com/hardwiredagent/silicon-forge-arm-neon-study
2•hardwiredagent•51m ago•0 comments

Ask HN: What are you doing now that Claude is down?

6•orbis-tertius•51m ago•6 comments

We had brain hemisphere theory all wrong – Iain McGilchrist [video]

https://www.youtube.com/watch?v=HBOqExZzYsc
1•rendx•51m ago•0 comments