frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

World Age for beginners: one way to compile a dynamic language

https://discourse.julialang.org/t/world-age-for-beginners-one-way-to-compile-a-dynamic-language/1...
1•anewhnaccount2•49s ago•0 comments

Show HN: 3D Visualizing YC Advice

https://github.com/akshay326/quote-viz
1•akshay326•2m ago•0 comments

Noise sensitivity disrupts the mind, brain and body

https://www.bbc.com/future/article/20250804-inside-the-brains-of-noise-sensitive-people
1•rock_artist•4m ago•0 comments

Your problem with Vim is that you don't grok vi.

https://stackoverflow.com/questions/1218390/what-is-your-most-productive-shortcut-with-vim
1•chanana•5m ago•0 comments

The Map Is Not the Territory: The Agent-Tool Trust Boundary

https://niyikiza.com/posts/map-territory/
2•niyikiza•18m ago•0 comments

If you want to plan your dev projects faster, use this tool

https://getfrostbyte.dev/
3•Skyfall106•25m ago•0 comments

Are the YouTube channel Courts and Crimes's shorts AI-generated deep fakes?

https://skeptics.stackexchange.com/questions/60423/are-the-youtube-channel-courts-crimess-shorts-...
2•razor-thin•30m ago•0 comments

Show HN: OpenAI FM – Zero-setup AI voice tool(Inspired by repo)

https://openai-fm.org/
2•thenextechtrade•34m ago•0 comments

Altaid 8800

https://sunrise-ev.com/8080.htm
2•exvi•35m ago•0 comments

Z80 Mem­ber­ship Card

https://sunrise-ev.com/z80.htm
3•exvi•35m ago•0 comments

Reverse Shell as a Service

https://github.com/lukechilds/reverse-shell
2•jayhoon•38m ago•0 comments

Bill Pearson's (Alien Supervising Model Maker) Portfolio, Projects and Showreel

https://www.bookofthedead.ws/bill_pearson/
2•exvi•39m ago•0 comments

BreachForums hacking forum database leaked, exposing 324,000 accounts

https://www.bleepingcomputer.com/news/security/breachforums-hacking-forum-database-leaked-exposin...
3•hentrep•41m ago•0 comments

Time without end: Physics and biology in an open universe-Freeman J. Dyson [pdf]

https://journals.aps.org/rmp/abstract/10.1103/RevModPhys.51.447
2•thunderbong•42m ago•0 comments

China-Australia relationship in 2026? This is how experts in China see it

https://theconversation.com/can-the-china-australia-relationship-stay-on-track-in-2026-this-is-ho...
3•defrost•46m ago•1 comments

Software Engineer Pay at McDonalds

https://www.levels.fyi/companies/mcdonalds/salaries/software-engineer
2•filetmignon•49m ago•0 comments

Pineapple Desktop (FreeDOS)

https://doscore.net/pineapple.html
3•kristopolous•50m ago•2 comments

Show HN: AgentWatch – A terminal dashboard for monitoring AI Agent costs

https://github.com/Tarunjit45/agentwatch
2•DarkX45•51m ago•0 comments

Harmonia – Hash function using Golden Ratio and Quasicrystal mathematics

https://github.com/faustodas-afk/harmonia-crypto
2•fadase•51m ago•1 comments

Found a rare moment when HN is restarting server

https://freeimage.host/i/screenshot-2026-01-12-112131.fvEKgee
2•celurian92•53m ago•0 comments

Show HN: Moving files without install on the host

https://uldl.sh
1•thedanielforum•53m ago•0 comments

Gh-Dash – GitHub PR Dashboard for Claude Code

https://github.com/jakozloski/claude-code-gh-dash
1•handfuloflight•1h ago•0 comments

Show HN: I kept forgetting Git worktree syntax, so I wrapped it

https://github.com/binbandit/workty
2•binbandit•1h ago•1 comments

Malaysia and Indonesia Block Elon Musk's Grok

https://www.cnbc.com/2026/01/12/malaysia-indonesia-block-elon-musks-grok-obscene-non-consensual-c...
3•rfarley04•1h ago•0 comments

The Workings of the Pentagon's UFO Reverse Engineering Program [video]

https://www.youtube.com/watch?v=u7g5Sn1DJF4
4•keepamovin•1h ago•0 comments

Semantic Rebase

https://www.peterjthomson.com/2026/01/semantic-rebase/
1•handfuloflight•1h ago•0 comments

XFCE Is Great

https://rubenerd.com/xfce-is-great/
67•mikece•1h ago•35 comments

Xibo open-source digital signage solution now works with Raspberry Pi 5

https://www.cnx-software.com/2026/01/12/xibo-open-source-digital-signage-solution-now-works-with-...
1•mikece•1h ago•0 comments

Everything you should know about PostgreSQL constraints

https://xata.io/blog/constraints-in-postgres
2•tudorg•1h ago•0 comments

The Definitive Guide to Claude Code

https://jpcaparas.medium.com/the-definitive-guide-to-claude-code-from-first-install-to-production...
3•handfuloflight•1h ago•0 comments