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.

Pakistan, Saudi in talks on JF-17 jets-for-loans deal, sources say

https://www.reuters.com/world/asia-pacific/pakistan-saudi-talks-jf-17-jets-for-loans-deal-sources...
1•JumpCrisscross•26s ago•0 comments

Built the behavior analytics tool I've always wanted as a product engineer

https://getallinsights.com/
1•psyentist•41s ago•1 comments

Early screentime linked to slower decision-making, higher anxiety in children

https://www.thelancet.com/journals/ebiom/article/PIIS2352-3964(25)00543-2/fulltext
1•hongsy•3m ago•0 comments

On (not) using Django in 2026

https://www.natemeyvis.com/on-not-using-django-in-2026/
1•ingve•6m ago•1 comments

Free Normal Map Generator Online for 3D

https://normalmapgenerator.io/
1•richardharmer•7m ago•1 comments

Show HN: Flowscape – A developer-first 2D canvas engine with full scene control

https://github.com/Flowscape-UI/core-sdk
1•flowscape_ui•8m ago•0 comments

I kept losing good AI prompts, so I built a prompt memory tool

https://promper.vercel.app/
1•SRMohitkr•10m ago•0 comments

Shelley: A Coding Agent for Exe.dev

https://blog.exe.dev/shelley
1•anupcshan•13m ago•0 comments

Show HN: FOSS Email-to-Webhook Bridge (Turn Monitoring Alerts into API Calls)

https://app.v3m.pw/
1•idrissmbellil•13m ago•0 comments

FreeBSD Home NAS, part 8: Backing up NFS and Samba data with restic

https://rtfm.co.ua/en/freebsd-home-nas-part-8-nfs-and-samba-data-backups-using-restic/
1•todsacerdoti•15m ago•0 comments

Show HN: Open-source autonomous dev teams for Claude Code

https://github.com/covibes/zeroshot
1•covibes•22m ago•0 comments

Show HN: A Python tool to turn your ChatGPT history to Word clouds and md files

https://github.com/mohamed-chs/chatgpt-history-export-to-md
1•mohamedchs•24m ago•0 comments

What daily progress means. (and how to track it)

https://medium.com/@loganholdsworth136/what-daily-progress-actually-means-and-how-to-track-it-58f...
1•boyodestroyer•27m ago•0 comments

BdayCanvas – Happy Birthday Images AI Generator

https://bdaycanvas.com/
1•alwaysaladdin•31m ago•1 comments

Woman in Minnesota fatally shot by ICE agent during raid, video shows

https://www.theguardian.com/us-news/2026/jan/07/minneapolis-shooting-immigration-crackdown
4•merusame•31m ago•0 comments

CISA tags max severity HPE OneView flaw as actively exploited

https://www.bleepingcomputer.com/news/security/cisa-tags-max-severity-hpe-oneview-flaw-as-activel...
1•fleahunter•32m ago•0 comments

Open source project chatbox is under attack

https://github.com/chatboxai/chatbox/issues
1•themez•35m ago•0 comments

How do you make your Agents to match interface in first try?

1•paulistaunb•36m ago•1 comments

Why are there 60 Seconds in a Minute, 60 Minutes in an Hour, and 24 Hours Days?

https://monochrome-watches.com/why-60-seconds-in-a-minute-why-60-minutes-in-an-hour-by-24-hours-i...
1•Pamar•37m ago•2 comments

Twinkle Star Sprites

http://www.hardcoregaming101.net/twinkle-star-sprites/
1•tosh•39m ago•0 comments

Tonari: Infinite Dynamic Space

https://tonari.no/
2•sbt567•39m ago•0 comments

Nano Banana Prompt Guide

https://banana-prompts.com/learn/ultimate-guide
1•icstiss•40m ago•1 comments

A12: Visions of the Networked Desktop (2013)

https://arcan-fe.com/2023/11/18/a12-visions-of-the-fully-networked-desktop/
2•pabs3•43m ago•0 comments

An Experienced C Programmer Tries AI Agents

https://danielchasehooper.com/posts/code-agents/
3•ingve•43m ago•1 comments

The End of the Orbital Index

https://orbitalindex.com/archive/2026-01-07-Issue-350/
2•simonebrunozzi•44m ago•0 comments

Building and Installing LineageOS for UTM Virtual Machine on Apple Silicon Mac

https://wiki.lineageos.org/utm-vm-on-apple-silicon-mac
1•arm•46m ago•0 comments

U.S. withdrawing from bodies, treaties, and conventions against U.S. interests

https://www.whitehouse.gov/presidential-actions/2026/01/withdrawing-the-united-states-from-intern...
3•uniclaude•47m ago•1 comments

The Personal Panoptican

https://docs.google.com/document/d/19-ajYTp2hwOW9WcirY9OIoSvMdfyivup8LMI92PkS20/mobilebasic
1•samuel246•51m ago•0 comments

Public key encryption for pedestrians: RSA and OAEP padding with a real SSH key

https://hal.science/hal-05359402v1
1•Ranlot•53m ago•0 comments

Estonian volunteers struggling to protect Wikipedia from Russian propaganda

https://news.err.ee/1609903256/estonian-volunteers-struggling-to-protect-wikipedia-from-russian-p...
4•obscurette•53m ago•0 comments