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.

Provenance: What It Takes to Prove Creator Data Dignity

https://medium.com/@vektormemory/provenance-what-it-actually-takes-to-prove-creator-data-dignity-...
1•vektormemory•1m ago•0 comments

Sizeof is surprisingly difficult to parse in C

https://sebsite.pw/w/20260802-sizeof.html
1•jandeboevrie•5m ago•0 comments

The Complete History of Michael Jackson's Sonic 3 [video]

https://www.youtube.com/watch?v=xiuC-616BPQ
1•austinallegro•6m ago•0 comments

Y'all Street Launches: Texas Stock Exchange Goes Live in Dallas

https://www.foxbusiness.com/markets/yall-street-launches-texas-stock-exchange-goes-live-dallas
1•testrun•9m ago•0 comments

Why are all the amounts values negative?

https://bankstatementconverter.com/blog/posts/2026-08-02-why-are-all-amounts-negative/
1•4pkjai•13m ago•0 comments

Cardstock – dynamic OG images from one URL (no render server)

https://cardstock.dev
1•atom_builder•17m ago•0 comments

Claude-for-Hardware

https://github.com/Midstall/claude-for-hardware
1•ai_critic•27m ago•0 comments

AI Ate Your RAM (Why Memory Prices Exploded)

https://www.youtube.com/watch?v=r7b2uwy8_CM
1•cable2600•33m ago•0 comments

CISA Guide Helps Fed Agencies Securely and Effectively Use Open Source Software

https://www.cisa.gov/news-events/news/cisa-guide-helps-federal-agencies-securely-and-effectively-...
3•billybuckwheat•34m ago•0 comments

Attribution model that gives 0% credit to demo requests

https://www.upside.tech/blog/how-we-built-pipedash/
1•aeromusek•35m ago•1 comments

X says ex-manager took $12,000 in crypto and bribed employee to

https://runtimewire.com/article/exclusive-x-says-ex-manager-took-12-000-in-crypto-and-bribed-empl...
1•ryanmerket•35m ago•1 comments

JobRadar: Open-source job search agent that scores listings with a local LLM

https://github.com/ANIRudH-lab-life/job-radar
1•anirudhshivam•37m ago•0 comments

RSS is dead. Stop crying about Google Reader

https://grigio.org/rss-is-dead-stop-crying-about-google-reader/
1•grigio•41m ago•0 comments

Foulab, Montréal's First Hackerspace

https://medium.com/chronicles-of-montr%C3%A9al/foulab-montr%C3%A9al-s-first-hackerspace-590b5ec00151
1•sillysaurusx•45m ago•0 comments

A Tech Founder Wanted to Start a New Country. An Actual Country Got in the Way

https://www.wsj.com/tech/a-tech-founder-wanted-to-start-a-new-country-an-actual-country-got-in-th...
1•nradov•52m ago•0 comments

Show HN: I don't code – I run a self-correcting protocol across 4 projects

https://github.com/Sovereign34/agent-protocol-notes
2•Sovereign34•55m ago•0 comments

Pixel 11 specs and price leak with no surprises

https://www.theverge.com/tech/974238/pixel-11-specs-and-price-leak
1•xiaoyu2006•56m ago•0 comments

U.S. sanctions Iranian firms for Bitcoin maritime insurance operation in Hormuz

https://home.treasury.gov/news/press-releases/sb0581
3•giuliomagnifico•59m ago•0 comments

Game Industry Hardship Fund

https://itch.io/b/3802/game-industry-hardship-fund
2•jbm•1h ago•0 comments

ebookfs – a 9P eBook library filesystem

https://github.com/ramblingenzyme/ebookfs
1•ramblingenzyme•1h ago•1 comments

ao486: x86-compatible Verilog core implementing all features of a 486 SX (2014)

https://github.com/alfikpl/ao486
1•csmantle•1h ago•0 comments

Challenge GPT and Claude to Run Their Own Lemonade Stands[video]

https://www.youtube.com/watch?v=6Ide5pRLR8Y
1•blackcat201•1h ago•0 comments

Strangers pretrained a language model with HF PRs and a cron job

1•somevyn•1h ago•0 comments

'Naked Dressing' Is Spreading and It's Making Things Awkward

https://www.wsj.com/style/fashion/naked-dressing-hailey-bieber-madonna-d2a1d94b
1•petethomas•1h ago•0 comments

Moochacha

https://codeberg.org/markdascher/moochacha
2•sillysaurusx•1h ago•0 comments

J3blocks

https://codeberg.org/amano.kenji/j3blocks
1•sillysaurusx•1h ago•0 comments

Show HN: Nuking the crap Claude left in the codebase – CCN

https://github.com/jonhardwick-spec/ccn
2•jonhardwickspec•1h ago•0 comments

Running Kimi K3 on MI355X at Better Performance per Dollar Than B300

https://www.wafer.ai/blog/kimi-k3-mi355x
67•ilreb•1h ago•0 comments

Booting Fuchsia's Zircon on a Homebrew RISC-V SoC, on an Artix-7 FPGA

https://www.hdlfactory.com/post/2026/07/19/cocoapuffs-booting-fuchsias-zircon-kernel-on-a-risc-v-...
4•mbStavola•1h ago•0 comments

Sick of waiting for agents to run and then having to debug

1•seafaithless•1h ago•1 comments