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.

Ask HN: B2B SaaS founders: what are (or have been) your biggest bottlenecks?

1•Vaxind•1m ago•0 comments

People over Process – By Adam Ard – Rethinking Software

https://rethinkingsoftware.substack.com/p/people-over-process
2•rbanffy•3m ago•0 comments

Show HN: Next-Gen Web Extension Framework

https://github.com/wxt-dev/wxt
1•jerawaj740•4m ago•0 comments

Figure AI sued – whistleblower warned ... robots 'fracture a human skull'

https://www.cnbc.com/2025/11/21/figure-ai-sued.html
1•ncr100•7m ago•1 comments

The Evolution of AMD EPYC HPC Performance Shown in the Azure Cloud

https://www.phoronix.com/review/amd-epyc-azure-hbv2-hbv5
1•rbanffy•7m ago•0 comments

'The Age of Disclosure' Documentary About UFO's, Gets a Congressional Audience

https://www.nytimes.com/2025/11/20/movies/the-age-of-disclosure-congress.html
1•Willingham•9m ago•0 comments

Scientists create new bullet-proof fiber stronger and thinner than Kevlar

https://phys.org/news/2025-11-scientists-bullet-proof-fiber-stronger.html
3•PaulHoule•10m ago•0 comments

Conjectural connection between Quantum Mechanics and Gravitation

https://zenodo.org/records/17682883
2•nik_slusarenko•11m ago•0 comments

Ask HN: Is it time to measure Inflation and CPI without the government?

3•cyrusradfar•11m ago•0 comments

Show HN: Run a Website from an Old Phone

https://far.computer/how-to/
2•louismerlin•12m ago•0 comments

Do You Know Where Your Load-Bearing Code Is?

https://codemanship.wordpress.com/2025/11/21/do-you-know-where-your-load-bearing-code-is/
2•saikatsg•13m ago•0 comments

Ask HN: Can recursion be more useful than regular loops?

2•DenisDolya•13m ago•1 comments

Minute Cryptic

https://www.minutecryptic.com/
1•beepbooptheory•14m ago•0 comments

I'm Writing Another Book

https://ericlippert.com/2025/10/30/im-writing-another-book/
1•mooreds•15m ago•0 comments

LLM Memory System

https://github.com/For-Sunny/nova-mcp-research
1•Opus_Warrior•21m ago•1 comments

Thoughts on AI by Gavin Baker, Investor and Financial Analyst

https://twitter.com/GavinSBaker/status/1991248768654803337
1•nowflux•22m ago•0 comments

San Francisco's most controversial startup is moving to New York

https://sfstandard.com/2025/11/20/san-francisco-s-controversial-startup-moving-new-york/
1•xfr•23m ago•0 comments

Don't Look Up: NYPD Drones Take Flight

https://www.stopspying.org/drones-report
1•raybb•24m ago•0 comments

Thankful for Memory Managed Languages

https://www.mooreds.com/wordpress/archives/3720
1•mooreds•27m ago•0 comments

Chinese drone impersonates British Typhoon fighter jet

https://ukdefencejournal.org.uk/chinese-drone-impersonates-british-typhoon-fighter-jet/
4•giuliomagnifico•31m ago•0 comments

Using PlanetScale to reduce the impact of thundering herd

https://depot.dev/blog/planetscale-to-reduce-the-thundering-herd
4•goller•33m ago•1 comments

kindleship: Command-line tool to ship ebooks to your Kindle library

https://gitlab.com/esr/kindleship
1•zerealshadowban•35m ago•0 comments

Show HN: NovaCiv – A New Digital Civilization for the Age of AI

https://novaciv.space
1•prokurorus•36m ago•0 comments

The Influence of Music on Mental Health Through Neuroplasticity

https://www.mdpi.com/2076-3425/15/11/1248
3•XzetaU8•36m ago•0 comments

David Lynch on Color Timing (2004) [video]

https://www.youtube.com/watch?v=qKFFZBEiKYc
2•exvi•36m ago•0 comments

China Reaches Energy Milestone by "Breeding" Uranium from Thorium

https://humanprogress.org/china-reaches-energy-milestone-by-breeding-uranium-from-thorium/
6•surprisetalk•40m ago•0 comments

I mathematically proved the best "Guess Who?" strategy [video]

https://www.youtube.com/watch?v=_3RNB8eOSx0
1•surprisetalk•40m ago•0 comments

Gwern's "Stem Humor" Directory

https://gwern.net/doc/math/humor/index
2•surprisetalk•40m ago•0 comments

The realities of being a pop star

https://itscharlibb.substack.com/p/the-realities-of-being-a-pop-star
2•lovestory•42m ago•0 comments

Java Decompiler

http://java-decompiler.github.io
3•mooreds•45m ago•0 comments