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.

Jan. 6 Archive: The Capital Charges database

https://apps.npr.org/jan-6-archive/database.html
1•1659447091•47s ago•0 comments

Taphouse – Homebrew GUI

https://taphouse.multimodalsolutions.gr/
1•twapi•5m ago•0 comments

Strauss–Howe Generational Theory

https://en.wikipedia.org/wiki/Strauss%E2%80%93Howe_generational_theory
1•thunderbong•6m ago•0 comments

AgiBot's Backpack-Sized Robot

https://www.superhuman.ai/p/robotics-special-agibot-s-backpack-sized-robot
1•akg130522•8m ago•0 comments

Intellectual Junkyards

https://www.forester-notes.org/QHXS/index.xml
1•ysangkok•8m ago•0 comments

Kashmir Police Crack Down on VPN Use

https://thewire.in/rights/jammu-and-kashmir-police-crack-down-on-vpn-use-file-firs-and-bind-down-...
1•jimsojim•10m ago•0 comments

China Revealed a Motion Controlled Military Robot for Remote Combat

https://www.bgr.com/2063885/china-motion-controlled-military-robot-remote-combat/
1•ashishgupta2209•17m ago•0 comments

ASCII Diagrams

https://asciidiagrams.github.io
1•andsoitis•19m ago•0 comments

I made a tool that ranks comments cleanly from a Reddit post

https://www.commentrank.com/
2•mattmerrick•25m ago•0 comments

My Tour Through a Particle Detector at Fermilab

https://whostakingthetrain.com/my-tour-through-a-particle-detector-at-fermilab/
1•toomuchtodo•26m ago•1 comments

California residents can now request all data brokers delete personal info

https://consumer.drop.privacy.ca.gov/
20•memalign•33m ago•0 comments

Mamdani's Decision to Revoke Executive Orders That Backed Israel

https://www.nytimes.com/2026/01/04/nyregion/mamdani-revoke-israel-orders.html
5•handfuloflight•38m ago•0 comments

Show HN: A concept implementation for a faster Transformer [pdf]

https://github.com/tuned-org-uk/tauformer-paper/blob/main/Permanent%20Domain%20Memory%20and%20Fas...
1•tuned•44m ago•0 comments

Claudish

https://claudish.com/
1•handfuloflight•45m ago•0 comments

The Benefits of a Support Ticket to Documentation Lifecycle

https://www.mooreds.com/wordpress/archives/3710
2•mooreds•46m ago•0 comments

Jarz-Streaming for Your Workloads

https://github.com/plasticity-of-cloud/jarz-streaming
1•mooreds•47m ago•0 comments

Ask HN: How is your work making the world a better place?

2•AbstractH24•48m ago•0 comments

Why I Keep Returning to Middle-Earth

https://www.nytimes.com/2025/12/19/opinion/tolkien-grief-lord-rings.html
1•handfuloflight•49m ago•0 comments

'Big Short' investor accuses AI hyperscalers of artificially boosting earnings (2025)

https://www.cnbc.com/2025/11/11/big-short-investor-michael-burry-accuses-ai-hyperscalers-of-artif...
3•1vuio0pswjnm7•50m ago•2 comments

Relationship Between Artist and Computer

https://www.atariarchives.org/artist/preface.php
1•alien0006•50m ago•1 comments

Bad Apple but it's played with cargo compilation output

https://old.reddit.com/r/rust/comments/1q3rrt0/media_bad_apple_but_its_played_with_cargo/
3•afdbcreid•51m ago•0 comments

A man who paves India's roads with old plastic (2018)

https://www.theguardian.com/world/2018/jul/09/the-man-who-paves-indias-roads-with-old-plastic
1•mooreds•53m ago•0 comments

Stop Playing Flappy Bird with Your New Year's Goals

https://suntreeapps.com/blog/posts/new-years-goals-difficulty-curve/
2•kenshi•54m ago•0 comments

Joke Father

https://jokefather.com/
1•fisenkodv•58m ago•0 comments

Enclose.horse

https://enclose.horse
1•justicz•1h ago•0 comments

Finley: Artificial Intelligence is making old guys obsolete

https://www.usatoday.com
1•cebert•1h ago•0 comments

High-level is the goal (but why?) [video]

https://www.youtube.com/watch?v=AmrBpxAtPrI
1•bobajeff•1h ago•0 comments

Personal notes of things I need to master, as AI writes most of the code now

https://pradyumnachippigiri.dev/blogs/engineers-future
3•PraddyChippzz•1h ago•0 comments

Meta releases open data to train General AI Co-Scientists

https://huggingface.co/datasets/facebook/research-plan-gen
1•shash42•1h ago•0 comments

Show HN: PokéPath TD – Free Pokémon tower defense game

https://pokepathgame.com
1•airobus•1h ago•0 comments