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.

It's not too late to roll back MCP

https://theoryvc.com/blog-posts/its-not-too-late-to-roll-back-mcp
3•bbischof•2m ago•0 comments

Can you identify these articles from the web by their social share images?

https://omgimg.jim-nielsen.com/
1•twapi•2m ago•0 comments

We are in the 'advanced stages' of an AI bubble, says Rockefellers Ruchir Sharma [video]

https://www.youtube.com/watch?v=j02DmOVdlt4
1•thelastgallon•2m ago•0 comments

Geometric Algorithms for Translucency Sorting in Minecraft [pdf]

https://douira.dev/assets/document/douira-master-thesis.pdf
1•HeliumHydride•2m ago•0 comments

New backgammon P2P wagering app

https://www.backgammon-cash.com
1•matthakimi•3m ago•1 comments

My First Impression on HP Zbook Ultra G1a: Ryzen AI Max+ 395, Strix Halo 128GB

https://forum.level1techs.com/t/my-first-impression-on-hp-zbook-ultra-g1a-ryzen-ai-max-395-strix-...
1•teleforce•5m ago•0 comments

Blockly based no-code (or code) Machine Learning platform you can click and run

https://github.com/pycanvas/pycanvas-desktop/releases/tag/v0.1.0
2•zxu•8m ago•1 comments

Paying $3 for a Dollar: The Rational Irrationality of Venture Capital

https://docs.google.com/document/d/1S7nazV5nKmYYkzxhmf3G-Ftb3DPAjV5Migokw3u8BsQ/edit?tab=t.0
1•dustingetz•8m ago•0 comments

Valid Polish: Polish is the language of the Digerati

https://validpolish.com/
2•kyyt•10m ago•1 comments

Devastated PC builder orders DDR5 RAM from Amazon receives DDR2 and some weights

https://www.tomshardware.com/pc-components/ddr5/sealed-ddr5-kit-sold-on-amazon-reportedly-contain...
7•teleforce•12m ago•0 comments

Breakthroughs that will redefine AI over the next 18 months [video]

https://www.youtube.com/watch?v=h-z71uspNHw
2•leoxv•20m ago•0 comments

Show HN: I built a tool to score your website's LLM readability

https://websiteaiscore.com
1•aggeeinn•22m ago•1 comments

The Biggest Breakthroughs in Mathematics: 2025 [video]

https://www.youtube.com/watch?v=hRpcWpAeWng
1•pykello•24m ago•0 comments

Fulton surface-to-air recovery system

https://en.wikipedia.org/wiki/Fulton_surface-to-air_recovery_system
1•ColinWright•24m ago•0 comments

Compute Trends Across Three Eras of Machine Learning (2022)

https://arxiv.org/abs/2202.05924
2•measurablefunc•26m ago•0 comments

Tell HN: The Gospel According to forever list

1•bikamonki•29m ago•0 comments

Closure of Greenlandic Wikipedia

https://meta.wikimedia.org/wiki/Proposals_for_closing_projects/Closure_of_Greenlandic_Wikipedia
1•geox•30m ago•0 comments

Habits Beat Motivation

https://dontbreakprod.com/posts/habits-beat-motivation
1•dorkrawk•30m ago•0 comments

Purdue makes 'AI working competency' a graduation requirement

https://www.theregister.com/2025/12/17/purdue_require_ai_working_competency/
2•Bender•33m ago•0 comments

Jassy taps 27-year Amazon veteran to run AGI org which is now definitely a thing

https://www.theregister.com/2025/12/17/jassy_taps_peter_desantis_to_run_agi/
2•Bender•33m ago•0 comments

Thunderbird Expanding Microsoft Exchange and Protocol Support for 2026

https://www.phoronix.com/news/Thunderbird-2026-Plans
2•Bender•33m ago•0 comments

Ask HN: How do you market a small project?

1•thebigship•34m ago•1 comments

Great Ideas in Theoretical Computer Science

https://www.cs251.com/
3•sebg•36m ago•0 comments

TikTok signs agreement to create new U.S. joint venture

https://www.cnbc.com/2025/12/18/tik-tok-us-sale-china.html
3•mfiguiere•36m ago•0 comments

GrapheneOS blocks WhatsApp 0-day 0-click RCE exploit

https://twitter.com/MetroplexGOS/status/1982163802188575178
5•akyuu•37m ago•1 comments

Evidence shows deadly Brown, MIT shootings may be linked, sources say

https://www.foxnews.com/us/evidence-shows-deadly-brown-mit-shootings-may-linked-sources-say-report
3•perihelions•39m ago•0 comments

Trump signs executive order reclassifying cannabis

https://www.cnbc.com/2025/12/18/trump-pot-reclassification-cannabis-stocks-medicare-cbd.html
7•evo_9•41m ago•1 comments

LLM-Interview-Questions-and-Answers: 100 LLM interview questions with answers

https://github.com/KalyanKS-NLP/LLM-Interview-Questions-and-Answers-Hub
3•simonpure•41m ago•0 comments

Results-only audit: interface transport shows 15x redundancy, 90% fewer hotspots

https://github.com/johnoliveiradev/SentinelHotSpot
1•johnoliveiradev•43m ago•1 comments

BirdRadio: Listen to bird sounds around the world

https://bird-radio.pages.dev
1•chill_ai_guy•43m ago•0 comments