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.

UK Government Unveils England's First Ever Men's Health Strategy

https://www.gov.uk/government/news/government-unveils-englands-first-ever-mens-health-strategy
1•robtherobber•34s ago•0 comments

China's shadow Navy trains to take Taiwan

https://www.reuters.com/graphics/USA-CHINA/TAIWAN-INVASION/zjpqdekmlvx/
1•m-hodges•41s ago•0 comments

More Updates and Fixes for ReSharper and Rider 2025.3

https://blog.jetbrains.com/dotnet/2025/11/20/resharper-rider-2025-3-0-2/
1•quapster•3m ago•0 comments

The Smellscapes of Cities

https://nemos.hypotheses.org/3072
2•mrzool•4m ago•0 comments

Study: People Support Higher Taxes After Understanding Benefits of Public Goods

https://www.tus.ac.jp/en/mediarelations/archive/20251114_3328.html
2•rustoo•5m ago•0 comments

Ask HN: How did your startup get its first real users?

2•paulwilsonn•6m ago•0 comments

Grace Blackwell Desktop Supercomputer: First Impressions

https://jasoneckert.github.io/myblog/grace-blackwell/
2•speckx•7m ago•0 comments

Redundancy vs. dependencies: which is worse?

https://www.yosefk.com/blog/redundancy-vs-dependencies-which-is-worse.html
2•gavinhoward•8m ago•0 comments

The Internet Archive Wayback Machine Is Down

https://bsky.app/profile/archive.org/post/3m62vw5dsr22r
2•AdmiralAsshat•8m ago•0 comments

rep+: A Lightweight Burp Suite Repeater Inside Chrome DevTools

https://github.com/bscript/rep
2•bscript•9m ago•1 comments

Sequoia's 'imperial' Roelof Botha pushed out by top lieutenants

https://www.ft.com/content/0f16e194-5e9b-4486-988e-6f90f927b153
2•wslh•10m ago•0 comments

First Air-Breathing Spacecraft

https://rdw.com/newsroom/redwire-awarded-44-million-darpa-contract-to-advance-very-low-earth-orbi...
2•bilsbie•14m ago•0 comments

Hacker News Post Formatter – Preview Your HN Posts

https://www.hnpostformatter.com/
3•janpio•14m ago•0 comments

Show HN: My Light & Spirit – A Clever Indie Puzzle Where Light Becomes Your Tool

https://groober.itch.io/my-light-spirit
2•electrodisk•15m ago•0 comments

China-linked hackers hijack Asus routers

https://www.scworld.com/brief/china-linked-hackers-hijack-thousands-of-asus-routers
3•ilamont•15m ago•0 comments

Blockage Mechanism and Unblocking Technology Around Petroleum Wellbores

https://www.mdpi.com/2079-6412/15/11/1293
2•PaulHoule•16m ago•0 comments

Show HN: Rapid-rs – Zero-config web framework for Rust

https://crates.io/crates/rapid-rs
3•ashish_sharda•16m ago•0 comments

Wearable bandage device around finger lets users feel the digital world

https://www.designboom.com/technology/stretchable-bandage-device-voxelite-finger-users-feel-digit...
2•65•18m ago•0 comments

The evolution of neoclouds and their next moves

https://www.mckinsey.com/capabilities/tech-and-ai/our-insights/the-evolution-of-neoclouds-and-the...
1•gmays•19m ago•0 comments

Show HN: OpenBotAuth – Open-Source Web Bot Auth with Social Registry

https://github.com/OpenBotAuth/openbotauth
2•gauravguitara•21m ago•0 comments

AI Eats the World [video]

https://www.youtube.com/watch?v=niJpDnNtNp4
3•gmays•21m ago•0 comments

Everything you need to know about hard drive vibration

https://www.ept.ca/features/everything-need-know-hard-drive-vibration/
2•asdefghyk•24m ago•1 comments

Show HN: Quick install script for self-hosted Forgejo (Git+CI) server

https://wkoszek.github.io/easyforgejo/
4•wkoszek•24m ago•1 comments

Fast and Specialized Rust CSV Readers Using SIMD Instructions

https://github.com/medialab/simd-csv
2•Yomguithereal•25m ago•0 comments

How much solopreneurs make and how they make their money [research report]

https://www.adrianatica.com/state-of-solopreneurship/
3•adriana_tica•26m ago•1 comments

Move over Harvard and MIT–this university might be winning the AI race

https://fortune.com/2025/11/19/us-china-ai-race-higher-education-tsinghua-university-outpacing-iv...
3•nis0s•26m ago•1 comments

Trillion‑Scale RL in Real Time: Awex Delivers Second‑Level Weight Updates

https://medium.com/@shawn.ck.yang/awex-an-ultra-fast-weight-sync-framework-powering-trillion-scal...
2•chaokunyang•27m ago•2 comments

Saudi Arabia's Prince Has Big Plans, but His Giant Fund Is Low on Cash

https://www.nytimes.com/2025/11/19/business/pif-saudi-arabia-fund-problems.html
4•toomanyrichies•28m ago•2 comments

Foveated streaming is the genius tech behind Valve's Steam Frame

https://www.pcgamer.com/hardware/vr-hardware/foveated-streaming-genius-tech/
3•thunderbong•29m ago•0 comments

Ask HN: What is the best way to see what files are being read in Windows?

2•jacobwilliamroy•29m ago•0 comments