frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

There Is a RAM Shortage

https://www.npr.org/2026/02/21/nx-s1-5719256/theres-a-shortage-of-ram-computer-memory-how-is-this...
1•paulpauper•2m ago•0 comments

Tokens: The New Oil – By Kent Beck

https://tidyfirst.substack.com/p/tokens-the-new-oil
1•paulpauper•3m ago•0 comments

Show HN: Termtrace – Record and replay terminal sessions as traces

1•amalChandru•3m ago•0 comments

Six Flags Was a Summer Destination. Can It Win Families Back?

https://www.nytimes.com/2026/04/02/business/six-flags-parks-closing-debt-attendance.html
1•duxup•6m ago•1 comments

In the Gulf, GPS jamming leaves delivery drivers navigating blind

https://restofworld.org/2026/gps-disruption-gulf-gig-workers/
1•donohoe•6m ago•0 comments

Show HN: SFS – A FUSE-based filesystem with SSH transport writen in Rust

https://github.com/skorotkiewicz/sfs
1•modinfo•7m ago•0 comments

Carbible.net: Car Guide

https://carbible.net/
1•polyspora•8m ago•1 comments

Iran is a distraction [video]

https://www.youtube.com/watch?v=koa2wUeWJL8
1•teleforce•9m ago•0 comments

Milgram's subjects were never aligned

https://hollisrobbinsanecdotal.substack.com/p/milgrams-subjects-were-never-aligned
1•mattas•9m ago•0 comments

What an Ivy League Education Gets You

https://www.theatlantic.com/ideas/2026/04/ivy-league-education-income/686682/
1•paulpauper•9m ago•1 comments

Octopus Energy's Agile Prices

https://agileprices.co.uk/
2•jonatron•10m ago•0 comments

How much it costs to attend a game at every MLB ballpark

https://visityourteam.com/mlb/rankings/game-day-costs
1•smarthomeu•10m ago•0 comments

Show HN: Detect7 – automatic DDoS protection layered on Cloudflare

https://detect7.com/
1•rk-baku•10m ago•0 comments

Environment as a Service: Agent as the Interface

https://blog.dreambubble.ai/en/posts/environment-as-a-service-agent-as-the-interface
1•qingant•10m ago•1 comments

The White House App Is Riddled with Cybersecurity Vulnerabilities

https://www.notus.org/technology/trump-white-house-app-cybersecurity
1•OrangePilled•10m ago•0 comments

Starlink Replacement: Russia Develops Stratospheric UAV Relay

https://en.topwar.ru/278156-zamena-starlink-v-rossii-razrabotali-stratosfernyj-bpla-retransljator...
1•B1FF_PSUVM•14m ago•0 comments

AI Models Choose Which Businesses to Recommend

https://driftspear.com/blog/how-ai-models-choose-which-business.md
1•woktalk•15m ago•0 comments

map of notable peoples birthplaces around the world.

https://tjukanovt.github.io/notable-people
3•BoredPositron•18m ago•0 comments

"Privacy. That's iPhone." – and Other Things That Need an Asterisk

https://blog.ppb1701.com/privacy-thats-iphone-and-other-things-that-need-an-asterisk
4•upofadown•19m ago•0 comments

What life looks like on the most remote inhabited island

https://apps.npr.org/life-on-tristan-da-cunha/
4•brightbeige•20m ago•1 comments

Amazon in Talks to Buy Globalstar

https://www.satellitetoday.com/finance/2026/04/03/amazon-in-talks-to-buy-globalstar-according-to-...
3•OrangePilled•20m ago•0 comments

Chinese AI Startup Is Mapping Every US Military Asset in Middle East in Realtime

https://breached.company/mizarvision-chinese-ai-satellite-us-military-tracking-2026/
2•Betelbuddy•21m ago•0 comments

Find out if your grandparents were Nazis

https://www.zeit.de/wissen/2026-04/nsdap-mitgliederkartei-karteikarten-familienmitglieder-suche
2•28304283409234•22m ago•1 comments

Buddy Feature in Claude Code Sucks

2•Avicebron•22m ago•0 comments

Apple approves driver that lets Nvidia eGPUs work with Arm Macs

https://www.theverge.com/tech/907003/apple-approves-driver-that-lets-nvidia-egpus-work-with-arm-macs
3•naves•25m ago•0 comments

Structured-ZSTD – A pure Rust Zstandard implementation

https://github.com/structured-world/structured-zstd
1•polaz•27m ago•0 comments

Retail buyer's silver purchases stolen at FedEx by someone with insider info

https://no01.substack.com/p/a-readers-story-worth-passing-on
1•OgsyedIE•28m ago•0 comments

Where Is Artemis?

https://www.whereisartemis.com/
2•kvakkefly•30m ago•0 comments

Real Time Lightning Map

https://www.lightningmaps.org/#m=ses;t=3;s=0;o=0;b=;ts=0;
1•ohjeez•30m ago•0 comments

GTO Wizard Benchmark

https://arxiv.org/abs/2603.23660
1•EvgeniyZh•30m ago•0 comments