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.

Great Plains Shelterbelt (1934 geoengineering project)

https://en.wikipedia.org/wiki/Great_Plains_Shelterbelt
1•oliculipolicula•4m ago•0 comments

Show HN: Ear Training Exercise

https://clarena.app/app/try
1•AlexFromClarena•4m ago•0 comments

Thinking Hard Above AI

https://bmbumpus.com/2026/09/16/thinking-hard-above-ai/
1•mathgenius•5m ago•0 comments

We Urgently Need a New Major Search Engine

https://www.vincentschmalbach.com/we-urgently-need-a-new-major-search-engine/
1•vincent_s•6m ago•0 comments

Shakespeare Skill: Make agents sound like William Shakespeare characters

https://github.com/cookiengineer/shakespeare-skill
1•cookiengineer•8m ago•1 comments

My Chinese Internet Is Talking About AI Doom

https://yinwei.substack.com/p/my-chinese-internet-is-talking-about
1•reasonableklout•10m ago•1 comments

OpenAI reveals six more safety issues and unveils plan to disclose incidents

https://www.bbc.co.uk/news/articles/cmpq0wj5g899o
3•dr_scully•18m ago•1 comments

Arch-Based Omarchy 4.0.4 Makes Its Custom Linux Kernel the Default

https://linuxiac.com/omarchy-4-0-4-makes-its-custom-linux-kernel-the-default/
1•djfergus•20m ago•0 comments

Using Blender with coding agents on macOS

https://til.simonwillison.net/llms/blender-coding-agents-macos
3•activatedgeek•20m ago•1 comments

Linux Finally Reached My Desk

https://vinny.dev/blog/2026-09-18-omarchy-linux-finally-reached-my-desk/
2•djfergus•21m ago•0 comments

Show HN: Puffdoro – A squishy 3D focus companion built with WebGL and Web Audio

https://puffdoro.200404200.xyz
1•sz_ycombinator•22m ago•0 comments

The story Scientific American wouldn't let me tell

https://www.whatsitlike.me/the-story-scientific-american-wouldnt-let-me-tell/
3•mathgenius•22m ago•0 comments

Roof ushered in a new era for full-text helmet

https://www.roof.fr/en/djagger/682-ro340-djagger-carbon.html
2•teekert•27m ago•1 comments

The Farnese Letter

https://simonklee.dk/farnese-letter
2•latchkey•29m ago•0 comments

Can AI Create PLC Attacks? Yes, but It's Not That Easy Yet

https://www.forescout.com/blog/can-ai-create-plc-attacks-yes-but-it%E2%80%99s-not-that-easy-yet/
1•Tomte•30m ago•0 comments

Müller-Lyer Illusion

https://en.wikipedia.org/wiki/M%C3%BCller-Lyer_illusion
1•thunderbong•34m ago•0 comments

Union Alpha Stealth Model

https://developers.cloudflare.com/ai/models/stealth/union-alpha/
1•djfergus•35m ago•0 comments

I'm excited about Maps and built an gamified news app around it

2•sourav_biswas•39m ago•2 comments

AI agents can modify themselves without humans telling them to do so

https://www.theregister.com/security/2026/09/16/ai-agents-can-modify-themselves-without-humans-te...
3•galaxyLogic•41m ago•0 comments

PC ports of old console games are the new AI vibe coding battleground

https://www.pcgamer.com/gaming-industry/pc-ports-of-old-console-games-are-the-new-ai-vibe-coding-...
2•FMecha•45m ago•0 comments

Typesafe AI

https://typesafe.ai/
1•bretpiatt•50m ago•0 comments

Show HN: Webhook Proxy – Fail-safe logging and 1-click retries for webhooks

https://webhook-proxy-zeta.vercel.app/
1•MAHADDD•52m ago•1 comments

Show HN: I built a Mac app to make technical PDF textbooks easier to study

https://marginstudy.app/
1•alricsiu•53m ago•0 comments

What Little Hope Remains

https://blog.coredump.cx/p/what-little-hope-remains
3•surprisetalk•55m ago•0 comments

Building S4 – our own high-performance time-series data store

https://medium.com/life-at-propeller/building-s4-our-own-high-performance-time-series-data-store-...
1•JoeBot3000•56m ago•0 comments

Ddlforge: Zero-downtime Postgres migration supervisor for Node ORMs

https://github.com/x7ssss/ddlforge
1•plamkatawee•57m ago•0 comments

Communication by means of modulated Johnson noise

https://www.pnas.org/doi/10.1073/pnas.2201337119
2•austinallegro•59m ago•0 comments

GuitarEasy: ChatGPT converts guitar tabs, GuitarEasy.app plays it, you practise

https://github.com/ShaojieJiang/guitareasy
1•NeuralNotwork•1h ago•0 comments

iOS 27.2 Could Save Your Marriage

https://www.macrumors.com/2026/09/16/ios-27-2-could-save-your-marriage/
2•tosh•1h ago•0 comments

The 'Kraken' Existed, and Scientists Found Its Jaws Hidden in Stone

https://dailygalaxy.com/2026/09/kraken-giant-octopus-fossil-jaws-discovery/
2•wertyk•1h ago•0 comments