frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Flavors of Dr. Pepper (Doom Scroll)

https://www.drpepper.ca/en/blend-of-23-flavours/
1•ninju•33s ago•0 comments

A Shiny Fuzzer in Go

https://github.com/cecinuga/fuzzy
1•cecinuga•1m ago•0 comments

Valve's version of Android on Linux (based on Waydroid) is now called Lepton

https://www.gamingonlinux.com/2025/12/valves-version-of-android-on-linux-based-on-waydroid-is-now...
1•wicket•2m ago•0 comments

What I learned building an opinionated and minimal coding agent

https://mariozechner.at/posts/2025-11-30-pi-coding-agent/
1•gmays•3m ago•0 comments

Show HN: TrackerNews – Keyword monitoring and insight extraction

https://trackernews.app/
1•winchester6788•4m ago•0 comments

My redirect rules from 2013 still work and yours don't

https://idiallo.com/blog/my-redirect-rules-from-2013-still-work
3•foxfired•5m ago•0 comments

JEP 504: Remove the Applet API

https://openjdk.org/jeps/504
1•matthews3•5m ago•1 comments

Pentagon Launches $1B Program to Buy Hundreds Of Thousands Of Kamikaze Drones

https://www.twz.com/air/pentagon-launches-1b-program-to-rapidly-buy-hundreds-of-thousands-of-kami...
3•breve•7m ago•0 comments

The case of the 500-mile email (2002)

https://www.ibiblio.org/harris/500milemail.html
5•Timothee•8m ago•0 comments

Solve Go Challenge: Octantconway

https://github.com/plutov/practice-go/tree/master/octantconway
2•todsacerdoti•9m ago•0 comments

Why Won't the Media Report Accurately on Road Deaths?

https://jakecoppinger.com/2025/12/why-wont-the-media-report-accurately-on-road-deaths/
2•jakecopp•10m ago•1 comments

Show HN: ThesisBoard – Trello for Investment Research

https://thesisboard.com/
1•egobrain27•12m ago•0 comments

Building a fintech platform's mobile app

https://hackclub.com/fiscal-sponsorship/mobile/
1•JustSkyfall•12m ago•0 comments

`npx vercel` opens a project

https://ando.so/company
1•frootoftheloom•13m ago•0 comments

Catalogue of Moulded and Ornamental Brick (1892)

https://archive.org/details/central-press-brick-1900s-a
1•georgefrowny•13m ago•0 comments

Social identification with a team boosts fans' social well-being

https://phys.org/news/2025-11-social-identification-team-boosts-fans.html
2•PaulHoule•13m ago•0 comments

StackOverflow: AI Assist

https://stackoverflow.com/ai-assist
2•Bootvis•16m ago•0 comments

Windows 11 growth slows as millions stick with Windows 10

https://www.theregister.com/2025/12/03/windows_11_statcounter/
1•naves•16m ago•0 comments

Michael Levin on Pain as Agent, Healing as Alignment

https://essays.debugyourpain.com/p/michael-levin-on-pain-as-agent-healing
1•yichab0d•16m ago•1 comments

Mixtures of dietary nutrients lessen behavioral deficits in mouse autism models

https://journals.plos.org/plosbiology/article?id=10.1371/journal.pbio.3003231
2•bookofjoe•16m ago•0 comments

Show HN: I built a trending books by month Bump Chart with Hardcover data

https://hardcover.app/labs/popular-by-month
1•dyogenez•18m ago•0 comments

Source code to Sega Saturn game "Powerslave" released

https://github.com/Lobotomy-Software/SlaveDriver-Engine
1•alexjplant•20m ago•0 comments

Comments on smartphone ban from high school teacher

https://marginalrevolution.com/marginalrevolution/2025/09/from-the-comments-39.html
2•surprisetalk•22m ago•0 comments

Hiring: Damned If You Do, Damned If You Don't

https://www.factorysettings.org/p/hiring-damned-if-you-do-damned-if
1•surprisetalk•23m ago•0 comments

Some Economics of Artificial Super Intelligence

https://papers.ssrn.com/sol3/papers.cfm?abstract_id=5728702
1•surprisetalk•23m ago•0 comments

Rock Paper Scissors Is a Game of Skill

https://collisteru.substack.com/p/rock-paper-scissors-is-a-game-of
1•surprisetalk•23m ago•0 comments

Alzheimer's is the symptom, not the problem

https://1393.xyz/blog/alzheimers-is-the-symptom-not-the-problem
2•rdgthree•23m ago•0 comments

Vibe Code Like It's 1986

https://github.com/AvitalTamir/vibecommander
1•fatliverfreddy•24m ago•1 comments

The Real-Life Hunt for Red October Happened 50 Years Ago

https://www.twz.com/sea/the-real-life-hunt-for-red-october-happened-50-years-ago
1•breve•24m ago•0 comments

Show HN: MetaConvert – Free PDF and Image Conversion Tools

https://metaconvert.blogspot.com/
2•MetaConvert•24m ago•0 comments