frontpage.
newsnewestaskshowjobs

Open Source @Github

fp.

Open in hackernews

Ask HN: Java why put string in a constant?

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

Think this open-source Flutter-native AI agent worth building?

https://github.com/anasfik/flutter_copilot
1•gwhyyy•26s ago•1 comments

Cheap USB-to-HDMI adapters are terrible, and yet useful

https://ounapuu.ee/posts/2026/06/22/usb-hdmi-adapter/
1•speckx•1m ago•0 comments

A Game Boy emulator that runs in your terminal (Rust)

https://github.com/dquigles/terminal_gameboy
1•Luvtooty•1m ago•0 comments

Swift Package Index Joins Apple

https://swiftpackageindex.com/blog/swift-package-index-joins-apple
1•JDevlieghere•2m ago•0 comments

PhoneBuddy: Training Open Models for Agentic Phone Use

https://phonebuddyai.github.io/
1•ilreb•2m ago•0 comments

Agents Declaration – Designing an Agents Orchestration Library – Part II

https://ffacu.dev/blog/arm-agent-fleet
1•ffacu•2m ago•0 comments

SharePoint Copilot Apps

https://devblogs.microsoft.com/microsoft365dev/going-beyond-text-in-microsoft-365-copilot-introdu...
1•saikatsg•4m ago•0 comments

Double stacked Indian freight trains

https://old.reddit.com/r/trains/comments/1udldj0/double_stacked_indian_freight_trains/
1•taubek•6m ago•0 comments

99% compressed, 1% on the bill: I audited 1B tokens to find out why

https://nuxs.ai/study
1•josuramos•6m ago•0 comments

Show HN: Grove – fast source insights for coding agents – via Tree-sitter

https://github.com/Entelligentsia/grove
1•bonigv•7m ago•0 comments

Oracle sheds 21,000 roles over the past year

https://www.cnbc.com/2026/06/23/oracle-ai-job-cuts-layoffs-21000.html
2•leopoldj•12m ago•0 comments

FUTO Swipe – A new swipe typing model

https://swipe.futo.tech/
3•futohq•12m ago•0 comments

Don't Trust OpenAI, "This Is Your Final Warning"

https://247wallst.com/investing/2026/06/21/dont-trust-openai-this-is-your-final-warning/
1•T-A•13m ago•0 comments

Hubble: Notepad for Agents

https://www.hubble.md/
1•handfuloflight•16m ago•0 comments

The truth about being a manager

https://sofiakodar.github.io/posts/becomingmanager/
9•adunk•16m ago•1 comments

NYC Wants to End Surveillance Pricing

https://prospect.org/2026/06/23/new-york-city-wants-to-end-surveillance-pricing/
4•h2si•17m ago•0 comments

My Steam Machine Is a 50ft HDMI Cable

https://blog.matthewbrunelle.com/my-steam-machine-is-a-50ft-hdmi-cable/
3•speckx•17m ago•0 comments

Skynet Progress Report

https://blog.dshr.org/2026/03/skynet-progress-report.html
1•jumpkick•19m ago•0 comments

A Man Has Been Drawing a Map of an Imaginary Land Since 1963

https://www.openculture.com/2026/06/this-man-has-been-drawing-a-map-of-an-imaginary-land-since-19...
2•Brajeshwar•20m ago•0 comments

Apache Grails 7.0.12 and 7.1.12

https://github.com/apache/grails-core
1•theanonymousone•20m ago•0 comments

JBang: Unleash the Power of Java

https://github.com/jbangdev/jbang
2•theanonymousone•22m ago•0 comments

New chip could help tiny robots traverse complex environments

https://news.mit.edu/2026/new-chip-could-help-tiny-robots-traverse-complex-environments-0623
2•droidjj•23m ago•0 comments

Ancient Tablets Show Markets Worked 4k Years Before Economists Explained Them

https://thedailyeconomy.org/article/ancient-clay-tablets-show-markets-worked-4000-years-before-ec...
2•NaOH•23m ago•0 comments

Over 1700 Cargill Meatpacking Workers Locked Out After Demanding Bathroom Breaks

https://sentientmedia.org/cargill-meatpacking-workers-locked-out/
2•randycupertino•23m ago•0 comments

Anthropic rolls out Claude Tag, your new agentic AI coworker in Slack

https://www.zdnet.com/article/anthropic-claude-tag-agentic-ai-coworker-slack/
1•thm•23m ago•0 comments

AI-Generated UI Is Inaccessible by Default

https://master.dev/blog/ai-generated-ui-is-inaccessible-by-default/
1•speckx•23m ago•0 comments

Thirsty and power hungry: Australia's datacenter boom

https://www.theguardian.com/technology/2026/jun/22/australia-ai-datacentre-boom-environmental-imp...
2•logickkk1•24m ago•0 comments

Looking Ahead to Postgres 19

https://www.snowflake.com/en/blog/engineering/postgresql-19-features-beta/
2•craigkerstiens•26m ago•0 comments

Charon: A blind, end-to-end-encrypted marketplace for LLM inference

https://github.com/DeepBlueDynamics/charon
1•kordlessagain•26m ago•1 comments

GVoice suspended in error: no Takeout, portout, incoming text forwarding blocked

https://www.google.com/appsstatus/dashboard/incidents/qqWhZSpoVLJWur1GfHfu
1•ProblemExplorer•27m ago•1 comments