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.

The AI Boom Is Missing the Secret Sauce of the 1990s

https://www.bloomberg.com/news/articles/2026-03-27/why-today-s-ai-boom-won-t-repeat-the-1990s-eco...
1•petethomas•1m ago•0 comments

Australia's Social Media Ban Runs into a Wave of Teen Workarounds

https://www.bloomberg.com/news/articles/2026-03-27/instagram-and-tiktok-ban-for-australian-kids-i...
1•petethomas•2m ago•0 comments

Gulf Countries' Frustration with the US Grows as War Wears On

https://www.bloomberg.com/news/articles/2026-03-27/gulf-countries-frustration-with-the-us-grows-a...
1•petethomas•3m ago•0 comments

Tyler Cowen on the state of economics and AI

https://arnoldkling.substack.com/p/tyler-cowen-on-the-state-of-economics
1•paulpauper•4m ago•0 comments

Social Security Should Be a Forced Savings Program Not a Welfare Program

https://marginalrevolution.com/marginalrevolution/2026/03/social-security-should-be-a-forced-savi...
1•paulpauper•5m ago•0 comments

The Age Verification Con

https://reclaimthenet.org/the-age-verification-con
1•sizzleflip5000•7m ago•0 comments

PonderingEffects

https://wiki.alopex.li/PonderingEffects
1•vinhnx•8m ago•0 comments

How to use mutliple Android devices together to make music and more

https://codeberg.org/ohmstone/android-multi-device-demo
1•tonelord•12m ago•1 comments

Everyone is building a software factory

https://blog.exe.dev/bones-of-the-software-factory
1•farmerbb•15m ago•0 comments

FitBit Fakes Data: Google Treats Integrity as Career Poison

https://www.flyingpenguin.com/fitbit-fakes-data-google-treats-integrity-as-career-poison/
2•astennumero•24m ago•0 comments

Your Defense Code Is Already AI-Generated. Now What?

https://warontherocks.com/2026/03/your-defense-code-is-already-ai-generated-now-what/
1•KnuthIsGod•26m ago•0 comments

The future of text layout is not CSS

https://chenglou.me/pretext/editorial-engine/
2•skeptrune•30m ago•0 comments

Consumer Rights Wiki

https://consumerrights.wiki/w/Main_Page
2•basilikum•31m ago•0 comments

Americans can 'directly text Donald Trump' with new White House app

https://www.the-sun.com/news/16147962/donald-trump-new-white-house-app-livestreams-unfiltered-news/
1•nh43215rgb•31m ago•0 comments

Show HN: Sigil – A zero-knowledge steganography vault for AI training data

1•nishalk•33m ago•0 comments

The Worst-Case Scenario for AI and the News Is Here

https://www.theatlantic.com/international/2026/03/netanyahu-not-dead-israel-ai/686593/
2•JumpCrisscross•33m ago•0 comments

Musk has a plan to make human labor obsolete. Billionaires are joining in.

https://www.washingtonpost.com/technology/2026/03/27/musk-optimus-robot-physical-ai/
3•bookofjoe•37m ago•7 comments

Trump Said This Policy Would Make Manhattan a 'Ghost Town.' He Was Wrong

https://www.nytimes.com/2026/03/27/opinion/congestion-pricing-traffic-new-york.html
2•lxm•47m ago•0 comments

SlopCodeBench: Benchmarking How Coding Agents Degrade over Long-Horizon Tasks

https://www.scbench.ai/
2•matt_d•49m ago•0 comments

ViraxLog – Tamper-Proof Logging Using Merkle Trees and BLAKE2B for Python

https://github.com/damienos61/viraxlog
2•DamienOS•51m ago•0 comments

Chezmoi: Manage Dotfiles Across Machines

https://www.chezmoi.io/
2•cdrnsf•54m ago•0 comments

SpaceX IPO Probability Tracker – S-1 filing imminent, built with Kalshi data

https://spacexipotracker.com/
2•RuslanPoptsov•55m ago•1 comments

An early-adopter used my software to build for their client

https://struere.dev
2•MarcoKueks•58m ago•1 comments

What parents should know about the social media addiction trials

https://www.transparencycoalition.ai/news/tcai-guide-what-parents-should-know-about-the-social-me...
1•evo_9•1h ago•0 comments

Ask HN: What do you think Information Technology in Japan?

1•AnonyMD•1h ago•0 comments

Working on Products People Hate

https://www.seangoedecke.com/working-on-products-people-hate/
3•PieUser•1h ago•0 comments

How notch traversal works on MacBooks

https://tailscale.com/blog/macos-notch-escape
1•LorenDB•1h ago•0 comments

ChatGPT Will Not Find Your Next Cancer Drug

https://blog.pauling.ai/p/chatgpt-will-not-find-your-next-cancer
1•tordable•1h ago•2 comments

Man beats machine at Go (2023)

https://www.ft.com/content/175e5314-a7f7-4741-a786-273219f433a1
2•bumbledraven•1h ago•3 comments

Public-records accountability site for California high-speed rail

https://highspeed.fail/
2•jasonculbertson•1h ago•0 comments