frontpage.
newsnewestaskshowjobs

Made with ♥ by @iamnishanth

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Nights Script

https://raw.githubusercontent.com/H4xScripts/Loader/refs/heads/main/loader.lua
1•wiiseguyy•3m ago•0 comments

All the Way Down

https://www.futilitycloset.com/2025/11/17/all-the-way-down-2/
1•surprisetalk•4m ago•0 comments

Wacky Fun Physics Ideas

https://scottlocklin.wordpress.com/2025/11/22/wacky-fun-physics-ideas/
1•surprisetalk•4m ago•0 comments

The Great Downzoning

https://worksinprogress.co/issue/the-great-downzoning/
1•barry-cotter•5m ago•0 comments

Proposing a New Cognitive Constant (Ca) with Full Math and Open Dataset

https://zenodo.org/records/17718241
1•Harry_Yoo•7m ago•1 comments

'Good Boy' Star Indy the Dog Becomes the First Animal Nominated for a Film Award

https://www.yahoo.com/entertainment/movies/articles/good-boy-star-indy-dog-180718575.html
1•thunderbong•9m ago•0 comments

Adventures with Chimera Linux

https://blog.xiaket.org/2025/chimera.html
1•todsacerdoti•18m ago•0 comments

Show HN: New VSCode extension: Objectify Params

https://marketplace.visualstudio.com/items?itemName=eridien.objectify-params
1•mchahn•18m ago•0 comments

Popping-and-Locking-Zed-Theme

https://github.com/randoneering/popping-and-locking-zed-theme
1•todsacerdoti•22m ago•0 comments

Understanding copy-on-write: why Redis needs memory overcommit

https://frn.sh/posts/cow/
1•shellpipe•24m ago•0 comments

Careless Whisper: Silently Monitoring Users on Mobile Instant Messengers

https://arxiv.org/abs/2411.11194
1•wakawaka28•27m ago•1 comments

An ancient foot reveals a hidden human cousin

https://www.sciencedaily.com/releases/2025/11/251128050512.htm
1•ashishgupta2209•28m ago•0 comments

Surely You're Joking, Mr. Feynman

https://en.wikipedia.org/wiki/Surely_You%27re_Joking,_Mr._Feynman!
1•nomilk•35m ago•0 comments

Tim Cook says he uses an iMac G4 as a monitor

https://www.theverge.com/2024/10/22/24276142/tim-cook-wsj-interview-every-apple-product-every-day
1•uneven9434•43m ago•1 comments

I made a free AUD/USD economic announcements dashboard for FOREX trading

https://fxmacrodata.com/dashboard/AUD_USD
1•roberttidball•47m ago•1 comments

Wine 10.20 – Run Windows Applications on Linux, BSD, Solaris and macOS

https://gitlab.winehq.org/wine/wine/-/releases/wine-10.20
2•neustradamus•52m ago•0 comments

Why I'm Shutting Down Lorelight (and What It Taught Me About Geo)

https://growwithless.com/shutting-down-lorelight/
1•anshulbhide•53m ago•1 comments

New record: Over 350k students in Japan miss school

https://www3.nhk.or.jp/nhkworld/en/news/20251029_15/
2•rguiscard•58m ago•0 comments

Tobacco Imagery in Movies Surged in 2024

https://truthinitiative.org/press/press-release/tobacco-imagery-movies-surged-2024-exposing-milli...
2•cyrusradfar•59m ago•1 comments

Ask HN: How do you inspect Avro/Protobuf records during debugging?

2•conqueso•1h ago•0 comments

Uncertainty Quantification for Auto Formalization [NeurIPS 2025]

https://github.com/DebarghaG/grammars-formal-uncertainty
1•barthelomew•1h ago•0 comments

Tokyo Tower of Babel

https://simple.wikipedia.org/wiki/Tokyo_Tower_of_Babel
2•xqcgrek2•1h ago•0 comments

Show HN: Free Z-Image – A Fast, High-Quality AI Image Generator for Creators

https://aiocmaker.com/z-image
2•qianjin1979•1h ago•0 comments

Shrinking Elephants: Managing large projects with Gradle

https://engineering.block.xyz/blog/shrinking-elephants
1•rileymichael•1h ago•0 comments

LLMs write code without compilers, could they do philosophy without logic?

https://lywald.github.io/blog_by_claude/turtles.html
1•ycosynot•1h ago•0 comments

AniFlow – Yet another AI anime image generator

https://animify.app
1•ashing•1h ago•1 comments

Show HN: HitCommit – Pay Devs to Solve Your GitHub Issues

https://hitcommit.com
1•nerdzoid•1h ago•0 comments

Lecture Summarization by Extracting Content from Whiteboards (2018)

https://github.com/bhargavaurala/accessmath-icfhr2018
1•downboots•1h ago•0 comments

DPRKDetector – Find North Korea-Affiliated Followers on X

https://dprkdetector.app
1•michael-bey•1h ago•0 comments

Fixing a Radare2 Bug from the Windows Cmd

https://trufae.github.io/aiblog/WinCdbNull-en.html
1•ibobev•1h ago•0 comments