frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

LUKSbox – Store sensitive files in the cloud without trusting the host

https://github.com/PentHertz/LUKSbox
1•danborn26•1m ago•0 comments

AI Gilfoyle

1•peterpommes•3m ago•0 comments

DuckDB-Delta Grows Up: Writes, Unity Catalog and Time Travel

https://duckdb.org/2026/05/07/delta-uc-updates
1•szarnyasg•4m ago•0 comments

Suits in 5 Nations Allege Apple Quashed "Watch Room" Competition

https://www.law.com/2026/05/08/suits-in-5-nations-allege-apple-quashed-watch-room-competition/
1•1vuio0pswjnm7•4m ago•0 comments

Accessibility of date formats (and superiority of YYYY-MM-DD)

https://mina86.com/2026/on-a11y-of-date-formats/
1•OuterVale•5m ago•0 comments

Trying a few ideas with rust and Python

https://github.com/KevinKenya/nairobi-connector-open-source/tree/main
1•kevinkenya•6m ago•0 comments

FCC Proposes Requiring Government-Issued ID for Phone Service [pdf]

https://docs.fcc.gov/public/attachments/DOC-421309A1.pdf
1•diogenes_atx•9m ago•1 comments

In search of wasted bits: how much information do LLM weights carry?

https://fergusfinn.com/blog/weight-entropy/
1•gmays•9m ago•0 comments

Sql2md – Zero-dependency SQL to Markdown converter (open source)

https://github.com/hyperbob/sql2md
1•sqlfans_qmejpi•11m ago•0 comments

Become the Place People Follow

https://www.researchterminal.ai
2•ipachanga•11m ago•0 comments

Rprom 7-fold Amiga 500/600/2000 from 6a/4.3 Kickstart Switcher configurable

https://www.retrobuddys.com/en/shop/amiga-hw-mods/amiga-2000-en/rprom-7-fold-amiga-500-600-2000-f...
1•doener•13m ago•0 comments

Show HN: I made an AI tool to keep my browser tabs clean

https://www.uncluttr.net
1•Nair0•18m ago•0 comments

Pinball Dreams

https://en.wikipedia.org/wiki/Pinball_Dreams
1•doener•18m ago•0 comments

The mangled remains of probes sent to Venus may still be there

https://www.scientificamerican.com/article/the-mangled-remains-of-probes-sent-to-venus-may-still-...
1•beardyw•19m ago•0 comments

Pentagon Wants Innovation. Small Businesses Want to Deliver. What's the Problem?

https://www.inc.com/issie-lapowsky/pentagon-wants-innovation-small-nimble-businesses-want-to-deli...
1•malloryerik•19m ago•0 comments

Malware Found in Trending Hugging Face Repository "Open-OSS/Privacy-Filter"

https://www.hiddenlayer.com/research/malware-found-in-trending-hugging-face-repository-open-oss-p...
1•aa_is_op•22m ago•0 comments

People and Timeline of Modern AI

https://senthil.learntosolveit.com/posts/2026/05/09/annotated-history-of-modern-ai-and-deep-learn...
1•orsenthil•24m ago•0 comments

Fake Pound Coin Database

https://www.thefakepoundcoindatabase.co.uk/
1•bhickey•25m ago•0 comments

Pushing Local Models with Focus and Polish

https://lucumr.pocoo.org/2026/5/8/local-models/
1•lumpa•27m ago•0 comments

Occupations with the Highest Divorce Rates

https://flowingdata.com/2026/05/07/divorce-and-occupation-2026/
1•lvnfg•28m ago•0 comments

The Shadow Admin Threat: AI Agents Creating Undetectable Backdoors

https://sharetxt.live/blog/the-shadow-admin-threat-how-autonomous-ai-agents-could-introduce-undet...
1•rexthonyy•31m ago•0 comments

The 28th Regime (EU)

https://the28thregime.eu/
2•dgellow•31m ago•0 comments

The left-wing case for AI

https://www.seangoedecke.com/the-left-wing-case-for-ai/
4•nsavage•33m ago•0 comments

LLM built for 12M-token reasoning

https://subq.ai/
1•tschibrillo•33m ago•0 comments

Ask HN: What's the most compelling paranormal evidence you found online?

3•johnnyfived•38m ago•1 comments

Let the Barbarians In

https://www.sigops.org/2026/let-the-barbarians-in-how-ai-can-accelerate-systems-performance-resea...
1•jruohonen•40m ago•0 comments

HTML/CSS/JS Viewer with full bidirectional code-preview highlighting

https://html-viewer.org
1•jackxmm•42m ago•0 comments

Extracting alignment data in open models

https://arxiv.org/abs/2510.18554
1•yakkomajuri•42m ago•0 comments

Show HN: Super ISO Updater

https://github.com/JoshuaVandaele/SuperISOUpdater
1•JoshuaVandaele•54m ago•0 comments

Show HN: Pace-coach watches your typing rhythm and nudges you when stressed

https://github.com/job-almekinders/pace-coach
1•Jobtimize•55m ago•1 comments