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.

I made a memory chip price tracker in five minutes

https://memoryindex.io/
1•mbloom1915•2m ago•1 comments

Amiko: A desperate virtue signalling attempt

https://ryanbigg.com/2026/08/amiko-a-desperate-virtue-signalling-attempt
1•thunderbong•7m ago•0 comments

How a liquid rocket engine is built, animated as a factory

https://laurentiugabriel.github.io/rocket-engine/
1•laurentiurad•9m ago•0 comments

Three.js gets native support for Gaussian Splatting

https://ben3d.ca/blog/gaussian-splatting-for-threejs
2•bhouston•15m ago•0 comments

Safety fears as scientists make first viruses designed by AI

https://www.theguardian.com/science/2026/aug/06/safety-fears-as-scientists-make-first-viruses-des...
1•pseudolus•16m ago•0 comments

China is gaining ground in AI. But the U.S. still has a major advantage

https://www.cnbc.com/2026/08/07/china-us-ai-race-hugging-face-models.html
1•makerdiety•18m ago•0 comments

Why AI has brought nothing more than the conceit of wisdom

https://hedgehogreview.com/web-features/thr/posts/platos-warning-has-been-vindicated
2•GeoAtreides•19m ago•0 comments

Remembering the pre-Google web, when search was an experiment

https://arstechnica.com/gadgets/2026/08/remembering-the-pre-google-web-when-search-was-an-experim...
2•pseudolus•19m ago•0 comments

The Diamond-Square Algorithm

https://diatone.net/toys/terrain
1•diatone•21m ago•1 comments

Show HN: Otaku – A Roleplay Terminal Client

https://github.com/enclavum/otaku
1•enclavum5•22m ago•0 comments

A passing star: our Sun's near miss

https://science.nasa.gov/universe/exoplanets/a-passing-star-our-suns-near-miss/
1•thunderbong•23m ago•0 comments

Auto Mode will be the default in Claude Code – because humans can't be trusted

https://thenewstack.io/claude-code-auto-mode/
1•maxloh•23m ago•0 comments

Modern Terminal File Manager

https://favflower.codeberg.page/
1•blueflowerbed•24m ago•0 comments

Google's Westinghouse Bet

https://asimovaddendum.substack.com/p/googles-westinghouse-bet
1•JohnHammersley•25m ago•0 comments

The world is full of marble. Most of it is fake [video]

https://www.youtube.com/watch?v=BLWGp8xyJEA
1•skibz•28m ago•0 comments

I couldn't have put it better myself (2016)

http://www.alexanderboot.com/i-couldnt-have-put-it-better-myself/
1•decimalenough•28m ago•0 comments

Show HN: Compactdiff – see what compaction dropped from an agent session

https://github.com/mkmkkkkk/compactdiff
3•mileskoldin•34m ago•0 comments

Show HN: Mail Attachment Lens: A Browser extension to manage Gmail attachments

https://github.com/kshk123/mail-attachment-lens
1•kshk123•36m ago•0 comments

Keeping yesterday's computers ticking takes more than nostalgia

https://www.theregister.com/offbeat/2026/08/05/keeping-yesterdays-computers-ticking-takes-more-th...
2•rbanffy•39m ago•0 comments

Ask HN: If you don't read books, what do you read?

2•kamphey•42m ago•2 comments

'I hate what AI is doing to the minds and happiness of the young'

https://www.theguardian.com/books/ng-interactive/2026/aug/08/i-hate-what-ai-is-doing-to-the-minds...
2•chrisjj•50m ago•1 comments

Show HN: Keynou Drive – Branded URL Shortener for SaaS

https://drive.keynou.com
1•not_wowinter13•51m ago•0 comments

I built AI at Amazon, then got laid off

https://www.youtube.com/watch?v=ZAom6f4-vh0
4•tcp_handshaker•55m ago•0 comments

How to Disable Gemini in Gmail and Google Docs

https://www.wired.com/story/how-to-disable-the-gemini-ai-features-in-gmail-and-google-docs/
1•joozio•56m ago•0 comments

Now we have a timeline of the OpenAI accidental attack against Hugging Face

https://simonwillison.net/2026/Aug/7/openai-timeline/
12•882542F3884314B•1h ago•2 comments

Show HN: Vibsync – One Shared Memory for Claude Code, Cursor and Codex (MCP)

https://vibsync.com/
2•snakaya•1h ago•0 comments

ByteDance is building a 10T model aimed straight at Anthropic [video]

https://www.youtube.com/shorts/2h0zVPRFb5U
3•nitrotechgames•1h ago•0 comments

New Bobby Fingers project after 2 years' wait [video]

https://www.youtube.com/watch?v=vGt7-WnWdhE
2•dmaa•1h ago•0 comments

The Future Encyclopedia of Luddism

https://thereader.mitpress.mit.edu/the-future-encyclopedia-of-luddism/
2•XzetaU8•1h ago•0 comments

Show HN: Presse – a Rust CLI tool to compress and merge PDFs

https://github.com/SimonBure/presse
3•saientifik•1h ago•0 comments