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.

Game Boy Advance SP with 162Wh Replaceable Battery

https://old.reddit.com/r/Gameboy/comments/1oiwk9g/gba_sp_with_162000mwh_replaceable_battery/
1•gnabgib•2m ago•0 comments

Zoomers are officially worse at passwords than 80-year-olds

https://www.theregister.com/2025/11/18/zoomer_passwords/
1•pseudolus•3m ago•0 comments

Middle-class pulls back, alarming retailers: 'signs of real distress'

https://www.businessinsider.com/middle-class-shoppers-pull-back-warning-signs-economy-home-depot-...
1•zerosizedweasle•4m ago•0 comments

SupaPwn: Hacking Our Way into Lovable's Office and Helping Secure Supabase

https://www.youtube.com/watch?v=jAWDq6bSOBw
1•zeyu1337•5m ago•1 comments

YouTube Unblocked – Safe Ways to Watch at School, Work

https://www.goproxy.com/blog/youtube-unblocked/
1•rgggggggg•7m ago•0 comments

Asymmetry of Verification and Verifier's Rule

https://www.jasonwei.net/blog/asymmetry-of-verification-and-verifiers-law
2•gmays•21m ago•0 comments

Context Management in Amp

https://ampcode.com/guides/context-management
1•gmays•26m ago•0 comments

Visual Studio Code Private Marketplace

https://code.visualstudio.com/blogs/2025/11/18/PrivateMarketplace
2•soheilpro•27m ago•0 comments

What If 'America First' Appears to Work?

https://www.theatlantic.com/international/2025/11/trump-foreign-policy/684969/
1•petethomas•28m ago•1 comments

Microsoft making major changes to how drivers are built, verified on Windows 11

https://www.windowscentral.com/microsoft/windows-11/microsoft-is-raising-the-bar-and-making-major...
2•thunderbong•28m ago•0 comments

Klimt Painting Becomes the Most Expensive Work of Modern Art at $236.4M

https://www.wsj.com/arts-culture/fine-art/klimt-cattelan-sothebys-auction-record-breuer-lauder-eb...
1•petethomas•31m ago•0 comments

Anthropic valued in range of $350B following investment deal

https://www.cnbc.com/2025/11/18/anthropic-ai-azure-microsoft-nvidia.html
2•personjerry•37m ago•1 comments

Colleges Are Surrendering to AI

https://www.persuasion.community/p/why-ill-encourage-my-students-to
3•pseudolus•38m ago•1 comments

New Apple Watch cases is 3D-Printed

https://www.apple.com/newsroom/2025/11/mapping-the-future-with-3d-printed-titanium-apple-watch-ca...
3•regsvr32•40m ago•3 comments

Two long-lost organ pieces by JavaScript Bach performed for first time

https://www.theguardian.com/music/2025/nov/17/lost-js-bach-organ-compositions-performed-for-first...
3•lordleft•40m ago•2 comments

World Cup ticket holders can get priority visa interviews

https://www.espn.com/soccer/story/_/id/47004400/world-cup-ticket-holders-get-priority-visa-interv...
2•1659447091•45m ago•0 comments

Startup offers free AI code review tool for non-commercial open source projects

https://blog.macroscope.com/blog/free-for-open-source
1•curiouska•45m ago•0 comments

Meta's Victory Opens the Way for Silicon Valley to Go Deal Shopping

https://www.nytimes.com/2025/11/18/technology/meta-ruling-silicon-valley-start-ups.html
2•jameslk•55m ago•0 comments

OpenHands Raises $18.8M to Build an Open Standard for Autonomous Dev

https://openhands.dev/blog/weve-just-raised-18-8m-to-build-the-open-standard-for-autonomous-softw...
4•mitchwainer•59m ago•0 comments

In Nebraska, Makeovers for Buildings That Don't Price Out the Locals

https://www.nytimes.com/2025/11/18/climate/nebraska-energy-efficiency-50-states-50-fixes.html
3•fleahunter•1h ago•0 comments

Man who cryogenically froze late wife sparks debate by dating new partner

https://www.bbc.com/news/articles/c78z2m43ykjo
1•1659447091•1h ago•0 comments

DOE gives Microsoft partner $1B loan to restart Three Mile Island reactor

https://techcrunch.com/2025/11/18/trump-doe-gives-microsoft-partner-1b-loan-to-restart-three-mile...
16•pseudolus•1h ago•0 comments

Driving an E.V. Across North Dakota? Thank the Standing Rock Tribe

https://www.nytimes.com/2025/11/18/climate/electic-vehicle-chargers-north-dakota.html
2•fleahunter•1h ago•0 comments

Google Antigravity

https://simonwillison.net/2025/Nov/18/google-antigravity/
2•vismit2000•1h ago•0 comments

First kiss dates back 21M years

https://www.bbc.com/news/articles/cr43gq61g2qo
2•1659447091•1h ago•0 comments

Ancient kiss-tory: new perspectives on the evolution of early historical kissing

https://www.sciencedirect.com/science/article/pii/S1090513825001278
2•defrost•1h ago•0 comments

AntCalc

https://www.breatharian.eu/hw/ch32libsdk/index_en.html#antcalc
1•jamesbowman•1h ago•0 comments

Unoffice Hours Webring

https://unofficehours.com/
2•hunvreus•1h ago•0 comments

The Zero-Bullshit Protocol – Hallucination-Proof AI Engineering System

https://gracefultc.gumroad.com/l/ctgyvz
1•Thugonerd•1h ago•2 comments

Show HN: I Spent 200 Hours Compiling These Open Source Reviews

https://opensourcereviews.github.io/
1•honestabraham•1h ago•0 comments