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.

Ask HN: Hypothesis is all you need

1•mikasisiki•1m ago•0 comments

The road to Unreal Engine 6

https://www.unrealengine.com/news/the-road-to-ue-6
1•ezekg•2m ago•0 comments

Show HN: Feynman – no more reading books as isolated objects

https://account.ycombinator.com/
1•imwoody•2m ago•0 comments

Amateur may have cracked Linear A, a 120-year-old puzzle

https://aiclambake.com/clamtakes/linear-a/
1•Kosturdistan•2m ago•1 comments

59% of videos served to a new TikTok user's FYP are AI slop

https://www.kapwing.com/resources/the-tiktok-ai-slop-report/
1•jenthoven•3m ago•0 comments

Norway imposes near ban on AI in elementary school

https://www.reuters.com/technology/norway-imposes-near-ban-ai-elementary-school-2026-06-19/
2•ilreb•4m ago•1 comments

Linux Apps That Maybe Run

https://sigwait.org/~alex/blog/2026/06/13/qAp2Gu.html
1•henry_flower•6m ago•0 comments

Show HN: Pretty LWN – a cleaner, HN-inspired theme for LWN.net

https://chromewebstore.google.com/detail/pretty-lwn-unofficial/hjplcclhjejakojgblkepakicgaclpog
1•TheThirdTuring•7m ago•0 comments

Ideonella Sakaiensis

https://en.wikipedia.org/wiki/Ideonella_sakaiensis
1•thunderbong•8m ago•0 comments

Google Said 'Nice Catch' Then Denied Bounty

https://www.theregister.com/security/2026/06/18/google-told-researcher-nice-catch-then-denied-bug...
3•olearysec•9m ago•0 comments

Show HN: Anumaan, a navigation app project for GPS and Internet denied regions

https://github.com/deepanwadhwa/anumaan
2•dwa3592•11m ago•0 comments

Conscripting engineers to make training data won't push AI

https://futuresearch.ai/meta-engineers-training-data/
1•ddp26•12m ago•0 comments

The Artificial Life Lesson: Forty Years of Digital Evolution Research

https://adaptivesoftware.substack.com/p/the-artificial-life-lesson
1•rafaelc•12m ago•0 comments

Munich 1991: The Roots of the Current AI Boom

https://people.idsia.ch/~juergen/ai-boom-roots-munich-1991.html
2•tosh•13m ago•0 comments

Tell HN: I want to build an open-source Trello that's free, and a joy to use

1•darkhorse13•13m ago•0 comments

Chinese DRAM/SSDs makers have an advantage over American and Taiwanese suppliers

https://www.tomshardware.com/pc-components/ssds/chinese-makers-of-dram-modules-ssds-have-a-seriou...
4•yogthos•15m ago•0 comments

Sankey diagram of the Democratic gubernatorial primary ranked choice tabulation

https://old.reddit.com/r/Maine/comments/1ua4e7n/sankey_diagram_of_the_democratic_gubernatorial/
3•robin_reala•23m ago•0 comments

How to write loops in Claude Code

https://techstackups.com/guides/how-to-write-loops-claude-code/
4•ritzaco•25m ago•0 comments

Host-HTML: an agent-native way of sharing HTML files

https://host-html.com/
1•phanosha•26m ago•1 comments

The CEO of Allbirds' new AI biz has a plan, but no employees

https://techcrunch.com/2026/06/19/the-ceo-of-allbirds-new-ai-biz-has-a-plan-but-no-employees/
4•gpi•27m ago•0 comments

European Delusions and Danish Drones

https://world.hey.com/dhh/european-delusions-danish-drones-a3da0d27
2•gpi•28m ago•0 comments

My agent stack for automating my personal life

https://nicolasbustamante.com/blog/how-agents-run-my-personal-life
1•azhenley•28m ago•0 comments

A Stressful Game

https://gridlockedgame.netlify.app/
1•charliemoffat•28m ago•1 comments

I feel like giving up on coding

https://reedybear.bearblog.dev/i-feel-like-giving-up-on-coding/
2•speckx•29m ago•1 comments

Zeroshot, an open-source CLI for coding-agent verification loops

https://github.com/the-open-engine/zeroshot
2•tomdps•29m ago•0 comments

NY mayor, other leaders push to ban horse-drawn carriage rides after teen death

https://www.cnn.com/2026/06/19/us/central-park-horse-carriage-ride-death-debate-hnk
1•dbvn•30m ago•1 comments

Why Do So Many Everyday Systems Feel Harder to Use Now?

https://therealitydrift.substack.com/p/reality-drift-in-everyday-life
4•realitydrift•33m ago•4 comments

Show HN: Claude Code for talking to potential customers

https://trydatapoint.com
1•yoloakki•34m ago•0 comments

The AI slop refactor wave is coming

https://old.reddit.com/r/SaaS/comments/1u6f84g/the_ai_slop_refactor_wave_is_coming_and_i_havent/
6•root-parent•34m ago•2 comments

Bcachefs exits experimental status in new 'performance release'

https://www.theregister.com/software/2026/06/19/bcachefs-exits-experimental-status-in-new-perform...
2•ofrzeta•37m ago•0 comments